tmh 2008-06-26 11:31
浏览 148
已采纳

一个关于java telnet的批处理郁闷问题

程序功能目标:通过一个已经连接telnet 发送另外一条telnet 命令跳转到跳转到另外一个主机。(第一个支持telnet命令)
代码如下:
[code="java"]
package tmh.tvs.util.net.telnet;

import org.apache.commons.net.telnet.*;

import java.io.*;
import java.net.SocketException;
import java.util.StringTokenizer;

public class TmhTelnet implements Runnable, TelnetNotificationHandler {

private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;
public static void main(String args[]){     
    Thread t = new Thread(new TmhTelnet("125.46.86.123",23,"*****","******"));
    t.start();
}
public TmhTelnet(String host, int port,String username,String password) {
    if(intconnect(host,port)){
        write(username);
        write(password);
        writescript("command.txt");
    }
}

private void writescript(String filename) {
    try {
        if(new File(filename).exists()){
            FileReader f = new FileReader(filename);
            BufferedReader reader = new BufferedReader(f);
            String command = "";
            while(command !=null){
                command = reader.readLine();
                write(command);
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
private void write(String command) {
    try{
        System.out.println("command:>>>>>>>>>"+command);
        out.println(command);
        out.flush();            
    }catch(Exception e){
        e.printStackTrace();
    }
}

private boolean intconnect(String host, int port) {
    try {
        tc = new TelnetClient();
        TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
                "VT100", false, false, true, false);
        EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
                true, false);
        SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
                true, true, true);

        tc.addOptionHandler(ttopt);
        tc.addOptionHandler(echoopt);
        tc.addOptionHandler(gaopt);
        tc.connect(host, port);
        in = tc.getInputStream();
        out = new PrintStream(tc.getOutputStream());
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        try {
            tc.disconnect();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return false;
    }       
}

public void receivedNegotiation(int negotiation_code, int option_code) {
    String command = null;
    if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
        command = "DO";
    } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
        command = "DONT";
    } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
        command = "WILL";
    } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {
        command = "WONT";
    }
    System.out.println("Received " + command + " for option code "
            + option_code);
}

/***************************************************************************
 * Reader thread. Reads lines from the TelnetClient and echoes them on the
 * screen.
 **************************************************************************/
public void run() {
    InputStream instr = tc.getInputStream();

    try {

        byte[] buff = new byte[1024];
        int ret_read = 0;
        do {
            ret_read = instr.read(buff);
            if (ret_read > 0) {
                System.out.print(new String(buff, 0, ret_read));
            }

        } while (ret_read >= 0);
    } catch (Exception e) {
        System.err.println("Exception while reading socket:"
                + e.getMessage());
    }

    try {
        tc.disconnect();
    } catch (Exception e) {
        System.err.println("Exception while closing telnet:"
                + e.getMessage());
    }
}

}

[/code]

输出结果:

command:>>>>>>>>>*****
command:>>>>>>>>>*******
command:>>>>>>>>>telnet 10.1.2.2
command:>>>>>>>>>******
command:>>>>>>>>>dis mac
command:>>>>>>>>>null


  • Copyright (c) 2004-2007 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
  • Without the owner's prior written consent, *
  • no decompiling or reverse-engineering shall be allowed. * ********************************************************************************

Login authentication

Username:admin
Password:
telnet 10.1.2.2
Trying 10.1.2.2 ...
Press CTRL+K to abort
Connected to 10.1.2.2 ...password:在这里应该是向下走的可是不行(很不解)

问题:在使用键盘输入的时候可以跳转,但是使用批处理的时候只到了要求密码的地方。请各位给点建议。我用的apache的commons net 包

  • 写回答

1条回答 默认 最新

  • llade163 2008-06-27 01:37
    关注

    花了2个小时研究了一下telnet,学艺不精,复杂的就不会了,在你的基础上修改了一下,能跑,不过很不完善(因为你的程序很有问题,HOHO),如果command.txt最后一个命令不是exit,则需要自己结束JVM。初步估计你的程序没有等到提示符出来就把所有命令都输出了。
    [code="java"]package tmh.tvs.util.net.telnet;
    import org.apache.commons.net.telnet.*;

    import java.io.*;

    import java.net.SocketException;

    import java.util.StringTokenizer;

    public class TmhTelnet implements Runnable, TelnetNotificationHandler {

    private static TelnetClient tc = null;   
    private InputStream in;   
    private PrintStream out;
    private Thread tThread;
    private boolean connected=true;
    public static void main(String args[]){
        TmhTelnet telnet=new TmhTelnet ("125.46.86.123",23);
        telnet.tThread = new Thread(telnet);   
        telnet.tThread.start();
    
        telnet.write("******");   
        telnet.write("******");
        telnet.writescript("command.txt");   
    }   
    public TestTelnet(String host, int port) {   
        intconnect(host,port);
    }   
    
    private void writescript(String filename) {   
        try {   
            if(new File(filename).exists()){   
                FileReader f = new FileReader(filename);   
                BufferedReader reader = new BufferedReader(f);   
                String command = "";   
                while(command !=null){   
                    command = reader.readLine();   
                    if(command!=null)write(command);   
                }   
            }   
        } catch (FileNotFoundException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
    
    
    }   
    private void write(String command) {   
        try{   
            System.out.println("command:>>>>>>>>>"+command);   
            out.println(command);
            out.flush();
            while(!tThread.getState().equals(Thread.State.BLOCKED));
        }catch(Exception e){   
            e.printStackTrace();   
        }   
    }   
    
    private boolean intconnect(String host, int port) {   
        try {   
            tc = new TelnetClient();   
            TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(   
                    "VT100", false, false, true, false);   
            EchoOptionHandler echoopt = new EchoOptionHandler(true, false,   
                    true, false);   
            SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,   
                    true, true, true);   
    
            tc.addOptionHandler(ttopt);   
            tc.addOptionHandler(echoopt);   
            tc.addOptionHandler(gaopt);
            tc.connect(host, port);   
            in = tc.getInputStream();   
            out = new PrintStream(tc.getOutputStream());  
            return true;   
        } catch (Exception e) {   
            e.printStackTrace();   
            try {   
                tc.disconnect();   
            } catch (IOException e1) {   
                // TODO Auto-generated catch block   
                e1.printStackTrace();   
            }   
            return false;   
        }          
    }   
    
    public void receivedNegotiation(int negotiation_code, int option_code) {   
        String command = null;   
        if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {   
            command = "DO";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {   
            command = "DONT";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {   
            command = "WILL";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {   
            command = "WONT";   
        }   
        System.out.println("Received " + command + " for option code "  
                + option_code);   
    }   
    
    /***************************************************************************  
     * Reader thread. Reads lines from the TelnetClient and echoes them on the  
     * screen.  
     **************************************************************************/  
    public void run() {   
        InputStream instr = tc.getInputStream();   
    
        try {   
    
            byte[] buff = new byte[1024];   
            int ret_read = 0;   
            do {   
                ret_read = instr.read(buff);   
                if (ret_read > 0) {   
                    System.out.print(new String(buff, 0, ret_read));   
                }   
            } while (ret_read >= 0); 
        } catch (Exception e) {   
            System.err.println("Exception while reading socket:"  
                    + e.getMessage());   
        }   
    
        try {   
            tc.disconnect();   
        } catch (Exception e) {   
            System.err.println("Exception while closing telnet:"  
                    + e.getMessage());   
        }   
    }   
    

    } [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组