java 远程调用shell 上传文件到服务器

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.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
 * Purpose:
 *
 * @see
 * @since 1.1.0
 */
public class Scp
{
    public static void main(String[] args)
    {
        String user = "root";
        String pass = "111111";
        String host = "192.168.0.24";

        Connection con = new Connection(host);
        try
        {
            con.connect();
            boolean isAuthed = con.authenticateWithPassword(user, pass);
            System.out.println("isAuthed====" + isAuthed);

            SCPClient scpClient = con.createSCPClient();
            // scpClient.put("localFiles", "remoteDirectory"); //从本地复制文件到远程目录
            // scpClient.get("remoteFiles","localDirectory"); //从远程获取文件

            scpClient.put("F:\\lib\\ganymed-ssh2-build210\\ganymed-ssh2-build210.jar", "/usr/local/dota"); // 从本地复制文件到远程目录

            //
            // SFTPv3Client sftpClient = new SFTPv3Client(con);
            // sftpClient.mkdir("newRemoteDir", 6); //远程新建目录
            // sftpClient.rmdir(""); //远程删除目录
            //
            // sftpClient.createFile("newRemoteFile"); //远程新建文件
            // sftpClient.openFileRW("remoteFile"); //远程打开文件,可进行读写

            Session session = con.openSession();
            session.execCommand("uname -a && date && uptime && who");
            // 远程执行命令

            // 显示执行命令后的信息
            System.out.println("Here is some information about the remote host:");
            InputStream stdout = new StreamGobbler(session.getStdout());

            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

            while (true)
            {
                String line = br.readLine();
                if (line == null)
                    break;
                System.out.println(line);
            }

            /* Show exit status, if available (otherwise "null") */

            System.out.println("ExitCode: " + session.getExitStatus());

            session.close();
            con.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值