加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java 登陆远程linux 执行 shell命令

(2012-10-12 17:28:49)
标签:

杂谈

分类: 学习
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
 

public class ssh {
    
    public static void main(String[] args)
    {
            String hostname = "192.168.5.102";
            String username = "root";
            String password = "leadtone";
            
            Connection conn = new Connection(hostname);
            Session sess = null;
            try
            {
               
                conn.connect();
               
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false)
                        throw new IOException("Authentication failed.");
               
                sess = conn.openSession();
                //sess.execCommand("uname -a && date && uptime && who");
                sess.execCommand("deleteuser -c 2 -P 123456 -i 7  &&  deleteuser -c 2 -P 123456 -i 8");
                //same
                sess.execCommand("deleteuser -c 2 -P 123456 -i 7  \n deleteuser -c 2 -P 123456 -i 8");
                // sess.execCommand("listuser -c 9189 -A admin -P 123456  ");
                System.out.println("Here is some information about the remote host:");
                InputStream stdout =    sess.getStdout() ;
                BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
                StringBuilder sb = new StringBuilder();
                while (true)
                {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    sb.append(line);
                }
               
                //  System.out.println("ExitCode: " + sess.getExitStatus());
                System.out.println(sb.toString());
            }catch (IOException e){
                e.printStackTrace(System.err); System.exit(2);
            }finally{
                
                sess.close();
               
                conn.close();
            }
        }
    
    
    public static String remoteRunCmd(String  cmd)
    {
            String hostname = "192.168.5.102";
            String username = "root";
            String password = "leadtone";
            
            Connection conn = new Connection(hostname);
            Session sess = null;
            try
            {
               
                conn.connect();
               
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false)
                        throw new IOException("Authentication failed.");
               
                sess = conn.openSession();
                //sess.execCommand("uname -a && date && uptime && who");
               // sess.execCommand("deleteuser -c 2 -P 123456 -i 10 ");
                // sess.execCommand("listuser -c 9189 -A admin -P 123456  ");
                sess.execCommand( cmd);
                InputStream stdout =    sess.getStdout() ;
                BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
                StringBuilder sb = new StringBuilder();
                while (true)
                {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    sb.append(line);
                }
               
                //  System.out.println("ExitCode: " + sess.getExitStatus());
               return sb.toString();
            }catch (IOException e){
                return "";
            }finally{
                
                sess.close();
               
                conn.close();
            }
        }
    
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有