Advertisement
Guest User

netscript.py

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