2

The code that I use is the following:

def ssh(host, cmd, user, password, timeout=30, bg_run=False):                                                                                                 
  """SSH'es to a host using the supplied credentials and executes a command.                                                                                                 
  Throws an exception if the command doesn't return 0.                                                                                                                       
  bgrun: run command in the background"""                                                                                                                                    

  fname = tempfile.mktemp()                                                                                                                                                  
  fout = open(fname, 'w')                                                                                                                                                    

  options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'                                                                         
  if bg_run:                                                                                                                                                         
    options += ' -f'                                                                                                                                                       
  ssh_cmd = 'ssh %s@%s %s "%s"' % (user, host, options, cmd)                                                                                                                 
  child = pexpect.spawn(ssh_cmd, timeout=timeout)                                                                                                                            
  child.expect(['password: '])                                                                                                                                                                                                                                                                                               
  child.sendline(password)                                                                                                                                                   
  child.logfile = fout                                                                                                                                                       
  child.expect(pexpect.EOF)                                                                                                                                                  
  child.close()                                                                                                                                                              
  fout.close()                                                                                                                                                               

  fin = open(fname, 'r')                                                                                                                                                     
  stdout = fin.read()                                                                                                                                                        
  fin.close()                                                                                                                                                                

  if 0 != child.exitstatus:                                                                                                                                                  
    raise Exception(stdout)                                                                                                                                                

  return stdout

I have noticed that the file on the remote is being created, but it is not populated. The cmd arg I use is something like this:

cmd = 'cd toTheLocation; /bin/bash ack "Pattern" file.txt > output.csv'

I have taken that function from this question (I already tried paramiko and the spur wrapper and I have the same problem): Sending a password over SSH or SCP with subprocess.Popen

Thanks for the help

Naz
  • 2,012
  • 1
  • 21
  • 45
  • If you're going to post Python code, you need to reproduce the indentation accurately. Badly indented Python code is nonsense. – khelwood Jul 05 '17 at 09:42
  • I posted the question from my mobile app because I cannot log in on the computer because I do not remember my password.... just tab the body of the function if you want to help. If not just pass along... I will edit the question once I get back home – Naz Jul 05 '17 at 09:46

0 Answers0