我要远程EXCUTE程序tcp_sender
与根特权 运行远程程序与根特权,下面的函数是用于制造ssh连接使用的paramiko SSH通道
def connect(hostname):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username='usr', pkey=paramiko.RSAKey.from_private_key(open('id_rsa'), 'psw'), timeout = 240.0)
return ssh
然后我有3个解决方案:
溶液A)
ssh = connect(hostname)
chan = ssh.invoke_shell()
chan.send('sudo ./tcp_sender\n')
用此溶液,再微尘tcp_sender
不执行,我检查使用ps -ef|grep "tcp_sender"
,没有进程
我试图chan.send('sudo ./tcp_sender > log 2>&1\n')
并在日志中,它说:
sudo: no tty present and no askpass program specified
解决方案B)
ssh = connect(hostname)
(stdin, stdout, stderr) = ssh.exec_command("[ -f tcp_sender ] && echo 1 || echo 0")
res = stdout.readlines()
print hostname,res[0]
if res[0] == '0\n':
UnusedHostFile.write(hostname+'no tcp_sender exists\n')
else:
chan = ssh.invoke_shell()
chan.send("sudo chmod 777 tcp_sender\n")
# if a tcp_sender is runnning, kill it
chan.send('x=`ps -ef|grep "tcp_sender"|grep -v "grep"|awk \'{print $2}\'`; [ -n "${x}" ] && sudo kill -9 $x\n')
time.sleep(4)
while not chan.recv_ready():
time.sleep(1)
buf = ''
buf +=chan.recv(9999)
print buf
chan.send('sudo ./tcp_sender\n')
使用这种解决方案,我只需添加一些不相关的线路,然后运行远程tcp_sender
,如下所示:
bash-4.0# ps -ef|grep "sender"
root 9348 9325 0 Apr07 ? 00:00:00 sudo ./tcp_sender
root 9349 9348 0 Apr07 ? 00:00:00 ./tcp_sender
但是,它无法正常运行(如预期)。在tcp_sender
,有一个fork()
,也许是由于这个?我试过chan.send('sudo ./tcp_sender > log 2>&1\n')
并且在日志中,它是空的。因为我在我的tcp_sender
程序中有很多与错误检查相关的printf
,我认为日志中应该有printf
结果,但它是空的。
另外,我注意到一个现象,如果我kill -9 9348
,所有这两个过程都结束了。 但是对于接下来的溶液C,过程9349将被移交到系统init
过程1.
解C):
用此溶液,我可以正确运行远程tcp_sender
。但是Python脚本将被远程程序阻止,直到它退出。我不希望我的脚本等待远程退出。
log = open('log','a+')
ssh = connect(hostname)
(stdin, stdout, stderr) = ssh.exec_command("[ -f tcp_sender ] && echo 1 || echo 0")
res = stdout.readlines()
print hostname,res[0]
if res[0] == '0\n':
UnusedHostFile.write(hostname+"tcp_sender doesn't exists\n")
else:
chan = ssh.invoke_shell()
chan.send("sudo chmod 777 tcp_sender\n")
chan.send('x=`ps -ef|grep "tcp_sender"|grep -v "grep"|awk \'{print $2}\'`; [ -n "${x}" ] && sudo kill -9 $x\n')
time.sleep(4)
while not chan.recv_ready():
time.sleep(1)
buf = ''
buf +=chan.recv(9999)
print buf
chan.send('sudo ./tcp_sender\n')
#chan.send('sudo whoami\n')
time.sleep(2)
(stdin, stdout, stderr) = ssh.exec_command("ps -ef|grep 'tcp_sender'|grep -v 'grep'|wc -l")
res = stdout.readlines()
while res[0].strip() != '0':
time.sleep(3)
(stdin, stdout, stderr) = ssh.exec_command("ps -ef|grep 'tcp_sender'|grep -v 'grep'|wc -l")
res = stdout.readlines()
print res[0].strip()
while not chan.recv_ready():
time.slepp(1)
buf = ''
buf += chan.recv(9999)
log.write(hostname+': '+''.join(str(elem) for elem in buf)+'\n\n')
log.close()
那么这种现象的潜在原因是什么? 任何人都可以给一些建议?谢谢!
谢谢你,我遵循你的程序,但'raise_on_stderr(ssh.exec_command(“bash tcp_exec.sh”))',我得到的经典错误'异常:sudo:没有tty存在并且没有指定askpass程序' – misteryes
正如我所说:你必须配置你的系统,使得'sudo'不需要输入密码。我的猜测是,对于你的测试,'sudo'在本地缓存密码。 /www.cyberciti.biz/tips/allow-a-normal-user-to-run-commands-as-root.html –
它不需要'sudo'的密码,问题是'tty'。 – misteryes