Guest User

Untitled

a guest
Oct 16th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import gamin, paramiko, os, time, ConfigParser, hashlib
  2. from twisted.internet import reactor, defer
  3. from twisted.internet.task import LoopingCall
  4. from twisted.internet.protocol import DatagramProtocol
  5. from twisted.protocols.basic import LineReceiver
  6. from twisted.internet.protocol import ClientFactory, Protocol
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. class Sync(LineReceiver):
  14. def __init__(self):
  15. config = ConfigParser.ConfigParser()
  16. config.readfp(open('openbox.config'))
  17. self.local_watch_dir = config.get("dirs", "local")
  18. self.remote_watch_dir = config.get("dirs", "remote")
  19. self.mon = gamin.WatchMonitor()
  20. self.mon.watch_directory(self.local_watch_dir, self.callback)
  21. self.remote_server = config.get("server", "address")
  22. self.username = config.get("server", "username")
  23. self.port = int(config.get("server", "port"))
  24. self.password = hashlib.sha224(config.get("server", "password")).hexdigest()
  25. self.authenticated = 0
  26.  
  27.  
  28.  
  29.  
  30.  
  31. def connectionMade(self):
  32. print "[Notice] Connection made to remote system, authenticating..."
  33. self.connected = 1
  34. self.setup_auth()
  35. gamin_loop = LoopingCall(self.check_gamin_status)
  36. gamin_loop.start(0.5)
  37.  
  38.  
  39.  
  40. def dataReceived(self, data):
  41.  
  42. is_file = os.path.basename(data.replace("\n",""))
  43.  
  44. if data.split()[-1] == "ok":
  45. self.authenticated = 1
  46. print "[Auth] Authenticated"
  47. self.setup_sftp()
  48.  
  49. if data.startswith("True"):
  50. print "[File] File %s exists on remote system" % (is_file)
  51. if os.path.isfile("%s%s%s" % (self.local_watch_dir, os.sep, is_file)) == False:
  52. self.send_delete(2, is_file)
  53.  
  54. if data.startswith("False"):
  55. print "[File] File %s does not exists on remote system" % (is_file)
  56. if os.path.isfile("%s%s%s" % (self.local_watch_dir, os.sep, is_file)) == True:
  57. print "[File] Sending..."
  58. self.send_file(0, is_file)
  59.  
  60.  
  61. def setup_auth(self):
  62. print "[Auth] Setting up auth..."
  63. privatekeyfile = os.path.expanduser("~/.ssh/id_rsa")
  64. self.known_hosts = os.path.expanduser("~/.ssh/known_hosts")
  65. self.ssh_key = paramiko.RSAKey.from_private_key_file(privatekeyfile)
  66. self.sendLine("auth %s" % (self.password))
  67. return True
  68.  
  69. def setup_sftp(self):
  70. print "[Auth] Setting up SFTP..."
  71. transport = paramiko.Transport((self.remote_server, self.port))
  72. transport.connect(username = self.username, pkey = self.ssh_key)
  73. self.sftp = paramiko.SFTPClient.from_transport(transport)
  74. print "[Auth] SFTP setup..."
  75. return True
  76.  
  77.  
  78. def check_remote_file(self, path):
  79. self.sendLine("exists %s%s%s" % (self.remote_watch_dir, os.sep, path))
  80.  
  81. def send_delete(self, event, path):
  82. if event == 2:
  83. print "[File] Deleting %s" % (path)
  84. self.sendLine("delete %s%s%s" % (self.remote_watch_dir, os.sep, path))
  85.  
  86.  
  87.  
  88. def send_notice(self, event, path):
  89. if self.connected == 1:
  90.  
  91. if event == 8:
  92. self.check_remote_file(path)
  93.  
  94.  
  95.  
  96. def send_file(self, event, path):
  97. self.sftp.put("%s%s%s" % (self.local_watch_dir, os.sep, path), "%s%s%s" % (self.remote_watch_dir, os.sep, path))
  98. self.check_remote_file(path)
  99.  
  100.  
  101.  
  102. def callback(self, path, event):
  103. path = os.path.basename(path)
  104.  
  105. if path == os.path.basename(self.local_watch_dir):
  106. return
  107.  
  108.  
  109.  
  110.  
  111. if event == 1:
  112. #print "%s was changed " % (path)
  113. self.send_file(event, path)
  114.  
  115. elif event == 2:
  116. #print "%s was deleted " % (path)
  117. self.send_delete(event, path)
  118.  
  119. elif event == 3:
  120. print "%s executing " % (path)
  121. self.send_notice(event, path)
  122.  
  123. elif event == 4:
  124. print "%s stopped executing " % (path)
  125. self.send_notice(event, path)
  126.  
  127. elif event == 5:
  128. #print "%s was created " % (path)
  129. self.send_file(event, path)
  130.  
  131. elif event == 6:
  132. print "%s was moved " % (path)
  133. self.send_notice(event, path)
  134.  
  135. elif event == 7:
  136. print "%s acknowledged" % (path)
  137. self.send_notice(event, path)
  138.  
  139. elif event == 8:
  140. #print "%s exists " % (path)
  141. self.send_notice(event, path)
  142.  
  143. elif event == 9:
  144. #print "%s end exits " % (path)
  145. self.send_notice(event, path)
  146.  
  147.  
  148. def check_gamin_status(self):
  149. self.mon.handle_events()
  150.  
  151.  
  152.  
  153.  
  154. class ServerFactory(ClientFactory):
  155. protocol = Sync
  156.  
  157. def clientConnectionFailed(self, connector, reason):
  158. print 'connection failed:', reason.getErrorMessage()
  159. reactor.stop()
  160.  
  161. def clientConnectionLost(self, connector, reason):
  162. print 'connection lost:', reason.getErrorMessage()
  163. reactor.stop()
  164.  
  165. factory = ServerFactory()
  166. reactor.connectTCP(Sync().remote_server, 9990, factory)
  167. reactor.run()
Add Comment
Please, Sign In to add comment