`
落叶换新叶
  • 浏览: 11119 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

ethz.ssh2 本地操作linux服务器

阅读更多

?


1.调用代码

import ch.ethz.ssh2.*;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class RemotShellTool {
    public static void main(String[] args) throws Exception {
        // 上传文件
        RemotShellTool.updateFile();
        // 修改文件权限
        RemotShellTool.chmodFile();
    }
    // 执行命令
    public static void execCommond() throws Exception {
        List<String> result=new ArrayList<String>();
        Connection connection = new Connection("ip");
        connection.connect();//连接
        connection.authenticateWithPassword("username","password");//认证
        Session session=connection.openSession();
        session.execCommand("cd /home/loginuser && sh test.sh");
        InputStream is = new StreamGobbler(session.getStdout());//获得标准输出流
        BufferedReader brs = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String temp = "";
        while ((temp=brs.readLine()) != null) {
            String[] a = temp.split("\\s+");
            //if(a.length > 7 ){
            result.add(a[a.length-1]);
            // }
        }

        System.out.println(result);
        if (session != null) {
            session.close();
        }
        session.close();
        brs.close();
    }
    // 修改文件权限
    public static void chmodFile() throws Exception {
        String fileName = "taiping-jinfu-task-0.0.1-SNAPSHOT.jar";
        Connection connection = new Connection("ip");
        connection.connect();//连接
        connection.authenticateWithPassword("username","password");//认证
        Session session=connection.openSession();
        session.execCommand("chmod 664 /home/loginuser/"+fileName);

        if (session != null) {
            session.close();
        }
        session.close();
    }

    public static void updateFile() throws Exception{
        Connection connection = new Connection("ip");
        connection.connect();//连接
        connection.authenticateWithPassword("username","password");//认证

        SCPClient scpClient = connection.createSCPClient();
        String localFilePath = "D:\\tpdata\\upload\\taiping-jinfu-task-0.0.1-SNAPSHOT.jar";
        File localFile = new File(localFilePath);

        String remoteFilePath = "/home/loginuser/";
        SCPOutputStream os= scpClient.put(localFile.getName(),localFile.length(),remoteFilePath,"0222");

        byte[] b = new byte[4096];
        FileInputStream fis = new FileInputStream(localFile);
        int i;
        while ((i = fis.read(b)) != -1) {
            os.write(b, 0, i);
        }
        os.flush();
        fis.close();
        os.close();

    }
}

??

2.引用maven,版本使用262的,使用较低的版本,api会不一样

?

?

<dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
</dependency>

?

?

?

?

?

?

2
0
分享到:
评论
Global site tag (gtag.js) - Google Analytics 重庆时时彩怎么作弊的