Advertisement
CrazyUncleHarris

Untitled

Mar 7th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 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.  
  28. # search the uptime string to extract the data we need
  29. days = '0'
  30. hours = '0'
  31. mins = '0'
  32. if 'day' in str(outputs[0]):
  33. match = re.search('([0-9]+)\s+day', str(outputs[0]))
  34. days = str(int(match.group(1)))
  35. if 'hours' in str(outputs[0]):
  36. match = re.search('([0-9]+):([0-9]+)', str(outputs[0]))
  37. hours = str(int(match.group(1)))
  38. mins = str(int(match.group(2)))
  39.  
  40. # search the port string for ports
  41. ports ='0'
  42. if 'Local' in str(outputs[1]):
  43. match = re.findall(':[0-9][0-9]', str(outputs[1]))
  44. # ports = str(int(match.group(1)))
  45. if match:
  46. print('YAY MATCH')
  47.  
  48.  
  49. # print out to console
  50. print('--------------------------------------------')
  51. print('Server Uptime: ' + days + ' days ' + hours + ' hours ' + mins + 'mins')
  52. print('--------------------------------------------')
  53. print('open network ports: ' + ports)
  54. print('--------------------------------------------')
  55. print('running processes: '+repr(outputs[2]))
  56. if __name__ =='__main__':
  57. parser = argparse.ArgumentParser(description='Connect over SSH')
  58. parser.add_argument('hostname', help='remote machine name')
  59. parser.add_argument('username', help='Username on the remote machine')
  60. args = parser.parse_args()
  61. password = getpass.getpass('Password:')
  62. main(args.hostname, args.username, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement