Java实现telnet远程调用脚本

java中远程登陆主机并远程调用执行shell脚本

Java代码   收藏代码
  1. package place.in.javaeye;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.PrintStream;  
  5.   
  6. import org.apache.commons.net.telnet.TelnetClient;  
  7. public class Shell  
  8. {  
  9.     private TelnetClient telnet = new TelnetClient();  
  10.   
  11.     private InputStream in;  
  12.   
  13.     private PrintStream out;  
  14.   
  15.     private char prompt = '$';// 普通用户结束  
  16.   
  17.     public Shell(String ip, int port, String user, String password)  
  18.     {  
  19.         try  
  20.         {  
  21.             telnet.connect(ip, port);  
  22.             in = telnet.getInputStream();  
  23.             out = new PrintStream(telnet.getOutputStream());  
  24.             // 根据root用户设置结束符  
  25.             this.prompt = user.equals("root") ? '#' : '>';  
  26.             login(user, password);  
  27.         }  
  28.         catch (Exception e)  
  29.         {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     /** 
  35.      * 登录 
  36.      * 
  37.      * @param user 
  38.      * @param password 
  39.      */  
  40.     public void login(String user, String password)  
  41.     {  
  42. //        readUntil("login:");  
  43.         readUntil("login:");  
  44.         write(user);  
  45.         readUntil("Password:");  
  46.         write(password);  
  47.         readUntil(prompt + "");  
  48.     }  
  49.   
  50.     /** 
  51.      * 读取分析结果 
  52.      * 
  53.      * @param pattern 
  54.      * @return 
  55.      */  
  56.     public String readUntil(String pattern)  
  57.     {  
  58.         try  
  59.         {  
  60.             char lastChar = pattern.charAt(pattern.length() - 1);  
  61.             StringBuffer sb = new StringBuffer();  
  62.             char ch = (char)in.read();  
  63.             while (true)  
  64.             {  
  65.                 sb.append(ch);  
  66.                 if (ch == lastChar)  
  67.                 {  
  68.                     if (sb.toString().endsWith(pattern))  
  69.                     {  
  70.                         return sb.toString();  
  71.                     }  
  72.                 }  
  73.                 ch = (char)in.read();  
  74.                 System.out.print(ch);  
  75.             }  
  76.         }  
  77.         catch (Exception e)  
  78.         {  
  79.             e.printStackTrace();  
  80.         }  
  81.         return null;  
  82.     }  
  83.   
  84.     /** 
  85.      * 写操作 
  86.      * 
  87.      * @param value 
  88.      */  
  89.     public void write(String value)  
  90.     {  
  91.         try  
  92.         {  
  93.             out.println(value);  
  94.             out.flush();  
  95.         }  
  96.         catch (Exception e)  
  97.         {  
  98.             e.printStackTrace();  
  99.         }  
  100.     }  
  101.   
  102.     /** 
  103.      * 向目标发送命令字符串 
  104.      * 
  105.      * @param command 
  106.      * @return 
  107.      */  
  108.     public String sendCommand(String command)  
  109.     {  
  110.         try  
  111.         {  
  112.             write(command);  
  113.             return readUntil(prompt + "");  
  114.         }  
  115.         catch (Exception e)  
  116.         {  
  117.             e.printStackTrace();  
  118.         }  
  119.         return null;  
  120.     }  
  121.   
  122.     /** 
  123.      * 关闭连接 
  124.      */  
  125.     public void disconnect()  
  126.     {  
  127.         try  
  128.         {  
  129.             telnet.disconnect();  
  130.         }  
  131.         catch (Exception e)  
  132.         {  
  133.             e.printStackTrace();  
  134.         }  
  135.     }  
  136.     public static void main(String[] args) {  
  137.         TelnetClient telnet = new TelnetClient();  
  138.         try {  
  139.             Shell she =new Shell("10.**.***.***"23"***""***");  
  140.             System.out.println(she);  
  141.             System.out.println(she.sendCommand("ls"));  
  142.             she.disconnect();  
  143.   
  144.         }catch (Exception e) {  
  145.             // TODO: handle exception  
  146.         }  
  147.   
  148.     }  


 原文:http://arne3166.iteye.com/blog/756533

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值