在Java中运行Linux的suu命令 – 在运行suu命令后无法运行任何其他命令

huangapple 未分类评论56阅读模式
英文:

Running linux suu command from Java - After running the suu command not able to run any other commands

问题

以下是您提供的内容的翻译部分:

在我从Java运行下面的脚本之后 -

suu -t -r "reason" <user_name>

它提示我输入密码,我输入了密码。
之后,程序正在运行,但没有执行任何进一步的命令。

我想通过Java(Jsch库)在Linux机器上模拟身份。我没有收到任何错误消息,但在输入密码后,它成功登录,然后不执行任何命令。

这是我遵循的链接以编写代码 -
https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch

在运行suu命令之后,我想读取一个文件。如果不运行suu,我将无法访问该路径。对于这个问题的任何帮助将不胜感激 在Java中运行Linux的suu命令 – 在运行suu命令后无法运行任何其他命令

这是我的代码片段-

[![enter image description here][1]][1]

提前感谢!

以下是我使用的代码 -

public class SSHConn {

    static Session session;
    static String suCmds = "suu -t -u \"reason\" simba";
    static String[] commands = {"whoami", suCmds};

    public static void main(String[] args) throws Exception {
        open();
        runCmd(commands);
        close();
    }

    public static void runCmd(String[] commands) throws JSchException, IOException {
        for (String cmd : commands) {
            System.out.println("\n执行命令: " + cmd);
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
            InputStream in = channel.getInputStream();
            OutputStream out = channel.getOutputStream();
            channel.connect();
            //仅在切换用户时传递凭据
            if (cmd.startsWith("suu -")) {
                System.out.println("现在设置suPasswd....");
                out.write((Constants.suPasswd + "\n").getBytes());
                out.flush();
                System.out.println("将suPasswd刷新到cli...");
            }
            captureCmdOutput(in, channel);
            channel.setInputStream(null);
            channel.disconnect();
        }
    }

    public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
        System.out.println("现在捕获cmdOutput...");
        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()) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }
        }
    }

    public static void open() throws JSchException {
        JSch jSch = new JSch();
        session = jSch.getSession(Constants.userId, Constants.host, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(Constants.userPasswd);
        System.out.println("连接SSH到 " + Constants.host + " - 请等待几秒钟... ");
        session.connect();
        System.out.println("已连接!\n");
    }

    public static void close() {
        session.disconnect();
        System.out.println("\n关闭通道和会话");
    }
}

希望这对您有所帮助。

英文:

After I run below script from java-

suu -t -r &quot;reason&quot; &lt;user_name&gt;

It prompted me for password and I entered it.
After that the program was running but not executing any further commands.

I want to impersonate on Linux machine through Java (Jsch library). I am not getting any errors, but after the password is given, Its successfully logging in and then no commands are being executed.

This is the link I followed to write code-
https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch

After running suu command, I want to read a file. Without running suu, I will not have access to that path. Any help for this issue will be more appreciated 在Java中运行Linux的suu命令 – 在运行suu命令后无法运行任何其他命令

Here is my snippet-
[![enter image description here][1]][1]

Thanks in advance!

Below is code I used-

public class SSHConn {

static Session session;
static String suCmds = &quot;suu -t -u \&quot;reason\&quot; simba&quot;;
static String[] commands = {&quot;whoami&quot;, suCmds};

public static void main(String[] args) throws Exception {
    open();
    runCmd(commands);
    close();
}

public static void runCmd(String[] commands) throws JSchException, IOException {
    for (String cmd : commands) {
        System.out.println(&quot;\nExecuting command: &quot; + cmd);
        Channel channel = session.openChannel(&quot;exec&quot;);
        ((ChannelExec) channel).setCommand(cmd);
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        channel.connect();
        //passing creds only when you switch user
        if (cmd.startsWith(&quot;suu -&quot;)) {
            System.out.println(&quot;Setting suPasswd now....&quot;);
            out.write((Constants.suPasswd + &quot;\n&quot;).getBytes());
            out.flush();
            System.out.println(&quot;Flushed suPasswd to cli...&quot;);
        }
        captureCmdOutput(in, channel);
        channel.setInputStream(null);
        channel.disconnect();
    }
}

public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
    System.out.println(&quot;Capturing cmdOutput now...&quot;);
    byte[] tmp = new byte[1024];
    while (true) {
        while (in.available() &gt; 0) {
            int i = in.read(tmp, 0, 1024);
            if (i &lt; 0) {
                break;
            }
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
            System.out.println(ee.getMessage());
        }
    }
}

public static void open() throws JSchException {
    JSch jSch = new JSch();
    session = jSch.getSession(Constants.userId, Constants.host, 22);
    Properties config = new Properties();
    config.put(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
    session.setConfig(config);
    session.setPassword(Constants.userPasswd);
    System.out.println(&quot;Connecting SSH to &quot; + Constants.host + &quot; - Please wait for few seconds... &quot;);
    session.connect();
    System.out.println(&quot;Connected!\n&quot;);
}

public static void close() {
    session.disconnect();
    System.out.println(&quot;\nDisconnected channel and session&quot;);
}}

huangapple
  • 本文由 发表于 2020年5月4日 15:11:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61586875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定