Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import paramiko
- import sys
- import getpass
- import signal
- import collections
- import os
- import time
- import subprocess
- import datetime
- signal.signal(signal.SIGPIPE, signal.SIG_DFL) #IOError: Broken Pipe
- signal.signal(signal.SIGINT, signal.SIG_DFL) #KeyboardInterrupt: Ctrl-C
- DEVICES = {
- 'mgmt2' : '192.168.1.109',
- 'mgmt3' : '192.168.1.124'
- }
- 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'):
- pass
- else:
- return name, host
- def write_now(name, content):
- print('Running write_now func.. ')
- #filename = name + '.txt'
- filename = "{0}-{1}.txt".format(name, datetime.date.today())
- print(filename)
- if os.path.isfile(filename):
- print('Removing file: {0}'.format(name))
- os.remove(filename)
- with open(filename, 'a') as myfile:
- print('Creating file for host: {0}'.format(filename))
- myfile.write(content)
- def send_string_and_wait_for_string(remote, command, filename=None, wait_string=None, create_file=False, should_print=False):
- remote.send(command)
- receive_buffer = ""
- while not wait_string in receive_buffer:
- receive_buffer += remote.recv(1024)
- if should_print:
- print receive_buffer
- if create_file:
- write_now(filename, receive_buffer)
- return receive_buffer
- def sshnode(hostname, ipaddr):
- to_be_run = ['cat output.txt']
- #to_be_run = ['show run']
- 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)
- remote = twrssh.invoke_shell()
- time.sleep(0.5)
- output = remote.recv(1000)
- #remote.send('term len 0\n')
- time.sleep(0.9)
- sys.stdout.flush()
- print(bcolors.WARNING + "Running command(s) on {0}...".format(hostname) + bcolors.ENDC)
- for ll in to_be_run:
- send_string_and_wait_for_string(remote, ll + "\n", filename=hostname, wait_string="tester@", create_file=True, should_print=False)
- #send_string_and_wait_for_string(remote, ll + "\n", wait_string="{0}#".format(hostname), create_file=True, should_print=False)
- except:
- print(bcolors.FAIL + "SSH failed for {0}: {1}".format(hostname, ipaddr) + bcolors.ENDC)
- pass
- def main():
- output = [ pingnode(kk, vv) for kk,vv in DEVICES.iteritems() ]
- if output:
- getcred()
- [ sshnode(kk, vv) for kk,vv in filter(None, output) ]
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment