linux - Jsch 和 sudo 命令

标签 linux groovy sudo jsch

我正在尝试自动执行一些操作,其中一项操作是切换到远程 Linux 机器上的技术用户。该过程如下所示:使用“普通”用户登录,然后切换为

sudo /bin/rootsh -i -u techUser

to the technical user.

Here's my Groovy code example that I am working on:

import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session
import com.jcraft.jsch.Channel
import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.JSchException

class Main {
    static void main(String[] args) {
        int responseCode = 0
        String responseText = ""
        def targetHost = "targetHost"
        def targetUser = "targetUser"
        def technicalUser= "technicalUser"
        def targetPass = "targetPass"
        def targetPort = 22
        Properties configConnection = new Properties()
        configConnection.put("StrictHostKeyChecking", "no")
        configConnection.put("PreferredAuthentications", "publickey,keyboard-interactive,password")
        JSch jsch = new JSch()
        try {
            Session targetSession = jsch.getSession(targetUser, targetHost, targetPort)
            targetSession.setPassword(targetPass)
            targetSession.setConfig(configConnection)
            targetSession.connect()
            Channel channel = targetSession.openChannel("exec")
            ((ChannelExec) channel).setCommand("echo 'targetPass' | sudo -S -p /bin/rootsh -i -u technicalUser")
            ((ChannelExec) channel).setPty(true)
            final ByteArrayOutputStream baos = new ByteArrayOutputStream()
            ((ChannelExec) channel).setErrStream(baos)
            channel.setInputStream(null)
            InputStream is = channel.getInputStream()
            channel.connect()
            byte[] tmp = new byte[1024]
            while (true) {
                while (is.available() > 0) {
                    int i = is.read(tmp, 0, 1024)
                    if (i < 0)
                        break
                    responseText = new String(tmp, 0, i)
                }
                if (channel.isClosed()) {
                    responseText = new String(baos.toByteArray())
                    responseCode = channel.getExitStatus()
                    break
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                    println("[ERROR] " + ee.getMessage())
                }
            }
            channel.disconnect()
            targetSession.disconnect()
            println("RESULT:  code: " + responseCode + ", text: \"" + responseText + "\"")
        } catch (JSchException e) {
            println("[ERROR] Exception, problem with connection: " + e.getMessage())
        }
    }
}

结果是:

RESULT:  code: 1, text: ""

当我设置为

((ChannelExec) channel).setPty(false)

结果是:

RESULT:  code: 1, text: "/bin/rootshSorry, user targetUser is not allowed to execute '/bin/bash' as technicalUser on targetHost."

当我从以下行中删除密码时:

((ChannelExec) channel).setCommand("echo '' | sudo -S -p /bin/rootsh -i -u technichalUser")

结果是:

RESULT:  code: 1, text: "/bin/rootsh/bin/rootsh
Sorry, try again.
/bin/rootsh
/bin/rootsh
sudo: pam_authenticate: Authentication information cannot be recovered"

当我设置以下命令时:

((ChannelExec) channel).setCommand("sudo -S -p /bin/rootsh -i -u technichalUser")

进程一直在运行,完全没有响应(进程可能在等待密码)

如果有人已经解决了这样的问题或类似的问题,我真的很感激任何帮助。

最佳答案

您不能使用输入重定向将密码传递给 sudo,至少不能使用默认配置。

因此,这行不通:

echo 'targetPass' | sudo -S -p /bin/rootsh -i -u technicalUser` 

您甚至在交互式终端中尝试过吗?我认为它在那里也行不通。


您必须将密码写入 channel 输入流(在 JSch 中称为“输出流”)。

看官方JSch Sudo example .

您可能需要启用 TTY/PTY。参见 Use JSch sudo example and Channel.setPty for running sudo command on remote host .

关于linux - Jsch 和 sudo 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467747/

相关文章:

javascript - ${__RandomString(10,195165984,)} 放在 Jmeter 外部的脚本文件中然后调用它时不起作用

eclipse - 如何在 Eclipse 中从 ant view 执行 "sudo ant"

linux期望sudo脚本将命令和参数视为命令

python - 在我的 Mac 上以 root 身份运行 pip 时获取 "Permission Denied"

c - 让群发邮件应用程序准确跟踪其进度的非常有效的方法是什么?

linux - 在 Bash 脚本 Linux 中使用带有 for 循环的 case 语句

java - 如何在docker上安装java到ubuntu?

linux - 如何在awk中使用循环

sql - 使用 Groovy 批处理异构 SQL 准备语句?

groovy - 如何使用 groovy/gradle 运行 jetty 7+ 并指定 war ?