Advertisement
CrazyUncleHarris

Untitled

Mar 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!usr/bin/env python3
  2. # Harris Plaisted
  3. # Program 2  remote monitoring of playground hosts
  4.  
  5. import argparse, paramiko, getpass, re
  6.  
  7. class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
  8.     def missing_host_key(self, client, hostname, key):
  9.         return
  10.  
  11. def main(hostname,username,password):
  12.     client =paramiko.SSHClient()
  13.     client.set_missing_host_key_policy(AllowAnythingPolicy())
  14.     client.connect(hostname,username=username,password=password)
  15.     print('vital statistics for server: ' + args.hostname)
  16.     outputs =['p','re','set']
  17.     counter = 0
  18.     for command in 'uptime','netstat -atun','ps -ef':
  19.         stdin, stdout, stderr = client.exec_command(command)
  20.         stdin.close()
  21.     #   print(repr(stdout.read()))
  22.         outputs[counter] = stdout.read()
  23.         stdout.close()
  24.         stderr.close()
  25.         counter += 1
  26.     client.close()
  27.     # search the uptime string to extract the data we need
  28.     days ='0'
  29.     if 'day' in outputs[0]:
  30.        match = re.search('([0-9]+)\s+day',outputs[0])
  31.        days = str(int(match.group(1)))
  32.     print('--------------------------------------------')
  33.     print('Server Uptime: ' + days + ' days')
  34.     print('--------------------------------------------')
  35.     print('open network ports: '+repr(outputs[1]))
  36.     print('--------------------------------------------')
  37.     print('running processes: '+repr(outputs[2]))
  38. if __name__ =='__main__':
  39.     parser = argparse.ArgumentParser(description='Connect over SSH')
  40.     parser.add_argument('hostname', help='remote machine name')
  41.     parser.add_argument('username', help='Username on the remote machine')
  42.     args = parser.parse_args()
  43.     password = getpass.getpass('Password:')
  44.     main(args.hostname, args.username, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement