Java通过Telnet连接执行shell脚本

import java.io.InputStream; import java.io.PrintStream; import org.apache.commons.net.telnet.TelnetClient; /** * @jdk 1.5.0_16 * @date Feb 26, 2011 */ public class NetTelnet { private TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private char prompt = '$'; public NetTelnet(String ip, int port, String user, String password) { try { telnet.connect(ip, port); in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream()); this.prompt = user.equals("root") ? '#' : '$'; login(user, password); } catch (Exception e) { e.printStackTrace(); } } public void login(String user, String password) { readUntil("login:"); write(user); readUntil("Password"); write(password); readUntil(prompt + " "); } 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(); } } catch (Exception e) { e.printStackTrace(); } return null; } public void write(String value) { try { out.println(value); out.flush(); } catch (Exception e) { e.printStackTrace(); } } 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) { System.out.println("启动Telnet..."); String ip = "***.***.***.***"; int port = 23; String user = "******"; String password = "******"; NetTelnet telnet = new NetTelnet(ip, port, user, password); telnet.sendCommand("export LANG=en"); String r1 = telnet.sendCommand("cd /home/postgres/shp"); String r2 = telnet.sendCommand("pwd"); String r3 = telnet.sendCommand("ls"); String r4 = telnet.sendCommand("tar cvf biaoshi.tar biaoshi.*"); System.out.println("显示结果"); System.out.println(r1); System.out.println(r2); System.out.println(r3); System.out.println(r4); telnet.disconnect(); System.out.println("退出Telenet!"); } }

 

避免unix命令长度限制

解决回显的部分中文乱码问题:

import java.io.InputStream; import java.io.PrintStream; import org.apache.commons.net.telnet.*; /** * @jdk 1.5.0_16 * @date Feb 26, 2011 */ public class NetTelnet { private TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private char prompt = '$'; public NetTelnet(String ip, int port, String user, String password) { try { telnet.connect(ip, port); in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream(), false); this.prompt = user.equals("root") ? '#' : '$'; login(user, password); } catch (Exception e) { e.printStackTrace(); } } public void login(String user, String password) { readUntil("login: "); write(user); readUntil("Password: "); write(password); readUntil(prompt + " "); } public String readUntil(String pattern) { try { byte[] buff = new byte[1024]; int ret_read = 0; String str = ""; do { ret_read = in.read(buff); if (ret_read > 0) { str += new String(buff, 0, ret_read, "GBK"); if (str.endsWith(pattern)) { System.out.println(str); break; } } } while (ret_read >= 0); return str; // char ch = (char) in.read(); // while (true) { // // sb.append(ch); // if (ch == lastChar) { // // if (sb.toString().endsWith(pattern)) { // System.out.println(sb.toString()); // return sb.toString(); // } // } // ch = (char) in.read(); // } } catch (Exception e) { e.printStackTrace(); } return "ERROR! when the program execute"; } public void write(String value) { try { for (int i = 0; i < value.length(); i++) { out.print(value.charAt(i)); out.flush(); if (i != 0 && i % 72 == 0) { out.print('//'); out.flush(); out.print('/n'); out.flush(); } } out.println(); out.flush(); // out.println(value); // out.flush(); } catch (Exception e) { e.printStackTrace(); } } 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(); } } }

你可能感兴趣的:(Java)