Advertisement
jeroenboot

netscript.py

Jan 13th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import paramiko
  3. import sys, getopt, time, re
  4.  
  5. def send_string_and_wait(command, wait_time, should_print):
  6.     shell.send(command+'\n')
  7.     response = shell.recv(9999)
  8.     time.sleep(wait_time)
  9.     if should_print:
  10.        sys.stdout.write(response)
  11.        sys.stdout.flush()
  12.  
  13. def process_comments(string, should_print):
  14.     if should_print:
  15.         print "Comments: "+string.strip()
  16.  
  17. def process_actions(string, should_print):
  18.     actions = mysplit(string)
  19.  
  20.     response = shell.recv(9999)
  21.     if should_print:
  22.        sys.stdout.write(response)
  23.        sys.stdout.flush()
  24.  
  25.     if should_print:
  26.         print "\nActions: "+string
  27.     if 'wait' in string:
  28.         shell.send('\n')
  29.     if len(actions) > 1:
  30.             wait_time = int(actions[1])
  31.         else:
  32.         wait_time = wait
  33.         if should_print:
  34.             sys.stdout.write("Waiting: "+str(wait_time)+" seconds")
  35.             sys.stdout.flush()
  36.         time.sleep(wait_time)
  37.  
  38. def mysplit(string):
  39.     words = []
  40.     inword = 0
  41.     for c in string:
  42.         if c in " \r\n\t": # whitespace
  43.             inword = 0
  44.         elif not inword:
  45.             words = words + [c]
  46.             inword = 1
  47.         else:
  48.             words[-1] = words[-1] + c
  49.     return words
  50.  
  51. def getarg(argv):
  52.     global target
  53.     global username
  54.     global password
  55.     global cmdfile
  56.     global verbose
  57.     global wait    
  58.     global interval
  59.  
  60.     target = ''
  61.     username = ''
  62.     password = ''
  63.     cmdfile = ''
  64.     verbose = False
  65.     wait = 5
  66.     interval = .2
  67.  
  68.     if len(sys.argv) == 1:
  69.         print 'netscript.py -t <target> -u <username> -p <password> -c <command_file> -v'
  70.         sys.exit()
  71.  
  72.     try:
  73.         opts, args = getopt.getopt(argv,"t:u:p:c:v",["target=","username=","password=","commandfile=","verbose"])
  74.     except getopt.GetoptError:
  75.         print 'netscript.py -t <target> -u <username> -p <password> -c <command_file> -v'
  76.         sys.exit(2)
  77.      
  78.     for opt, arg in opts:
  79.         if opt == '-h':
  80.             print 'netscript.py -t <target> -u <username> -p <password> -c <command_file> -v'
  81.             sys.exit()
  82.         elif opt in ("-t", "--target"):
  83.             target = arg
  84.         elif opt in ("-u", "--username"):
  85.             username = arg
  86.         elif opt in ("-p", "--password"):
  87.             password = arg
  88.         elif opt in ("-c", "--commandfile"):
  89.             cmdfile = arg
  90.         elif opt in ("-v", "--verbose"):
  91.             verbose = True    
  92.     if verbose:
  93.         print "-----------------------"
  94.     print 'Input parameters'
  95.         print 'Target host  :', target
  96.         print 'Username     :', username
  97.         print 'Password     :', password
  98.         print 'Command file :', cmdfile
  99.         print 'Verbose      :', verbose
  100.         print 'Global wait  :', wait,'seconds'
  101.     print 'Interval     :', interval,'seconds'
  102.         print "-----------------------"
  103.  
  104. if __name__ == '__main__':
  105.  
  106.     # Get the command-line arguments
  107.     getarg(sys.argv[1:])
  108.  
  109.     # Create the SSHClient object
  110.     remote_conn_pre = paramiko.SSHClient()
  111.  
  112.     # Automatically add untrusted hosts (be careful with this unless you trust your network)
  113.     remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  114.  
  115.     # Initiate SSH connection
  116.     remote_conn_pre.connect(target, username=username, password=password, timeout=wait, allow_agent=False, look_for_keys=False, pkey=None, key_filename=None)
  117.     print "SSH connection established to %s" % target
  118.  
  119.     # Invoke interactive SSH shel
  120.     shell = remote_conn_pre.invoke_shell()
  121.  
  122.     # Looping the command file and executing them in the shell
  123.     with open(cmdfile) as fp:
  124.         for line in fp:
  125.             line = line.strip()
  126.  
  127.             # Processing comments which starts with #'
  128.             if re.match(r'#', line):
  129.                 process_comments(line, verbose)
  130.  
  131.             # Processing actions which start with '!' (so far 'wait' is allowed)
  132.             elif re.match(r'!', line):
  133.                 process_actions(line, verbose)
  134.  
  135.             # Processing commands to the device
  136.             else:
  137.                 send_string_and_wait(line, interval, verbose)
  138.            
  139.     # Closing SSH connection
  140.     print "\nSSH connection closed from %s" % target
  141.     remote_conn_pre.close()
  142.  
  143.     quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement