Java實現遠程ssh執行shell腳本對文件打包下載

public class SSHsftp {

private static Vector<String> stdout;  

public static Session getSession(String host,String user,String psw,int port){

    Session session =null;
    try {
        JSch jsch=new JSch();

        session = jsch.getSession(user, host, port);
        Properties config = new Properties();


        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(psw);
        //session.setTimeout(10000);
        session.connect();
        System.out.println("Session connected. session會話鏈接建立");

    } catch (JSchException e) {
        e.printStackTrace();
    }finally{

    }
    return session;
}

public  static String exec(String directory, String downloadFile, String saveFile,String host,String user,String psw,int port,String command){
    ChannelExec ExecChannel =null;
    String result= "";
    ChannelSftp sftp=null;
    Session session =null;
     int returnCode = 0;
    try {
        session = SSHsftp.getSession(host, user, psw, port);

        ExecChannel = (ChannelExec) session.openChannel("exec");
        ExecChannel.setErrStream(System.err);
        ExecChannel.setCommand(command);

        System.out.println("正在執行腳本命令");



        //接收遠程服務器執行命令的結果


        //------------------------------------------
        ExecChannel.setInputStream(null);
        ExecChannel.connect();  
        BufferedReader input = new BufferedReader(new InputStreamReader (ExecChannel.getInputStream()));
        System.out.println("遠程腳本命令是: " + command);  



        //StringBuilder message = new StringBuilder();

        InputStream in = ExecChannel.getInputStream();  

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));  

        String buf = null;

        while ((buf = reader.readLine()) != null) {
            result+= new String(buf.getBytes("gbk"),"UTF-8")+"    \r\n";  
        }  

        ExecChannel.disconnect();

        while(!ExecChannel.isClosed()){

        }
        result=ExecChannel.getExitStatus()+"";
        //Thread.sleep(5000);
        //����^_^^.^O_O^.^
        Channel channel = session.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;
        sftp.cd(directory);
        Thread.sleep(1000);
        sftp.get(downloadFile, saveFile);

    }catch (JSchException e) {
        result+=e.getMessage();
        System.out.println("shell 腳本沒有執行完畢");
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        if(ExecChannel!=null&&!ExecChannel.isClosed()){
            ExecChannel.disconnect();
        }
        if(session!=null&&session.isConnected()){
            session.disconnect();
        }
    }
    return result;
}



public static void main(String[] args) throws InterruptedException  {
    String host = "192.168.**.**";
    int port = 22;
    String user = "root";
    String pwd = "******";
    String command="tar -zcvf /home/sftp/test/12.tar.gz /home/sftp/test/1.log;sleep 20;";

    //Thread.sleep(10000);

    String directory = "/home/sftp/test/";
    String downloadFile = "/home/sftp/test/12.tar.gz";
    String saveFile = "D://JavaTest//sftp//download//";
    String exec = exec(directory, downloadFile, saveFile,host, user, pwd, port, command);
    System.out.println(exec);
    //SSHsftp.downloadFile(directory, downloadFile, saveFile, host, user, pwd, port);
    System.out.println("finished");
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章