当我ssh到远程服务器然后grep时,不会填充输出文件

时间:2017-07-05 09:38:10

标签: python ssh grep

我使用的代码如下:

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

我注意到正在创建远程文件,但它没有填充。我使用的cmd arg是这样的:

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

我已经从这个问题中取得了这个功能(我已经尝试了paramiko和spur包装器,我遇到了同样的问题):Sending a password over SSH or SCP with subprocess.Popen

感谢您的帮助

0 个答案:

没有答案