开发者社区> 问答> 正文

java使用telnet调用远程cmd命令

"

代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;

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

public class WindowsShell {

TelnetClient telnet = new TelnetClient("VT220");

InputStream in;
PrintStream out;

String prompt = ">";

public WindowsShell(String ip, int port, String user, String password) {
    try {
        telnet.connect(ip, port);
        in = telnet.getInputStream();
        out = new PrintStream(telnet.getOutputStream());
        login(user, password);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
/**
 * 读取分析结果
 * 
 * @param pattern
 * @return
 */
public String readUntil(String pattern) {
    try {
        char lastChar = pattern.charAt(pattern.length() - 1);
        StringBuffer sb = new StringBuffer();
        char ch = (char) in.read();
        while (true) {
            sb.append(ch);
            if (ch == lastChar) {
                if (sb.toString().endsWith(pattern)) {
                    return sb.toString();
                }
            }
            ch = (char) in.read();

// System.out.print(ch);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * 写操作
 * 
 * @param value
 */
public void write(String value) {

    try {
        out.println(value);
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 向目标发送命令字符串
 * 
 * @param command
 * @return
 */
public String sendCommand(String command) {

    try {
        write(command);
        return readUntil(prompt + "");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

    /**
 * 登录
 * 
 * @param user
 * @param password
 */
public void login(String user, String password) {
    // read()Until("login:");
    readUntil("login:");
    write(user);
    readUntil("password:");
    write(password);
    readUntil(prompt + "");
}

/**
 * 关闭连接
 */
public void disconnect() {
    try {
        telnet.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
        WindowsShell ws = new WindowsShell("192.168.100.100", 23, "Administrator", "123456");

// System.out.println(ws);

        // 执行的命令
        String str = ws.sendCommand("ipconfig");
        try{
            str = new String(str.getBytes("ISO-8859-1"),"GBK");

        }catch(UnsupportedEncodingException e){
            e.printStackTrace();

}

        System.out.println(str);
        ws.disconnect();
}

}

运行后报错如下:

这样应该如何解决呢?

"

展开
收起
因为相信,所以看见。 2020-05-27 10:04:15 3255 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    因为连接被拒绝了,你先试试本地telnet能不能连上去?

    2020-05-27 17:32:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载