Guest User

Untitled

a guest
Mar 7th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. def testGraph():
  2. from neo4j.v1 import GraphDatabase, basic_auth
  3. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  4. session = driver.session()
  5.  
  6. session.run("CREATE (a:Person {name: {name}, title: {title}})",
  7. {"name": "Arthur", "title": "King"})
  8.  
  9. result = session.run("MATCH (a:Person) WHERE a.name = {name} "
  10. "RETURN a.name AS name, a.title AS title",
  11. {"name": "Arthur"})
  12. for record in result:
  13. print("%s %s" % (record["title"], record["name"]))
  14.  
  15. session.close()
  16.  
  17. def runSSH(hostname, command):
  18. client = paramiko.Transport((hostname, ssh_port))
  19. client.connect(username=ssh_username, password=ssh_password)
  20.  
  21. stdout_data = []
  22. stderr_data = []
  23. session = client.open_channel(kind='session')
  24. session.exec_command(command)
  25. while True:
  26. if session.recv_ready():
  27. stdout_data.append(session.recv(buffer_nbytes))
  28. if session.recv_stderr_ready():
  29. stderr_data.append(session.recv_stderr(buffer_nbytes))
  30. if session.exit_status_ready():
  31. break
  32.  
  33. if session.recv_exit_status() == 0:
  34. return ''.join([str(x) for x in stdout_data])
  35. else:
  36. return 'exit status: '.join(session.recv_exit_status()).join(''.join([str(x) for x in stderr_data]))
  37.  
  38. session.close()
  39. client.close()
  40.  
  41. def newSSH(hostname, command, timeout=1):
  42. client = paramiko.Transport((hostname, ssh_port))
  43. client.connect(username=ssh_username, password=ssh_password)
  44.  
  45. stdout_data = []
  46. session = client.open_channel(kind='session')
  47. if timeout: session.settimeout(timeout)
  48. session.exec_command(command)
  49. while True:
  50. buf = session.recv(1024)
  51. if not buf:
  52. break
  53. stdout_data.append(buf)
  54.  
  55. session.close()
  56. client.close()
  57.  
  58. return ''.join([str(x) for x in stdout_data])
  59.  
  60. def testReadDatabase(input):
  61. import xml.etree.ElementTree as ET
  62. root = ET.fromstring(input)
  63. #for child in root[0][1]:
  64. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  65. print xmlns
  66.  
  67. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  68. #print child.tag, child.attrib
  69. hostname = child.find(xmlns+'isis-tlv') \
  70. .find(xmlns+'hostname-tlv') \
  71. .find(xmlns+'hostname').text
  72. print hostname
  73. ipaddress = child.find(xmlns+'isis-tlv') \
  74. .find(xmlns+'ipaddress-tlv') \
  75. .find(xmlns+'address').text
  76. print ipaddress
  77.  
  78. #for child2 in tlv:
  79. # print child2.tag#, child2.attrib
  80. #print db.rpc-reply.isis-database.isis-database-entry.isis-tlv.hostname-tlv
  81. #print child.tag, child.attrib
  82.  
  83. def main():
  84. #testGraph()
  85. #print newSSH(ssh_hostname, 'show isis database extensive | display xml')
  86. #test = runSSH(ssh_hostname, 'show isis database extensive | display xml')
  87. #testReadDatabase(test)
  88.  
  89. #print runSSH(ssh_hostname, 'show isis database | display xml')
  90. #print runSSH(ssh_hostname, 'show isis database GSHT-LAB-MC01.00-00 extensive | display xml')
  91. #print runSSH(ssh_hostname, 'show isis adjacency')
  92. #print runSSH(ssh_hostname, 'show isis hostname')
  93. #print runSSH(ssh_hostname, 'show isis interface')
  94. #print runSSH(ssh_hostname, 'show interfaces ae1')
  95. #print runSSH(ssh_hostname, 'show configuration interfaces ae1.0')
  96. #print runSSH(ssh_hostname, 'show configuration interfaces ae2.0')
  97. #print runSSH(ssh_hostname, 'show configuration interfaces lo0.0')
  98. #print runSSH(ssh_hostname, 'show isis database extensive | display xml')
  99.  
  100. #nodes = getNodeList()
  101. #createNode(nodes)
  102. createNode()
  103. createLink()
  104.  
  105. def getNodeList():
  106. input = newSSH(ssh_hostname, 'show isis database | display xml')
  107. root = ET.fromstring(input)
  108.  
  109. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  110. #print xmlns
  111.  
  112. result = []
  113.  
  114. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  115. #print child.tag, child.attrib
  116. lspid = child.find(xmlns+'lsp-id').text
  117. if not lspid in result:
  118. result.append(lspid)
  119.  
  120. return result
  121.  
  122. def createNode(nodes):
  123. from neo4j.v1 import GraphDatabase, basic_auth
  124. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  125. session = driver.session()
  126.  
  127. session.run("MATCH (n:Node) OPTIONAL MATCH (n)-[r]-() DELETE r,n")
  128.  
  129. for node in nodes:
  130. input = newSSH(ssh_hostname, 'show isis database %s extensive | display xml' % (node))
  131. root = ET.fromstring(input)
  132.  
  133. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  134. #print xmlns
  135.  
  136. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  137. #print child.tag, child.attrib
  138. hostname = child.find(xmlns+'isis-tlv') \
  139. .find(xmlns+'hostname-tlv') \
  140. .find(xmlns+'hostname').text
  141. ipaddress = child.find(xmlns+'isis-tlv') \
  142. .find(xmlns+'ipaddress-tlv') \
  143. .find(xmlns+'address').text
  144. print hostname
  145. print ipaddress
  146. print '----------'
  147. sys.stdout.flush()
  148.  
  149. session.run("CREATE (a:Node {title: {title}, hostname: {hostname}, ipaddress: {ipaddress}})",
  150. {"title": hostname, "hostname": hostname, "ipaddress": ipaddress})
  151.  
  152. session.close()
  153.  
  154. def createNode():
  155. from neo4j.v1 import GraphDatabase, basic_auth
  156. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  157. session = driver.session()
  158.  
  159. session.run("MATCH (n:Node) OPTIONAL MATCH (n)-[r]-() DELETE r,n")
  160.  
  161. input = newSSH(ssh_hostname, 'show isis database extensive | display xml')
  162. root = ET.fromstring(input)
  163.  
  164. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  165. #print xmlns
  166.  
  167. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  168. #print child.tag, child.attrib
  169. try:
  170. hostname = child.find(xmlns+'isis-tlv') \
  171. .find(xmlns+'hostname-tlv') \
  172. .find(xmlns+'hostname').text
  173. ipaddress = child.find(xmlns+'isis-tlv') \
  174. .find(xmlns+'ipaddress-tlv') \
  175. .find(xmlns+'address').text
  176. print hostname
  177. print ipaddress
  178. print '----------'
  179. sys.stdout.flush()
  180.  
  181. session.run("CREATE (a:Node {title: {title}, hostname: {hostname}, ipaddress: {ipaddress}})",
  182. {"title": hostname, "hostname": hostname, "ipaddress": ipaddress})
  183. except Exception as e:
  184. print e
  185. pass # or you could use 'continue'
  186.  
  187. session.close()
  188.  
  189. def createLink():
  190. from neo4j.v1 import GraphDatabase, basic_auth
  191. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  192. session = driver.session()
  193.  
  194. nodes = session.run("MATCH (n:Node) RETURN n.hostname AS hostname, n.ipaddress AS ipaddress")
  195.  
  196. for node in nodes:
  197. hostname = node["hostname"]
  198. ipaddress = node["ipaddress"]
  199.  
  200. try:
  201. print '===================='
  202. print "Connect to %s(%s)" % (hostname, ipaddress)
  203. sys.stdout.flush()
  204. input = newSSH(ipaddress, 'show isis adjacency | display xml')
  205. root = ET.fromstring(input)
  206.  
  207. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  208. #print xmlns
  209.  
  210. for child in root[0].findall(xmlns+'isis-adjacency'):
  211. #print child.tag, child.attrib
  212. interface_name = child.find(xmlns+'interface-name').text
  213. system_name = child.find(xmlns+'system-name').text
  214. adjacency_state = child.find(xmlns+'adjacency-state').text
  215. print '----------'
  216. print hostname
  217. print system_name
  218. print interface_name
  219. print adjacency_state
  220. sys.stdout.flush()
  221.  
  222. command = "MATCH (n1:Node {hostname:'%s'}), \
  223. (n2:Node {hostname:'%s'}) \
  224. CREATE (n1)-[:LINKED {name:'%s', state:'%s'}]->(n2)" % \
  225. (hostname, system_name, interface_name, adjacency_state)
  226. session.run(command)
  227. #session.run("MATCH (n1:Node {hostname:'{hostname1}'}), \
  228. # (n2:Node {hostname:'{hostname2}'}) \
  229. # CREATE (n1)-[:LINKED {name:'{name}', state:'{state}'}]->(n2)",
  230. # {"hostname1": hostname, "hostname2": system_name, "name": interface_name, "state": adjacency_state})
  231. except Exception as e:
  232. print e
  233. pass # or you could use 'continue'
  234.  
  235. session.close()
  236.  
  237. if __name__ == "__main__" :
  238. main()
Add Comment
Please, Sign In to add comment