Advertisement
Guest User

Untitled

a guest
Oct 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import paramiko
  2. import sys
  3.  
  4.  
  5. # connect to remote server
  6. def connect_to_server(server_name, user_name):
  7. client = paramiko.SSHClient()
  8. client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  9. client.load_system_host_keys()
  10. client.connect(server_name, username=user_name, password=None)
  11. return client
  12.  
  13.  
  14. # transfer file from remote host to local machine.
  15. def copy_remote_file(client, remote_file, local_file):
  16. sftp = client.open_sftp()
  17. sftp.get(remote_file, local_file)
  18.  
  19.  
  20. # run command on remote machine.
  21. def run_remote_command(client, cmd):
  22. stdin, stdout, stderr = client.exec_command(cmd)
  23. return stdin, stdout, stderr
  24.  
  25.  
  26. if __name__ == '__main__':
  27.  
  28. server = sys.argv[1]
  29. user = sys.argv[2]
  30. command = sys.argv[3]
  31. remotefile = sys.argv[4]
  32. localfile = sys.argv[5]
  33. conn = connect_to_server(server, user)
  34. std_in, std_out, std_err = run_remote_command(conn, command)
  35. for line in std_err:
  36. print(' --> ' + line)
  37.  
  38. print('copying remote file: %s\n' % remotefile)
  39. copy_remote_file(conn, remotefile, localfile)
  40.  
  41. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement