Java 使用JSch的多个命令

Java 使用JSch的多个命令,java,unix,ssh,jsch,channel,Java,Unix,Ssh,Jsch,Channel,我的要求如下: 我必须使用我的凭据登录到Unix box,一旦登录,我必须对不同的用户执行sudo。一旦sudo成功,我就必须在nohup中调用shell。执行完成后,关闭通道和会话 我尝试了第一步,即使用sudo命令连接,但我不知道在sudo命令之后如何调用shell脚本 在下面的代码中,我可以执行sudo命令,但是在获得sudo访问权限之后,如何使用usermasteruser在nohup中执行shell呢。因此,my shell创建的所需文件的所有者为masteruser public c

我的要求如下:
我必须使用我的凭据登录到Unix box,一旦登录,我必须对不同的用户执行sudo。一旦sudo成功,我就必须在nohup中调用shell。执行完成后,关闭通道和会话

我尝试了第一步,即使用sudo命令连接,但我不知道在sudo命令之后如何调用shell脚本

在下面的代码中,我可以执行sudo命令,但是在获得sudo访问权限之后,如何使用user
masteruser
在nohup中执行shell呢。因此,my shell创建的所需文件的所有者为
masteruser

public class SSHUploader {

    Session session = null;

    public SSHUploader(){

    }

    public void connect(){
    try {

            JSch jsch = new JSch();
            session = jsch.getSession("user", "xxx.xxx.xx.xx", 22);
            session.setPassword("test");
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void executeCommand(String script) throws JSchException, IOException{
        System.out.println("Execute sudo");
        String sudo_pass = "test";
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        ((ChannelExec) channel).setCommand( script);

        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);

        channel.connect();
        out.write((sudo_pass + "\n").getBytes());
        out.flush();

        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0)
                    break;
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                System.out.println(ee);
            }
        }
        channel.disconnect();
        System.out.println("Sudo disconnect");
    }

    public void disconnect(){
        session.disconnect();
    }


    public static void main(String... args) throws JSchException, IOException {

        SSHUploader up = new SSHUploader();
        up.connect();

        up.executeCommand("sudo -u masteruser bash");

        up.disconnect();
    }

}
公共类SSHUploader{
会话=空;
公共SSHUploader(){
}
公共void connect(){
试一试{
JSch JSch=新的JSch();
session=jsch.getSession(“用户”,“xxx.xxx.xx.xx”,22);
会话设置密码(“测试”);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
session.setConfig(config);
session.connect();
}捕获(例外情况除外){
例如printStackTrace();
}
}
public void executeCommand(字符串脚本)抛出JSCHEException、IOException{
System.out.println(“执行sudo”);
字符串sudo_pass=“test”;
ChannelExec channel=(ChannelExec)session.openChannel(“exec”);
((ChannelExec)channel.setCommand(脚本);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel.setErrStream(System.err);
channel.connect();
out.write((sudo_pass+“\n”).getBytes();
out.flush();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
if(i<0)
打破
系统输出打印(新字符串(tmp,0,i));
}
if(channel.isClosed()){
System.out.println(“退出状态:+channel.getExitStatus());
打破
}
试一试{
睡眠(1000);
}捕获(异常ee){
系统输出打印(ee);
}
}
通道断开();
System.out.println(“Sudo disconnect”);
}
公共空间断开连接(){
session.disconnect();
}
公共静态void main(字符串…args)抛出JSCHEException、IOException{
SSHUploader up=新的SSHUploader();
up.connect();
up.executeCommand(“sudo-u主用户bash”);
up.disconnect();
}
}

要按顺序执行多个命令,可以创建如下命令字符串:

String script ="pbrun su - user; cd /home/scripts;./sample_script.sh”

执行它并将此字符串传递给上面的方法

这篇文章可能很旧,但我找到了另一种简单的方法,可以让您分别检索每个命令的输出。请注意,此代码必须在会话打开后执行,如示例()所示:


其中,
printOutput
使用
channel.getInputStream()
读取命令的结果。

此外,添加“echo Running script_X;/script_X”似乎很适合用于多个命令。请注意,这是*nix shell特定的解决方案-它既不是SSH也不是JSch功能-在其他系统(如Windows)上也是如此,可能需要使用不同的语法+1我对这种类型的实施存在问题。相反,我创建了2个通道,而不是1个(这里我有2个命令要运行)可能的重复通道
for (String command : commands) {
    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    channel.setInputStream(null);
    channel.setErrStream(System.err);
    channel.setCommand(command);
    channel.connect();
    printOutput(channel);
    channel.disconnect();
}