我想在远程计算机上使用python-paramiko(在linux2上使用Python 2.7.9)执行sudo命令。 下面是代码。当我执行的代码它每次给出不同的输出,而其工作正常,当我运行在python相同的代码>>> CMDLINE在需要使用密码输入Sudo的远程服务器上执行命令 - Paramiko
import paramiko
import sys
import time
def send_string_and_wait(command, wait_time, should_print):
shell.send(command)
time.sleep(wait_time)
receive_buffer = shell.recv(1024)
if should_print:
return receive_buffer
dbname='test'
cl='testdb'
host='testhost'
owner='uname'
passwd='p'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(str(host), username=str(owner), password=str(passwd), port=22)
shell = client.invoke_shell()
send_string_and_wait("sudo su - oracle\n", 1, True)
send_string_and_wait(str(passwd) + "\n", 1, True)
a=send_string_and_wait("sh Validation_Final.sh" + str(' ') + str(dbname) + str(' ') + str(cl) + "\n", 0, True)
print a
client.close()
任何意见和建议,将不胜感激,谢谢您!
感谢您的回复,我尝试过使用sudo -k,但没有运气。有趣的是,当我硬编码dbname和像[[“”“send_string_and_wait('sh /orashare/ettool/Validation_Final.sh dbname client'+”\ n“,1,True)['”“] – Prince
等客户端值时, m“在传递变量时遇到问题”“send_string_and_wait(”sh /orashare/ettool/Validation_Final.sh“+”“+ str(dbname)+”“+ str(cl)+”\ n“,0,True)” “”我猜这肯定是语法问题,如果我错了,请纠正我。 – Prince
试着看看你从远程服务器上得到了什么,使用'-k'只能确保远程服务器的行为一致。但是,你必须记住,当要求输入密码时,它不使用shell符号(比如''''或'$'),因此如果paramiko只等待其中一个,它就会被阻塞。 'Password:'(或者它出现在你的系统中的任何东西)被认为是该呼叫的shell标记。 – Mike