gpt4 book ai didi

linux - Jsch 和 sudo 命令

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:25 24 4
gpt4 key购买 nike

我正在尝试自动执行一些操作,其中一项操作是切换到远程 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com