java远程调用 shell_Java实践 — SSH远程执行Shell脚本

SSHCommandExecutor.java:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.Vector;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

/**

* This class provide interface to execute command on remote Linux.

*/

public class SSHCommandExecutor {

private String ipAddress;

private String username;

private String password;

public static final int DEFAULT_SSH_PORT = 22;

private Vector stdout;

public SSHCommandExecutor(final String ipAddress, final String username, final String password) {

this.ipAddress = ipAddress;

this.username = username;

this.password = password;

stdout = new Vector();

}

public int execute(final String command) {

int returnCode = 0;

JSch jsch = new JSch();

MyUserInfo userInfo = new MyUserInfo();

try {

// Create and connect session.

Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);

session.setPassword(password);

session.setUserInfo(userInfo);

session.connect();

// Create and connect channel.

Channel channel = session.openChannel("exec");

((ChannelExec) channel).setCommand(command);

channel.setInputStream(null);

BufferedReader input = new BufferedReader(new InputStreamReader(channel

.getInputStream()));

channel.connect();

System.out.println("The remote command is: " + command);

// Get the output of remote command.

String line;

while ((line = input.readLine()) != null) {

stdout.add(line);

}

input.close();

// Get the return code only after the channel is closed.

if (channel.isClosed()) {

returnCode = channel.getExitStatus();

}

// Disconnect the channel and session.

channel.disconnect();

session.disconnect();

} catch (JSchException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return returnCode;

}

public Vector getStandardOutput() {

return stdout;

}

public static void main(final String [] args) {

SSHCommandExecutor sshExecutor = new SSHCommandExecutor("xx.xx.xx.xx", "username", "password");

sshExecutor.execute("uname -s -r -v");

Vector stdout = sshExecutor.getStandardOutput();

for (String str : stdout) {

System.out.println(str);

}

}

}

getSession()只是创建一个session,需要设置必要的认证信息之后,调用connect()才能建立连接。

调用openChannel(String type) 可以在session上打开指定类型的channel。该channel只是被初始化,使用前需要先调用connect()进行连接。

Channel的类型可以为如下类型:

shell - ChannelShell

exec - ChannelExec

direct-tcpip - ChannelDirectTCPIP

sftp - ChannelSftp

subsystem - ChannelSubsystem

其中,ChannelShell和ChannelExec比较类似,都可以作为执行Shell脚本的Channel类型。它们有一个比较重要的区别:ChannelShell可以看作是执行一个交互式的Shell,而ChannelExec是执行一个Shell脚本。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值