简体   繁体   English

使用 JSch 的多个命令

[英]Multiple commands using JSch

My requirement is as follow:我的要求如下:
I have to login to Unix box using my credentials and once login, I have to do sudo to different user.我必须使用我的凭据登录到 Unix box,一旦登录,我必须对不同的用户执行 sudo。 Once sudo is successful, I have to invoke shell in nohup. sudo 成功后,我必须在 nohup 中调用 shell。 On completion of executions, close channel and session both.执行完成后,关闭通道和会话。

I tried the first step which is connect using sudo command, but I don't know how to invoke shell script after the sudo command.我尝试了使用 sudo 命令连接的第一步,但我不知道如何在 sudo 命令之后调用 shell 脚本。

In the below code I am able to execute sudo command, but after getting sudo access how can I execute a shell in nohup with user masteruser .在下面的代码中,我能够执行 sudo 命令,但是在获得 sudo 访问权限后,如何使用用户masteruser在 nohup 中执行 shell。 So that required files created my shell has owner as masteruser .因此,我的 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();
    }

}

For executing multiple commands in sequence, you can create a command string like below:要按顺序执行多个命令,您可以创建如下命令字符串:

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

Execute it and pass this string to your method above.执行它并将这个字符串传递给上面的方法。

The post may be old, but I found another easy way that allows you to retrieve the output of each command separately.这篇文章可能很旧,但我找到了另一种简单的方法,可以让您分别检索每个命令的输出。 Note that this code has to be executed once the session has been opened, as shown in the examples ( http://www.jcraft.com/jsch/examples/Exec.java.html ):请注意,此代码必须在会话打开后执行,如示例 ( http://www.jcraft.com/jsch/examples/Exec.java.html ) 所示:

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();
}

Where printOutput uses channel.getInputStream() to read the result of the command.其中printOutput使用channel.getInputStream()来读取命令的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM