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