Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SFTP到远程机器,切换到root并在远程机器上执行命令_Python_Linux_Python 3.x_Shell_Paramiko - Fatal编程技术网

Python SFTP到远程机器,切换到root并在远程机器上执行命令

Python SFTP到远程机器,切换到root并在远程机器上执行命令,python,linux,python-3.x,shell,paramiko,Python,Linux,Python 3.x,Shell,Paramiko,我需要更新10多个远程主机上的文件。我们系统的设计方式是,我们拥有一个用户say$user,该用户使用密码say$passwd ssh进入任何远程主机,成功登录后,$user只能在远程主机上运行一个命令,即sudo su-root(这在/etc/sudoers中定义) 我的问题是,我无法从我的脚本中sudo su-root,下面是我的代码 (注意-我已经尝试了invoke_shell()和get_transport()) 我收到的输出如下 Welcome on-board ... Please

我需要更新10多个远程主机上的文件。我们系统的设计方式是,我们拥有一个用户say
$user
,该用户使用密码say$passwd ssh进入任何远程主机,成功登录后,
$user
只能在远程主机上运行一个命令,即
sudo su-root
(这在
/etc/sudoers
中定义)

我的问题是,我无法从我的脚本中
sudo su-root
,下面是我的代码

(注意-我已经尝试了
invoke_shell()
get_transport()

我收到的输出如下

Welcome on-board ...
Please choose from following
1 - Global Update
2 - Specific Update
 Please input as 1 or 2 :2
Please provide 'IPADDRESS' of Specific Remote Client :x.x.x.x
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
['$user\n']
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
**['$user\n'] #--------> I need this to be ['root\n']**
这是行不通的:

stdin,stdout,stderr=ssh\u client.exec\u命令('sudo su-root'))
stdin,stdout,stderr=ssh_client.exec_命令(“whoami”)
这基本上与以下comamnds相同:

[本地主机]#sshuser@hostsudo su-root
[本地主机]#sshuser@host哇
相反,你应该这样写:

stdin,stdout,stderr=ssh\u client.exec\u命令('sudo su-root'))
stdin.write('whoami\n')
#这里stdout.readlines()无法工作,因为它会继续读取
#直到EOF(即“sudo”comamnd已退出)。
打印(stdout.channel.recv(1024))

运行命令后,命令完成,程序继续运行。也许看看你说的不能做sudo su-root是什么意思?有什么错误吗?当从脚本执行sudo su-root时,我无法以root身份执行命令(在我的例子中是远程的)。考虑到你有一个脚本,你先做SUDO SU-root然后运行,从同一个脚本中去哇,并期望输出到WoMAMI将是root。然而,在我的例子中,whoami给了我$user而不是root,这就是问题所在。希望这能解释…@tripleee-expect解决了这个问题,我现在明白了。
Welcome on-board ...
Please choose from following
1 - Global Update
2 - Specific Update
 Please input as 1 or 2 :2
Please provide 'IPADDRESS' of Specific Remote Client :x.x.x.x
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
['$user\n']
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
**['$user\n'] #--------> I need this to be ['root\n']**