Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. from addkey import FILE_NAME
  2. import paramiko
  3.  
  4.  
  5.  
  6.  
  7.  
  8. hostname = '10.0.43.4' # remote hostname where SSH server is running
  9. port = 22
  10. username = 'myssh-username'
  11. password = 'myssh-password'
  12. rsa_private_key = r"/home/paramikouser/.ssh/rsa_private_key"
  13.  
  14. dir_local='/home/paramikouser/local_data'
  15. dir_remote = "remote_machine_folder/subfolder"
  16. glob_pattern='*.*'
  17.  
  18. import os
  19. import glob
  20. import paramiko
  21. import hashlib
  22.  
  23. def agent_auth(transport, username):
  24.     """
  25.    Attempt to authenticate to the given transport using any of the private
  26.    keys available from an SSH agent or from a local private RSA key file (assumes no pass phrase).
  27.    """
  28.     try:
  29.         ki = paramiko.RSAKey.from_private_key_file(rsa_private_key)
  30.     except Exception, e:
  31.         print 'Failed loading' % (rsa_private_key, e)
  32.  
  33.     agent = paramiko.Agent()
  34.     agent_keys = agent.get_keys() + (ki,)
  35.     if len(agent_keys) == 0:
  36.         return
  37.  
  38.     for key in agent_keys:
  39.         print 'Trying ssh-agent key %s' % key.get_fingerprint().encode('hex'),
  40.         try:
  41.             transport.auth_publickey(username, key)
  42.             print '... success!'
  43.             return
  44.         except paramiko.SSHException, e:
  45.             print '... failed!', e
  46.  
  47. # get host key, if we know one
  48. hostkeytype = None
  49. hostkey = None
  50. files_copied = 0
  51. try:
  52.     host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
  53. except IOError:
  54.     try:
  55.         # try ~/ssh/ too, e.g. on windows
  56.         host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
  57.     except IOError:
  58.         print '*** Unable to open host keys file'
  59.         host_keys = {}
  60.  
  61. if host_keys.has_key(hostname):
  62.     hostkeytype = host_keys[hostname].keys()[0]
  63.     hostkey = host_keys[hostname][hostkeytype]
  64.     print 'Using host key of type %s' % hostkeytype
  65.  
  66. # now, connect and use paramiko Transport to negotiate SSH2 across the connection
  67. try:
  68.     print 'Establishing SSH connection to:', hostname, port, '...'
  69.     t = paramiko.Transport((hostname, port))
  70.     t.start_client()
  71.  
  72.     agent_auth(t, username)
  73.  
  74.     if not t.is_authenticated():
  75.         print 'RSA key auth failed! Trying password login...'
  76.         t.connect(username=username, password=password, hostkey=hostkey)
  77.     else:
  78.         sftp = t.open_session()
  79.     sftp = paramiko.SFTPClient.from_transport(t)
  80.  
  81.     # dirlist on remote host
  82. #    dirlist = sftp.listdir('.')
  83. #    print "Dirlist:", dirlist
  84.  
  85.     try:
  86.         sftp.mkdir(dir_remote)
  87.     except IOError, e:
  88.         print '(assuming ', dir_remote, 'exists)', e
  89.  
  90. #    print 'created ' + dir_remote +' on the hostname'
  91.  
  92.     # BETTER: use the get() and put() methods
  93.     # for fname in os.listdir(dir_local):
  94.  
  95.     for fname in glob.glob(dir_local + os.sep + glob_pattern):
  96.         is_up_to_date = False
  97.         if fname.lower().endswith('xml'):
  98.             local_file = os.path.join(dir_local, fname)
  99.             remote_file = dir_remote + '/' + os.path.basename(fname)
  100.  
  101.             #if remote file exists
  102.             try:
  103.                 if sftp.stat(remote_file):
  104.                     local_file_data = open(local_file, "rb").read()
  105.                     remote_file_data = sftp.open(remote_file).read()
  106.                     md1 = hashlib.new(local_file_data).digest()
  107.                     md2 = hashlib.new(remote_file_data).digest()
  108.                     if md1 == md2:
  109.                         is_up_to_date = True
  110.                         print "UNCHANGED:", os.path.basename(fname)
  111.                     else:
  112.                         print "MODIFIED:", os.path.basename(fname),
  113.             except:
  114.                 print "NEW: ", os.path.basename(fname),
  115.  
  116.             if not is_up_to_date:
  117.                 print 'Copying', local_file, 'to ', remote_file
  118.                 sftp.put(local_file, remote_file)
  119.                 files_copied += 1
  120.     t.close()
  121.  
  122. except Exception, e:
  123.     print '*** Caught exception: %s: %s' % (e.__class__, e)
  124.     try:
  125.         t.close()
  126.     except:
  127.         pass
  128. print '=' * 60
  129. print 'Total files copied:',files_copied
  130. print 'All operations complete!'
  131. print '=' * 60
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement