Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import paramiko
  4. import os
  5. import time
  6.  
  7. from argparse import ArgumentParser
  8. parser = ArgumentParser()
  9. parser.add_argument(
  10.     '-t', '--timer', help='timer in minutes', type=int, default=5)
  11. args = parser.parse_args()
  12.  
  13.  
  14. class FastTransport(paramiko.Transport):
  15.  
  16.     def __init__(self, sock):
  17.         super(FastTransport, self).__init__(sock)
  18.         self.channel_events = {}
  19.         self.channels_seen = {}
  20.         self._channel_counter = 1
  21.         self.default_window_size = 65535
  22.         self.default_max_packet_size = 34816
  23.         self.use_compression()
  24.  
  25.  
  26. def download(ip, user, passwd, remote_file, local_file, port=22):
  27.     try:
  28.         print("Connecting...")
  29.         transport = FastTransport((ip, port))
  30.         transport.connect(username=user, password=passwd)
  31.         sftp = paramiko.SFTPClient.from_transport(transport)
  32.         print("Downloading...")
  33.         sftp.get(remotepath=remote_file, localpath=local_file)
  34.         sftp.close()
  35.         transport.close()
  36.         print("Download Success")
  37.     except Exception as e:
  38.         print(e)
  39.  
  40.  
  41. def remote_listdir(ip, user, passwd, port=22):
  42.     print("Retreiving remote directory listing...")
  43.     transport = FastTransport((ip, port))
  44.     transport.connect(username=user, password=passwd)
  45.     sftp = paramiko.SFTPClient.from_transport(transport)
  46.     return sftp.listdir(path='/root/masscan_output/')
  47.  
  48.  
  49. def merge_files():
  50.     print("Merging files...")
  51.     with open('output.txt', 'a') as localfile:
  52.         with open('temp_output.txt') as remotefile:
  53.             localfile.writelines('{}\n'.format(remotefile))
  54.  
  55.  
  56. def remove_duplicates():
  57.     print("Removing Duplicates")
  58.     with open('output.txt') as f:
  59.         data = set(f.read.splitlines())
  60.     with open('output.txt', 'w') as f:
  61.         f.writelines(data + '\n')
  62.  
  63.  
  64. def delete_temp_file():
  65.     os.system('rm output_backup.txt')
  66.  
  67. if __name__ == '__main__':
  68.     os.system('touch output.txt')
  69.     os.system('touch output_backup.txt')
  70.     while True:
  71.         for remote_file in remote_listdir(IP,USER,PASS):
  72.             download('IP,USER,PASS,
  73.                     remote_file=remote_file, local_file='temp_output.txt')
  74.            merge_files()
  75.            remove_duplicates()
  76.            delete_temp_file()
  77.        time.sleep(args.timer * 60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement