Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import paramiko
  2. from prettytable import PrettyTable
  3. import time
  4. import ConfigParser
  5. import os
  6. import threading
  7. import subprocess
  8. import sys
  9. import re
  10.  
  11. config= ConfigParser.ConfigParser()
  12. config_bgp= ConfigParser.ConfigParser()
  13. if os.path.isfile('sshInfo.conf'):
  14. config.read('sshInfo.conf')
  15. else:
  16. print("File not found")
  17. sys.exit()
  18.  
  19. if os.path.isfile('bgp.conf'):
  20. config_bgp.read('bgp.conf')
  21. else:
  22. print("File not found")
  23. sys.exit()
  24.  
  25. u_1 = config.get("credentials","username_r1")
  26. u_2 = config.get("credentials","username_r2")
  27. p_1 = config.get("credentials","password_r1")
  28. p_2 = config.get("credentials","password_r2")
  29. ip_1 = config.get("IP","IP_R1")
  30. ip_2 = config.get("IP","IP_R2")
  31.  
  32. LAS_1= config_bgp.get("R1","LocalAS_number")
  33. R_ID_1= config_bgp.get("R1","RouterID")
  34. N_IP_1= config_bgp.get("R1","NeighbourIP")
  35. N_Rem_1= config_bgp.get("R1","NeighbourRemoteAS")
  36. N_LIST_1= config_bgp.get("R1","NetworkList")
  37. LAS_2= config_bgp.get("R2","LocalAS_number")
  38. R_ID_2= config_bgp.get("R2","RouterID")
  39. N_IP_2= config_bgp.get("R2","NeighbourIP")
  40. N_Rem_2= config_bgp.get("R2","NeighbourRemoteAS")
  41. N_LIST_2= config_bgp.get("R2","NetworkList")
  42.  
  43. list_neighbour= N_LIST_1.split(',')
  44. dict_IP_1={}
  45. dict_mask={"24":"255.255.255.0","32":"255.255.255.255"}
  46. for i in list_neighbour:
  47. IP=i.split('/')[0]
  48. Mask=i.split('/')[1]
  49. Subnet_Mask=dict_mask[Mask]
  50. dict_IP_1[IP]=Subnet_Mask
  51.  
  52. list_neighbour= N_LIST_2.split(',')
  53. dict_IP_2={}
  54. for i in list_neighbour:
  55. IP=i.split('/')[0]
  56. Mask=i.split('/')[1]
  57. Subnet_Mask=dict_mask[Mask]
  58. dict_IP_2[IP]=Subnet_Mask
  59.  
  60.  
  61. def connect_routers(ip,u,p,local,R_ID,N_IP,N_AS,dict_IP):
  62. session = paramiko.SSHClient()
  63. session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  64. session.connect(ip, username=u, password=p)
  65. connection = session.invoke_shell()
  66. response= subprocess.check_output(["ping","-c","3",ip])
  67. print(response)
  68.  
  69. connection.send("enable\n")
  70. connection.send("cisco\n")
  71. connection.send("configure terminal\n")
  72. connection.send("router bgp "+local+"\n")
  73. connection.send("bgp router-id "+R_ID+"\n")
  74. connection.send("neighbor "+N_IP+" remote-as "+N_AS+"\n")
  75. for k,v in dict_IP.items():
  76. connection.send("network "+k+" mask "+v+"\n")
  77. time.sleep(1)
  78. connection.send("end\n")
  79. connection.send("show ip bgp neighbor\n")
  80. time.sleep(5)
  81. connection.send("terminal length 0\n")
  82. connection.send("show running\n")
  83. router_output = connection.recv(65535)
  84. nei_IP = re.search(r'[BGP neighbor is] (\d*\.\d*\.\d*\.\d*)',router_output)
  85. nei_IP=nei_IP.group(1)
  86. rem_AS = re.search(r'remote AS (\d*)',router_output)
  87. rem_AS=rem_AS.group(1)
  88. state = re.search(r'BGP state = (.*)',router_output)
  89. state=state.group(1)
  90. state = state.split(',')[0]
  91.  
  92. time.sleep(20)
  93. print(router_output)
  94. table = PrettyTable(["BGP Neighbor IP","BGP Neighbor AS","BGP Neighbor State"])
  95. table.add_row([nei_IP,rem_AS,state])
  96. print(table)
  97. session.close()
  98.  
  99. session = paramiko.SSHClient()
  100. session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  101. session.connect(ip, username=u, password=p)
  102. connection = session.invoke_shell()
  103. connection.send("enable\n")
  104. connection.send("cisco\n")
  105. connection.send("terminal length 0\n")
  106. connection.send("show running\n")
  107. time.sleep(20)
  108. router_output = connection.recv(65535)
  109. hostname = re.search(r'hostname (.*)',router_output)
  110. hostname=hostname.group(1).replace(" ","")
  111. filehandle = open(hostname+".txt", "w")
  112. filehandle.write(router_output)
  113. filehandle.close()
  114. session.close()
  115. response= subprocess.check_output(["s3cmd","mb","s3://hastu_router_configs"])
  116. print("Bucket Created!")
  117. response= subprocess.check_output(["s3cmd","put","/home/netman"+hostname+".txt","s3://hastu_router_configs"])
  118. print("Files Copied!")
  119.  
  120. 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))
  121. 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))
  122. t1.start()
  123. t2.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement