【问题标题】:Java Jsch sudo as user and execute a script [duplicate]Java Jsch sudo作为用户并执行脚本[重复]
【发布时间】:2018-02-05 16:21:22
【问题描述】:

我使用 jsch 登录到远程主机,以不同的用户身份执行脚本。 必须使用“exec”频道。 我当前的 Unix 操作是: 1)须藤苏 - 2) 运行脚本

我怎样才能运行这个命令“sudo su -”然后在同一个频道中执行脚本


更新了代码 cmets,我正在尝试发送以下两个命令作为输入。它正在循环运行,我没有看到它正在执行。下面的 tw 是在数组列表“命令”中发送的输入 sudo su - testusr /home/testusr/start.sh

ChannelShell channel = null;

List<String> result = new ArrayList<String>();
InputStream inStream = null;
OutputStream outStream = null;

PipedOutputStream pOutStream = null;
PipedInputStream pInStream = null;
try {
    inStream = new PipedInputStream();
    pOutStream = new PipedOutputStream((PipedInputStream) inStream);

    outStream = new PipedOutputStream();
    pInStream = new PipedInputStream((PipedOutputStream) outStream);
    channel = (ChannelShell) session.openChannel("shell");
    // channel.setPty(true);
    channel.setInputStream(inStream);
    channel.setOutputStream(outStream);
    channel.connect();
    BufferedReader bfs = null;

    for (String command : commands) {
        LOGGER.info("Executing command {} ", command);
        pOutStream.write((command.concat("\n")).getBytes());
    }
    LOGGER.info(" exit status {}", channel.getExitStatus());
    bfs = new BufferedReader(new InputStreamReader((inStream)));
    if (channel.getExitStatus() != 0) {

        result.add("ERROR");
    }
    String line;
    byte[] bt = new byte[1024];
    while (true) {
        while (inStream.available() > 0) {
            int i = inStream.read(bt, 0, 1024);
            if (i < 0) {
                break;
            }
            LOGGER.info("result {}", new String(bt, 0, i));
        }
        if (channel.isClosed()) {
            LOGGER.info("exit status {}", channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

二次编辑

for (String command : commands) {
                OutputStream out = channel.getOutputStream();
                out.write((command.concat("\n")).getBytes());
                out.flush();
                InputStream in = channel.getInputStream();
                byte[] tmp = new byte[1024];
                while (true) {
                    while (in.available() > 0) {
                        int i = in.read(tmp, 0, 1024);
                        if (i < 0) {
                            break;
                        }
                        LOGGER.info("Output stream execution {}", new String(
                                tmp, 0, i));
                    }
                    if (channel.isClosed()) {
                        LOGGER.info("Executing exit status {}",
                                channel.getExitStatus());
                        break;
                    }
                }
            }

【问题讨论】:

  • 按照这个建议。 ((ChannelExec)channel).setCommand("sudo -S -p '' "+command);我如何以用户身份运行,在会话中我将作为登录用户,现在我已经切换 sudo su -star
  • 是的,在使用 jsch 执行之前在 putty 终端中尝试了相同的操作,但它会在 putty 终端中提示输入密码,因此使用 jsch 会出现相同的行为。 "sudo su -c /home/testusr/rn.sh -testusr
  • su - testusr。这会提示输入密码。这些尝试来自腻子终端,我没有使用 jsch 进行测试。在腻子终端中,如果我执行 sudo su-testusr。它不提示输入密码
  • 是的,你在写。
  • 好的,所以使用stackoverflow.com/a/41674203/850848中建议的第二种方法

标签: java unix sudo jsch


【解决方案1】:

你真的需要sudo su -吗? 如果你的脚本是 bash 脚本,你可以调用sudo bash your_script.sh

这应该适用于更改解释器的任何脚本类型。

su -恢复用户环境,如果你真的需要,你的脚本可以源~/.bash_profile为例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2018-01-16
    • 1970-01-01
    • 2011-01-29
    • 2016-04-06
    • 2014-05-30
    • 2011-10-01
    相关资源
    最近更新 更多