Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import paramiko
- import subprocess
- import time
- import collections
- import pprint
- import getpass
- import sys
- import signal
- import re
- import os
- signal.signal(signal.SIGPIPE, signal.SIG_DFL) #IOError: Broken Pipe
- signal.signal(signal.SIGINT, signal.SIG_DFL) #KeyboardInterrupt: Ctrl-C
- F5_devices = {
- '<hostname of F5>' : '<IP address of F5>',
- }
- up_devices = collections.defaultdict(list)
- down_devices = collections.defaultdict(list)
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- HIGHGREEN = '\033[1;42m'
- def getcred():
- global UN
- global PW
- print(bcolors.HEADER + "##################################################################################################" + bcolors.ENDC)
- print(bcolors.HEADER + "###### Script to extract information from F5 devices (specified in dictionary) #######" + bcolors.ENDC)
- print(bcolors.HEADER + "##################################################################################################" + bcolors.ENDC)
- print
- UN = raw_input(bcolors.WARNING + "Username: " + bcolors.ENDC)
- PW_init = getpass.getpass(bcolors.WARNING + "Password: " + bcolors.ENDC)
- PW2_init = getpass.getpass(bcolors.WARNING + "Verify Password: " + bcolors.ENDC)
- if PW_init == PW2_init:
- PW = PW_init
- print(bcolors.HIGHGREEN + "Password's match --- Proceeding to SSH-Login function of script" + bcolors.ENDC)
- else:
- print(bcolors.FAIL + "Password's don't match" + bcolors.ENDC)
- sys.exit()
- def pingnode(name, host):
- print("[+] ----- Pinging %s : %s -----" % (name, host))
- ping = subprocess.Popen(["ping", "-c", "2", "-W", "1", "-i", "0.9", host], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output = ping.communicate()[0]
- if not "bytes from" in output.decode('utf-8'):
- down_devices[name].append('{0} -- DOWN'.format(host))
- else:
- up_devices[name].append('{0} -- UP'.format(host))
- def run_now(this):
- VIPS_found = []
- ips_found = []
- data = this.split('\n')
- for l in data:
- if 'destination' in l:
- VIPS_found.append(l.split()[0])
- ips_found.append(l.split()[2])
- if VIPS_found:
- keydict = dict(zip(VIPS_found, ips_found))
- if keydict:
- print_this(**keydict)
- def print_this(**args):
- for kk, vv in args.iteritems():
- print('{0}: {1}'.format(kk, vv))
- def ssh_kickoff(**args):
- for yy, xx in args.iteritems():
- if 'ltm' in yy:
- sshnode(yy, xx[0])
- def sshnode(hostname, ipaddr):
- print
- print(bcolors.WARNING + "Hostname:{0} IP-Addr:{1}".format(hostname, ipaddr) + bcolors.ENDC)
- try:
- twrssh = paramiko.SSHClient()
- twrssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- twrssh.connect(ipaddr, port=22, username=UN, password=PW, timeout=5.0)
- print(bcolors.WARNING + "Retrieving VIP Info from {0}...".format(hostname) + bcolors.ENDC)
- 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")
- if stdout:
- for line in stdout:
- run_now(str(line))
- else:
- print("No VIP info found on {0}".format(hostname))
- except:
- print(bcolors.FAIL + "SSH failed for {0}: {1}".format(hostname, ipaddr) + bcolors.ENDC)
- pass
- for kk, vv in F5_devices.iteritems():
- if 'ltm' in kk:
- pingnode(kk, vv)
- if __name__ == '__main__':
- if down_devices:
- print_now(**down_devices)
- print
- if up_devices:
- getcred()
- print
- ssh_kickoff(**up_devices)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement