簡體   English   中英

使用 JSch 的多個命令

[英]Multiple commands using JSch

我的要求如下:
我必須使用我的憑據登錄到 Unix box,一旦登錄,我必須對不同的用戶執行 sudo。 sudo 成功后,我必須在 nohup 中調用 shell。 執行完成后,關閉通道和會話。

我嘗試了使用 sudo 命令連接的第一步,但我不知道如何在 sudo 命令之后調用 shell 腳本。

在下面的代碼中,我能夠執行 sudo 命令,但是在獲得 sudo 訪問權限后,如何使用用戶masteruser在 nohup 中執行 shell。 因此,我的 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();
    }

}

要按順序執行多個命令,您可以創建如下命令字符串:

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

執行它並將這個字符串傳遞給上面的方法。

這篇文章可能很舊,但我找到了另一種簡單的方法,可以讓您分別檢索每個命令的輸出。 請注意,此代碼必須在會話打開后執行,如示例 ( 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();
}

其中printOutput使用channel.getInputStream()來讀取命令的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM