linux - run remote program with root priviledge using paramiko ssh channel -


i want remotely excute program tcp_sender root priviledge ,the following function making ssh connection

    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 

then have 3 solutions:

solution a)

    ssh = connect(hostname)     chan = ssh.invoke_shell()     chan.send('sudo ./tcp_sender\n') 

with solution, remote tcp_sender not executed, checked using ps -ef|grep "tcp_sender", there no process

i tried chan.send('sudo ./tcp_sender > log 2>&1\n') , in log, says:

sudo: no tty present , no askpass program specified 

solution 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 tcp_sender runnning, kill             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') 

with solution, add un-relevant lines, remote tcp_sender running, like:

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 

however, can't run normally(as expected). in tcp_sender, there fork(), maybe due this?

i tried chan.send('sudo ./tcp_sender > log 2>&1\n') , in log, empty. because have many error-checking related printf in tcp_sender program, think there should printf results in log, empty.

in addition, noticed phenomenon, if kill -9 9348, these 2 processes ended. next solution c, process 9349 handed on system init process 1.

solution c):

with solution, can run remote tcp_sender correctly. python script blocked remote program until exits. don't want script wait remote exits.

    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) elem in buf)+'\n\n')     log.close() 

so potential reasons phenomenon? can give advice? thanks!

you're mixing things should keep separate.

first, write script on remote side usr (= username give paramiko) can execute , can correctly start tcp_sender using sudo without asking password, etc.

in script, start sudo background process using nohup:

nohup sudo ./tcp_sender 

nohup makes sure new child process detached stays alive when connection lost/cut.

when script works, start new script using ssh.exec_command('script')

reasoning: it's possible want using shell , clever python code drives shell if typing commands. brittle, hard test - it's variant of god object.

instead, split problem small, distinct problems can develop , test independently. have 3 problems solve:

  1. tcp_sender itself.
  2. starting tcp_sender
  3. starting remotely

so use 3 distinct tools solve them.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -