Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import paramiko
  4. import subprocess
  5. import time
  6. import collections
  7. import pprint
  8. import getpass
  9. import sys
  10. import signal
  11. import re
  12. import os
  13.  
  14. signal.signal(signal.SIGPIPE, signal.SIG_DFL) #IOError: Broken Pipe
  15. signal.signal(signal.SIGINT, signal.SIG_DFL) #KeyboardInterrupt: Ctrl-C
  16.  
  17.  
  18. F5_devices = {
  19. '<hostname of F5>' : '<IP address of F5>',
  20. }
  21.  
  22. up_devices = collections.defaultdict(list)
  23. down_devices = collections.defaultdict(list)
  24.  
  25.  
  26. class bcolors:
  27. HEADER = '\033[95m'
  28. OKBLUE = '\033[94m'
  29. OKGREEN = '\033[92m'
  30. WARNING = '\033[93m'
  31. FAIL = '\033[91m'
  32. ENDC = '\033[0m'
  33. BOLD = '\033[1m'
  34. UNDERLINE = '\033[4m'
  35. HIGHGREEN = '\033[1;42m'
  36.  
  37.  
  38. def getcred():
  39. global UN
  40. global PW
  41. print(bcolors.HEADER + "##################################################################################################" + bcolors.ENDC)
  42. print(bcolors.HEADER + "###### Script to extract information from F5 devices (specified in dictionary) #######" + bcolors.ENDC)
  43. print(bcolors.HEADER + "##################################################################################################" + bcolors.ENDC)
  44. print
  45. UN = raw_input(bcolors.WARNING + "Username: " + bcolors.ENDC)
  46. PW_init = getpass.getpass(bcolors.WARNING + "Password: " + bcolors.ENDC)
  47. PW2_init = getpass.getpass(bcolors.WARNING + "Verify Password: " + bcolors.ENDC)
  48. if PW_init == PW2_init:
  49. PW = PW_init
  50. print(bcolors.HIGHGREEN + "Password's match --- Proceeding to SSH-Login function of script" + bcolors.ENDC)
  51. else:
  52. print(bcolors.FAIL + "Password's don't match" + bcolors.ENDC)
  53. sys.exit()
  54.  
  55.  
  56.  
  57. def pingnode(name, host):
  58. print("[+] ----- Pinging %s : %s -----" % (name, host))
  59. ping = subprocess.Popen(["ping", "-c", "2", "-W", "1", "-i", "0.9", host], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  60. output = ping.communicate()[0]
  61. if not "bytes from" in output.decode('utf-8'):
  62. down_devices[name].append('{0} -- DOWN'.format(host))
  63. else:
  64. up_devices[name].append('{0} -- UP'.format(host))
  65.  
  66.  
  67.  
  68. def run_now(this):
  69. VIPS_found = []
  70. ips_found = []
  71. data = this.split('\n')
  72. for l in data:
  73. if 'destination' in l:
  74. VIPS_found.append(l.split()[0])
  75. ips_found.append(l.split()[2])
  76. if VIPS_found:
  77. keydict = dict(zip(VIPS_found, ips_found))
  78.  
  79. if keydict:
  80. print_this(**keydict)
  81.  
  82.  
  83.  
  84. def print_this(**args):
  85. for kk, vv in args.iteritems():
  86. print('{0}: {1}'.format(kk, vv))
  87.  
  88.  
  89.  
  90. def ssh_kickoff(**args):
  91. for yy, xx in args.iteritems():
  92. if 'ltm' in yy:
  93. sshnode(yy, xx[0])
  94.  
  95.  
  96.  
  97. def sshnode(hostname, ipaddr):
  98. print
  99. print(bcolors.WARNING + "Hostname:{0} IP-Addr:{1}".format(hostname, ipaddr) + bcolors.ENDC)
  100. try:
  101. twrssh = paramiko.SSHClient()
  102. twrssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  103. twrssh.connect(ipaddr, port=22, username=UN, password=PW, timeout=5.0)
  104. print(bcolors.WARNING + "Retrieving VIP Info from {0}...".format(hostname) + bcolors.ENDC)
  105. stdin, stdout, stderr = twrssh.exec_command("tmsh list ltm virtual | grep -iE --color '(ltm virtual|destination)' | sed -e 's/ltm virtual//g;s/{//g' | sed '/\(_\|-\)/{N;s/\\n//;}'\n")
  106. if stdout:
  107. for line in stdout:
  108. run_now(str(line))
  109. else:
  110. print("No VIP info found on {0}".format(hostname))
  111. except:
  112. print(bcolors.FAIL + "SSH failed for {0}: {1}".format(hostname, ipaddr) + bcolors.ENDC)
  113. pass
  114.  
  115.  
  116.  
  117. for kk, vv in F5_devices.iteritems():
  118. if 'ltm' in kk:
  119. pingnode(kk, vv)
  120.  
  121.  
  122.  
  123. if __name__ == '__main__':
  124. if down_devices:
  125. print_now(**down_devices)
  126. print
  127. if up_devices:
  128. getcred()
  129. print
  130. ssh_kickoff(**up_devices)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement