Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko
- from prettytable import PrettyTable
- import time
- import ConfigParser
- import os
- import threading
- import subprocess
- import sys
- import re
- config= ConfigParser.ConfigParser()
- config_bgp= ConfigParser.ConfigParser()
- if os.path.isfile('sshInfo.conf'):
- config.read('sshInfo.conf')
- else:
- print("File not found")
- sys.exit()
- if os.path.isfile('bgp.conf'):
- config_bgp.read('bgp.conf')
- else:
- print("File not found")
- sys.exit()
- u_1 = config.get("credentials","username_r1")
- u_2 = config.get("credentials","username_r2")
- p_1 = config.get("credentials","password_r1")
- p_2 = config.get("credentials","password_r2")
- ip_1 = config.get("IP","IP_R1")
- ip_2 = config.get("IP","IP_R2")
- LAS_1= config_bgp.get("R1","LocalAS_number")
- R_ID_1= config_bgp.get("R1","RouterID")
- N_IP_1= config_bgp.get("R1","NeighbourIP")
- N_Rem_1= config_bgp.get("R1","NeighbourRemoteAS")
- N_LIST_1= config_bgp.get("R1","NetworkList")
- LAS_2= config_bgp.get("R2","LocalAS_number")
- R_ID_2= config_bgp.get("R2","RouterID")
- N_IP_2= config_bgp.get("R2","NeighbourIP")
- N_Rem_2= config_bgp.get("R2","NeighbourRemoteAS")
- N_LIST_2= config_bgp.get("R2","NetworkList")
- list_neighbour= N_LIST_1.split(',')
- dict_IP_1={}
- dict_mask={"24":"255.255.255.0","32":"255.255.255.255"}
- for i in list_neighbour:
- IP=i.split('/')[0]
- Mask=i.split('/')[1]
- Subnet_Mask=dict_mask[Mask]
- dict_IP_1[IP]=Subnet_Mask
- list_neighbour= N_LIST_2.split(',')
- dict_IP_2={}
- for i in list_neighbour:
- IP=i.split('/')[0]
- Mask=i.split('/')[1]
- Subnet_Mask=dict_mask[Mask]
- dict_IP_2[IP]=Subnet_Mask
- def connect_routers(ip,u,p,local,R_ID,N_IP,N_AS,dict_IP):
- session = paramiko.SSHClient()
- session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- session.connect(ip, username=u, password=p)
- connection = session.invoke_shell()
- response= subprocess.check_output(["ping","-c","3",ip])
- print(response)
- connection.send("enable\n")
- connection.send("cisco\n")
- connection.send("configure terminal\n")
- connection.send("router bgp "+local+"\n")
- connection.send("bgp router-id "+R_ID+"\n")
- connection.send("neighbor "+N_IP+" remote-as "+N_AS+"\n")
- for k,v in dict_IP.items():
- connection.send("network "+k+" mask "+v+"\n")
- time.sleep(1)
- connection.send("end\n")
- connection.send("show ip bgp neighbor\n")
- time.sleep(5)
- connection.send("terminal length 0\n")
- connection.send("show running\n")
- router_output = connection.recv(65535)
- nei_IP = re.search(r'[BGP neighbor is] (\d*\.\d*\.\d*\.\d*)',router_output)
- nei_IP=nei_IP.group(1)
- rem_AS = re.search(r'remote AS (\d*)',router_output)
- rem_AS=rem_AS.group(1)
- state = re.search(r'BGP state = (.*)',router_output)
- state=state.group(1)
- state = state.split(',')[0]
- time.sleep(20)
- print(router_output)
- table = PrettyTable(["BGP Neighbor IP","BGP Neighbor AS","BGP Neighbor State"])
- table.add_row([nei_IP,rem_AS,state])
- print(table)
- session.close()
- session = paramiko.SSHClient()
- session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- session.connect(ip, username=u, password=p)
- connection = session.invoke_shell()
- connection.send("enable\n")
- connection.send("cisco\n")
- connection.send("terminal length 0\n")
- connection.send("show running\n")
- time.sleep(20)
- router_output = connection.recv(65535)
- hostname = re.search(r'hostname (.*)',router_output)
- hostname=hostname.group(1).replace(" ","")
- filehandle = open(hostname+".txt", "w")
- filehandle.write(router_output)
- filehandle.close()
- session.close()
- response= subprocess.check_output(["s3cmd","mb","s3://hastu_router_configs"])
- print("Bucket Created!")
- response= subprocess.check_output(["s3cmd","put","/home/netman"+hostname+".txt","s3://hastu_router_configs"])
- print("Files Copied!")
- t1= threading.Thread(target=connect_routers, args=(ip_1,u_1,p_1,LAS_1,R_ID_1,N_IP_1,N_Rem_1,dict_IP_1))
- t2= threading.Thread(target=connect_routers, args=(ip_2,u_2,p_2,LAS_2,R_ID_2,N_IP_2,N_Rem_2,dict_IP_2))
- t1.start()
- t2.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement