Guest User

Untitled

a guest
Mar 6th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 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 testReadDatabase(input):
  42. import xml.etree.ElementTree as ET
  43. root = ET.fromstring(input)
  44. #for child in root[0][1]:
  45. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  46. print xmlns
  47.  
  48. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  49. #print child.tag, child.attrib
  50. hostname = child.find(xmlns+'isis-tlv') \
  51. .find(xmlns+'hostname-tlv') \
  52. .find(xmlns+'hostname').text
  53. print hostname
  54. ipaddress = child.find(xmlns+'isis-tlv') \
  55. .find(xmlns+'ipaddress-tlv') \
  56. .find(xmlns+'address').text
  57. print ipaddress
  58.  
  59. #for child2 in tlv:
  60. # print child2.tag#, child2.attrib
  61. #print db.rpc-reply.isis-database.isis-database-entry.isis-tlv.hostname-tlv
  62. #print child.tag, child.attrib
  63.  
  64. def main():
  65. #testGraph()
  66. #print runSSH(ssh_hostname, 'show isis database extensive | display xml')
  67. #test = runSSH(ssh_hostname, 'show isis database extensive | display xml')
  68. #testReadDatabase(test)
  69.  
  70. #print runSSH(ssh_hostname, 'show isis database | display xml')
  71. #print runSSH(ssh_hostname, 'show isis database GSHT-LAB-MC01.00-00 extensive | display xml')
  72. #print runSSH(ssh_hostname, 'show isis adjacency')
  73. #print runSSH(ssh_hostname, 'show isis hostname')
  74. #print runSSH(ssh_hostname, 'show isis interface')
  75. #print runSSH(ssh_hostname, 'show interfaces ae1')
  76. #print runSSH(ssh_hostname, 'show configuration interfaces ae1.0')
  77. #print runSSH(ssh_hostname, 'show configuration interfaces ae2.0')
  78. #print runSSH(ssh_hostname, 'show configuration interfaces lo0.0')
  79. #print runSSH(ssh_hostname, 'show isis database extensive | display xml')
  80.  
  81. nodes = getNodeList()
  82. createNode(nodes)
  83. createLink()
  84.  
  85. def getNodeList():
  86. input = runSSH(ssh_hostname, 'show isis database | display xml')
  87. root = ET.fromstring(input)
  88.  
  89. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  90. #print xmlns
  91.  
  92. result = []
  93.  
  94. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  95. #print child.tag, child.attrib
  96. lspid = child.find(xmlns+'lsp-id').text
  97. if not lspid in result:
  98. result.append(lspid)
  99.  
  100. return result
  101.  
  102. def createNode(nodes):
  103. from neo4j.v1 import GraphDatabase, basic_auth
  104. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  105. session = driver.session()
  106.  
  107. session.run("MATCH (n:Node) OPTIONAL MATCH (n)-[r]-() DELETE r,n")
  108.  
  109. for node in nodes:
  110. input = runSSH(ssh_hostname, 'show isis database %s extensive | display xml' % (node))
  111. root = ET.fromstring(input)
  112.  
  113. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  114. #print xmlns
  115.  
  116. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  117. #print child.tag, child.attrib
  118. hostname = child.find(xmlns+'isis-tlv') \
  119. .find(xmlns+'hostname-tlv') \
  120. .find(xmlns+'hostname').text
  121. ipaddress = child.find(xmlns+'isis-tlv') \
  122. .find(xmlns+'ipaddress-tlv') \
  123. .find(xmlns+'address').text
  124. print hostname
  125. print ipaddress
  126. print '----------'
  127. sys.stdout.flush()
  128.  
  129. session.run("CREATE (a:Node {title: {title}, hostname: {hostname}, ipaddress: {ipaddress}})",
  130. {"title": hostname, "hostname": hostname, "ipaddress": ipaddress})
  131.  
  132. session.close()
  133.  
  134. def createLink():
  135. from neo4j.v1 import GraphDatabase, basic_auth
  136. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  137. session = driver.session()
  138.  
  139. nodes = session.run("MATCH (n:Node) RETURN n.hostname AS hostname, n.ipaddress AS ipaddress")
  140.  
  141. for node in nodes:
  142. hostname = node["hostname"]
  143. ipaddress = node["ipaddress"]
  144.  
  145. input = runSSH(ipaddress, 'show isis adjacency | display xml')
  146. root = ET.fromstring(input)
  147.  
  148. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  149. #print xmlns
  150.  
  151. for child in root[0].findall(xmlns+'isis-adjacency'):
  152. #print child.tag, child.attrib
  153. interface_name = child.find(xmlns+'interface-name').text
  154. system_name = child.find(xmlns+'system-name').text
  155. adjacency_state = child.find(xmlns+'adjacency-state').text
  156. print hostname
  157. print system_name
  158. print interface_name
  159. print adjacency_state
  160. print '----------'
  161. sys.stdout.flush()
  162.  
  163. command = "MATCH (n1:Node {hostname:'%s'}), \
  164. (n2:Node {hostname:'%s'}) \
  165. CREATE (n1)-[:LINKED {name:'%s', state:'%s'}]->(n2)" % \
  166. (hostname, system_name, interface_name, adjacency_state)
  167. session.run(command)
  168. #session.run("MATCH (n1:Node {hostname:'{hostname1}'}), \
  169. # (n2:Node {hostname:'{hostname2}'}) \
  170. # CREATE (n1)-[:LINKED {name:'{name}', state:'{state}'}]->(n2)",
  171. # {"hostname1": hostname, "hostname2": system_name, "name": interface_name, "state": adjacency_state})
  172.  
  173. session.close()
  174.  
  175. if __name__ == "__main__" :
  176. main()
Add Comment
Please, Sign In to add comment