Skip to content
Snippets Groups Projects
Commit c3b36a36 authored by Yang Yuechun's avatar Yang Yuechun
Browse files

telnet

parent a3cbf01d
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -5,7 +5,7 @@ import socket ...@@ -5,7 +5,7 @@ import socket
# TCP_IP = '127.0.0.1' # TCP_IP = '127.0.0.1'
# TCP_PORT = 5006 # TCP_PORT = 5006
BUFFER_SIZE = 1024 BUFFER_SIZE = 1024
UDP_IP = "127.0.0.1" UDP_IP = "10.193.163.46"
UDP_PORT = 5005 UDP_PORT = 5005
# MESSAGE = "Hello, World!" # MESSAGE = "Hello, World!"
......
import gns3 import gns3
import getpass
import sys
import telnetlib
project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc' project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc'
...@@ -10,6 +13,8 @@ def update_nodes_info(): ...@@ -10,6 +13,8 @@ def update_nodes_info():
node_info = {} node_info = {}
node_info['name'] = node.get('name') node_info['name'] = node.get('name')
node_info['node_id'] = node.get('node_id') node_info['node_id'] = node.get('node_id')
node_info['console'] = node.get('console')
node_info['name'] = node.get('name')
if node.get('properties').get('platform'): if node.get('properties').get('platform'):
node_info['platform'] = node.get('properties').get('platform') node_info['platform'] = node.get('properties').get('platform')
nodes_info.append(node_info) nodes_info.append(node_info)
...@@ -26,4 +31,18 @@ def update_links_info(): ...@@ -26,4 +31,18 @@ def update_links_info():
link_info['node_id_2'] = link.get('nodes')[1].get('node_id') link_info['node_id_2'] = link.get('nodes')[1].get('node_id')
links_info.append(link_info) links_info.append(link_info)
print('link updated') print('link updated')
return links_info return links_info
\ No newline at end of file
def telnet_console(PORT, cmd, name):
HOST = "127.0.0.1"
tn = telnetlib.Telnet(HOST, PORT)
tn.read_very_eager().decode('ascii')
tn.write(cmd.encode('ascii') + b"\r\n")
data = tn.read_until((name+'#').encode()).decode('ascii')
tn.close()
return data
# print(telnet_console(5000, "show int sum", "R1"))
# print(telnet_console(5000, "show int sum", "R1"))
# print(telnet_console(5000, "show int sum", "R1"))
\ No newline at end of file
Provided functionality in GNS3: Provided functionality in GNS3:
0: version information 00: version information
1: nodes information 01: nodes information
2: links information 02: links information
3: create node 03: create node
4: delete node 04: delete node
5: create link 05: create link
6: delete link 06: delete link
7: start all nodes 07: start all nodes
8: stop all nodes 08: stop all nodes
9: suspend all nodes 09: suspend all nodes
10: start one node 10: start one node
11: stop one node 11: stop one node
12: suspend one node 12: suspend one node
13: console command
-1: close connection -1: close connection
Enter index: Enter index:
\ No newline at end of file
...@@ -10,7 +10,7 @@ import time ...@@ -10,7 +10,7 @@ import time
# TCP_PORT = 5006 # TCP_PORT = 5006
BUFFER_SIZE = 1024 # Normally 1024, but we want fast response BUFFER_SIZE = 1024 # Normally 1024, but we want fast response
project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc' project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc'
UDP_IP = "127.0.0.1" UDP_IP = "10.193.163.46"
UDP_PORT = 5005 UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet sock = socket.socket(socket.AF_INET, # Internet
...@@ -33,43 +33,44 @@ while 1: ...@@ -33,43 +33,44 @@ while 1:
msg, addr = sock.recvfrom(BUFFER_SIZE) msg, addr = sock.recvfrom(BUFFER_SIZE)
index = msg.decode('utf-8') index = msg.decode('utf-8')
if index == '0': if index == '00':
data = str(gns3.get_version())+str('END') data = str(gns3.get_version())+str('END')
elif index == '1': elif index == '01':
nodes_info = gn.update_nodes_info() nodes_info = gn.update_nodes_info()
data = str(nodes_info)+str('END') data = str(nodes_info)+str('END')
elif index == '2': elif index == '02':
links_info = gn.update_links_info() links_info = gn.update_links_info()
data = str(links_info)+str('END') data = str(links_info)+str('END')
elif index == '3': elif index == '03':
nodes_info = gn.update_nodes_info() nodes_info = gn.update_nodes_info()
name = len(nodes_info)+1 name = len(nodes_info)+1
# gns3.node_create(project_id, name) gns3.node_create(project_id, name)
data = str(gns3.node_create(project_id, name))+str('END') data = str('okEND')
# data = str('OK 4')
elif index == '4': elif index == '04':
nodes_info = gn.update_nodes_info() nodes_info = gn.update_nodes_info()
node_id = nodes_info[-1].get('node_id') node_id = nodes_info[-1].get('node_id')
data = str(gns3.node_delete(project_id, node_id))+str('END') data = str(gns3.node_delete(project_id, node_id))+str('END')
elif index == '7': elif index == '07':
data = str(gns3.node_start_all(project_id))+str('END') data = str(gns3.node_start_all(project_id))+str('END')
elif index == '8': elif index == '08':
data = str(gns3.node_stop_all(project_id))+str('END') data = str(gns3.node_stop_all(project_id))+str('END')
elif index == '9': elif index == '09':
data = str(gns3.node_suspend_all(project_id))+str('END') data = str(gns3.node_suspend_all(project_id))+str('END')
elif index[0:2] == '13':
node = nodes_info[0]
data = gn.telnet_console(node.get('console'), index[2:], node.get('name')) + str('END')
elif index == '-1': elif index == '-1':
data = str('close')+str('END') data = str('close')+str('END')
elif not index: break
else: else:
data = str('incorrect msg')+str('END') data = str('incorrect msg')+str('END')
sock.sendto(data.encode('utf-8'), addr) # echo sock.sendto(data.encode('utf-8'), addr) # echo
......
import getpass
import sys
import telnetlib
def telnet_console(PORT, cmd):
HOST = "127.0.0.1"
tn = telnetlib.Telnet(HOST, 5000)
tn.read_very_eager().decode('ascii')
tn.write(cmd.encode('ascii') + b"\r\n")
data = tn.read_until("R1#".encode()).decode('ascii')
return data
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment