繁体   English   中英

如何使用并行 ssh 执行“su”命令

[英]How to execute 'su' command using parallel-ssh

我想使用并行 ssh登录到两个主机并执行su命令。 然后我想通过打印whoami来确认我是 root 用户

代码:

hosts = ['myHost1', 'myHost2']
client = ParallelSSHClient(hosts, user='myUser', password='myPassword')

output = client.run_command('su')

for host in output:
    stdin = output[host].stdin
    stdin.write('rootPassword\n')
    stdin.flush()

client.join(output)

output = client.run_command('whoami')

for host, host_output in output.items():
    for line in host_output.stdout:
        print("Host [%s] - %s" % (host, line))

结果:

Host [myHost1] - myUser
Host [myHost2] - myUser

显然,我希望在输出中使用 root。 我正在关注文档。

我试过使用所有不同的行结尾而不是\\n并且没有任何改变。 如何使用parallel-ssh执行su命令?

尝试这个:

**def exec_command(hosts):
    strr = ""
    client = ParallelSSHClient(hosts, user='admin', password='admin_password')
    cmd = 'echo root_password |su -c "commmand" root'
    output = client.run_command(cmd)
    client.join()
    for host_out in output:
    for line in host_out.stdout:
        strr+=line+" "
    return strr
    **

'echo root_password |su -c "command" root'

尝试将sudo=True放在run_command

output = client.run_command(<..>, sudo=True)

就像在文档中一样

事实证明,我正在尝试做的事情是无法实现的。

第一个问题

我在这篇文章中发现所有命令都在他们自己的频道中。 这意味着即使su成功,它也不会影响第二个命令。 帖子作者推荐运行

su -c whoami - root

第二个问题

我设法通过改变甚至进一步调试问题host_output.stdouthost_output.stderr原来,我接收这在以前是不被所述终端上显示一个错误:

standard in must be a tty

此问题的可能解决方案在这里 它们对我不起作用,但可能对你有用。

对我来说,解决方法是允许我所有的主机 root 登录。 然后在parallel-ssh 中,我以root 身份登录并拥有所有权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM