Guest User

Untitled

a guest
Mar 7th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 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. createNode()
  102. createLink()
  103.  
  104. def getNodeList():
  105. input = newSSH(ssh_hostname, 'show isis database | display xml')
  106. root = ET.fromstring(input)
  107.  
  108. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  109. #print xmlns
  110.  
  111. result = []
  112.  
  113. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  114. #print child.tag, child.attrib
  115. lspid = child.find(xmlns+'lsp-id').text
  116. if not lspid in result:
  117. result.append(lspid)
  118.  
  119. return result
  120.  
  121. def createNode(nodes):
  122. from neo4j.v1 import GraphDatabase, basic_auth
  123. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  124. session = driver.session()
  125.  
  126. session.run("MATCH (n:Node) OPTIONAL MATCH (n)-[r]-() DELETE r,n")
  127.  
  128. for node in nodes:
  129. input = newSSH(ssh_hostname, 'show isis database %s extensive | display xml' % (node))
  130. root = ET.fromstring(input)
  131.  
  132. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  133. #print xmlns
  134.  
  135. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  136. #print child.tag, child.attrib
  137. hostname = child.find(xmlns+'isis-tlv') \
  138. .find(xmlns+'hostname-tlv') \
  139. .find(xmlns+'hostname').text
  140. ipaddress = child.find(xmlns+'isis-tlv') \
  141. .find(xmlns+'ipaddress-tlv') \
  142. .find(xmlns+'address').text
  143. print hostname
  144. print ipaddress
  145. print '----------'
  146. sys.stdout.flush()
  147.  
  148. session.run("CREATE (a:Node {title: {title}, hostname: {hostname}, ipaddress: {ipaddress}})",
  149. {"title": hostname, "hostname": hostname, "ipaddress": ipaddress})
  150.  
  151. session.close()
  152.  
  153. def createNode():
  154. from neo4j.v1 import GraphDatabase, basic_auth
  155. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  156. session = driver.session()
  157.  
  158. session.run("MATCH (n:Node) OPTIONAL MATCH (n)-[r]-() DELETE r,n")
  159.  
  160. input = newSSH(ssh_hostname, 'show isis database extensive | display xml')
  161. root = ET.fromstring(input)
  162.  
  163. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  164. #print xmlns
  165.  
  166. for child in root[0][1].findall(xmlns+'isis-database-entry'):
  167. #print child.tag, child.attrib
  168. try:
  169. hostname = child.find(xmlns+'isis-tlv') \
  170. .find(xmlns+'hostname-tlv') \
  171. .find(xmlns+'hostname').text
  172. ipaddress = child.find(xmlns+'isis-tlv') \
  173. .find(xmlns+'ipaddress-tlv') \
  174. .find(xmlns+'address').text
  175. print hostname
  176. print ipaddress
  177. print '----------'
  178. sys.stdout.flush()
  179.  
  180. session.run("CREATE (a:Node {title: {title}, hostname: {hostname}, ipaddress: {ipaddress}})",
  181. {"title": hostname, "hostname": hostname, "ipaddress": ipaddress})
  182. except Exception:
  183. pass # or you could use 'continue'
  184.  
  185. session.close()
  186.  
  187. def createLink():
  188. from neo4j.v1 import GraphDatabase, basic_auth
  189. driver = GraphDatabase.driver(graph_address, auth=basic_auth(graph_username, graph_password))
  190. session = driver.session()
  191.  
  192. nodes = session.run("MATCH (n:Node) RETURN n.hostname AS hostname, n.ipaddress AS ipaddress")
  193.  
  194. for node in nodes:
  195. hostname = node["hostname"]
  196. ipaddress = node["ipaddress"]
  197.  
  198. try:
  199. input = newSSH(ipaddress, 'show isis adjacency | display xml')
  200. root = ET.fromstring(input)
  201.  
  202. xmlns = root[0].tag[root[0].tag.index('{'):root[0].tag.index('}')+1]
  203. #print xmlns
  204.  
  205. for child in root[0].findall(xmlns+'isis-adjacency'):
  206. #print child.tag, child.attrib
  207. interface_name = child.find(xmlns+'interface-name').text
  208. system_name = child.find(xmlns+'system-name').text
  209. adjacency_state = child.find(xmlns+'adjacency-state').text
  210. print hostname
  211. print system_name
  212. print interface_name
  213. print adjacency_state
  214. print '----------'
  215. sys.stdout.flush()
  216.  
  217. command = "MATCH (n1:Node {hostname:'%s'}), \
  218. (n2:Node {hostname:'%s'}) \
  219. CREATE (n1)-[:LINKED {name:'%s', state:'%s'}]->(n2)" % \
  220. (hostname, system_name, interface_name, adjacency_state)
  221. session.run(command)
  222. #session.run("MATCH (n1:Node {hostname:'{hostname1}'}), \
  223. # (n2:Node {hostname:'{hostname2}'}) \
  224. # CREATE (n1)-[:LINKED {name:'{name}', state:'{state}'}]->(n2)",
  225. # {"hostname1": hostname, "hostname2": system_name, "name": interface_name, "state": adjacency_state})
  226. except Exception:
  227. pass # or you could use 'continue'
  228.  
  229. session.close()
  230.  
  231. if __name__ == "__main__" :
  232. main()
Add Comment
Please, Sign In to add comment