java telnet 在linux/windows上远程执行cmd

linux:

import java.io.InputStream;
import java.io.PrintStream;
 
import org.apache.commons.net.telnet.TelnetClient;
 
public class Shell {
    private TelnetClient telnet = new TelnetClient();
 
    private InputStream in;
 
    private PrintStream out;
 
    private char prompt = '$';// 普通用户结束
 
    public Shell(String ip, int port, String user, String password) {
        try {
            telnet.connect(ip, port);
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());
            // 根据root用户设置结束符
            this.prompt = user.equals("root") ? '#' : '>';
            login(user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 登录
     * 
     * @param user
     * @param password
     */
    public void login(String user, String password) {
        // read()Until("login:");
        readUntil("login:");
        write(user);
        readUntil("Password:");
        write(password);
        readUntil(prompt + "");
    }
 
    /**
     * 读取分析结果
     * 
     * @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;
    }
 
    /**
     * 关闭连接
     */
    public void disconnect() {
        try {
            telnet.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
//      TelnetClient telnet = new TelnetClient();
        try {
            Shell she = new Shell("192.168.1.10", 23, "root", "123456");
            System.out.println(she);
            //执行的命令
            System.out.println(she.sendCommand("ll"));
            she.disconnect();
 
        } catch (Exception e) {
            // TODO: handle exception
        }
 
    }
}

windows:

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();

	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 user
	 * @param password
	 */
	public void login(String user, String password) {
		// read()Until("login:");
		readUntil("login:");
		write(user);
		readUntil("password:");
		write(password);
		readUntil(prompt + "");
	}

	/**
	 * 读取分析结果
	 * 
	 * @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;
	}

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

	public static void main(String[] args) {
			WindowsShell ws = new WindowsShell("192.168.202.131", 23, "root", "123456");
//			System.out.println(ws);
			// 执行的命令
			String str = ws.sendCommand("ipconfig");
			System.out.println(str);
			ws.disconnect();
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值