java使用telnet连接远程服务器调用cmd命令时 报堆溢出错误是什么原因? 200
publicclasstest6{TelnetClienttelnet=newTelnetClient("VT220");InputStreamin;PrintStrea...
public class test6 { TelnetClient telnet = new TelnetClient("VT220"); InputStream in; PrintStream out; String prompt = ">"; public test6(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) { test6 ws = new test6("192.168.40.128", 23, "wangguofei", "!QAZ2wsx"); // 执行的命令 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(); }}
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询