title: run remote program with root priviledge using paramiko ssh channel
body: <p>I want to remotely excute a program <code>tcp_sender</code> with root priviledge
  ,the following function is for making a ssh connection</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>then I have 3 solutions:</p>
  
  <p>solution <strong>A)</strong></p>
  
  <pre><code>    ssh = connect(hostname)
      chan = ssh.invoke_shell()
      chan.send('sudo ./tcp_sender\n')
  </code></pre>
  
  <p>with this solution, the remote <code>tcp_sender</code> is not executed, I checked using <code>ps -ef|grep "tcp_sender"</code>, there is no process</p>
  
  <p>I tried <code>chan.send('sudo ./tcp_sender &gt; log 2&gt;&amp;1\n')</code>
  and in the log, it says:</p>
  
  <pre><code>sudo: no tty present and no askpass program specified
  </code></pre>
  
  <p>solution <strong>B)</strong></p>
  
  <pre><code>    ssh = connect(hostname)
      (stdin, stdout, stderr) = ssh.exec_command("[ -f tcp_sender ]  &amp;&amp; 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}" ] &amp;&amp; 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')
  </code></pre>
  
  <p>with this solution, I just add some un-relevant lines, then the remote <code>tcp_sender</code> is running,  something like:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>however, it can't run normally(as expected).  In the <code>tcp_sender</code>, there is a <code>fork()</code>, maybe it is due to this?</p>
  
  <p>I tried <code>chan.send('sudo ./tcp_sender &gt; log 2&gt;&amp;1\n')</code>
  and in the log, it is empty. Because I have many error-checking related <code>printf</code> in my <code>tcp_sender</code> program, I think there should be <code>printf</code> results in the log, but it is empty.</p>
  
  <p>In addition, I noticed a phenomenon, if I <code>kill -9 9348</code>, all these two processes are ended.
  But for the next solution C, the process 9349 will be handed over to system <code>init</code> process 1. </p>
  
  <p>Solution <strong>C):</strong></p>
  
  <p>with this solution, I can run the remote <code>tcp_sender</code> correctly. But the python script will be blocked by the remote program until it exits. I don't want my script to wait that the remote exits.</p>
  
  <pre><code>    log = open('log','a+')
      ssh = connect(hostname)
      (stdin, stdout, stderr) = ssh.exec_command("[ -f tcp_sender ] &amp;&amp; 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}" ] &amp;&amp; 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()
  </code></pre>
  
  <p>so what are potential reasons for this phenomenon?
  can anyone give some advice? thanks!</p>