From c3b36a367ef75991725a505e4b6cc232cbf4c1a5 Mon Sep 17 00:00:00 2001
From: Yang Yuechun <younggy1997@outlook.com>
Date: Tue, 18 Dec 2018 19:44:29 -0600
Subject: [PATCH] telnet

---
 server/__pycache__/gns3_helper.cpython-36.pyc | Bin 1016 -> 1436 bytes
 server/client.py                              |   2 +-
 server/gns3_helper.py                         |  21 ++++++++++++-
 server/index_instruction.txt                  |  21 +++++++------
 server/server.py                              |  29 +++++++++---------
 server/telnet_console.py                      |  13 ++++++++
 6 files changed, 60 insertions(+), 26 deletions(-)
 create mode 100644 server/telnet_console.py

diff --git a/server/__pycache__/gns3_helper.cpython-36.pyc b/server/__pycache__/gns3_helper.cpython-36.pyc
index 745646374da305b41ff475d36728bc237db394c5..4ba445dd2ed5e0127f25d4d097bb8ce90b2a157c 100644
GIT binary patch
literal 1436
zcmY*Z&5qkP5GEy3lHFY=+boa`S`-DE9(>q1jyLHB2#NyDp{GsIY=0oM5M+_Nl_OIs
zNpG`;3>0uLeSzNk0DUE1d&(>1)EUZMJ0dvb3}=SJ`DQfP-|zqa_r0I~<b?c7cD?}i
zU%@m;1QE0%X}8jpdOk=4zlLcjf|Q9+us=x31s4&lk%&bCYb@w1GVHwtFXU`_|8z1v
zdHD44>9G{@$?=Jh#qrbP;^J72$5U}JIh{_6qCj2zASS|Keht(71>};fh$S;>gEd`I
zL07?>tCl_|KdV((k`-&i8N{-tEfc{r;<?as;f7<LV;k;$O}?gJ9j&Quz2yxL-XgFd
zGuDDVCBGj1Kr9zA?pp${OU@i|OyzUwVpR*7mBPhEtxR3XwBP8uk=mBhxTL9adr|B8
zav$}~r;A49R*GTd*rhTPci_cYsZc4mWvv|A&tzfIX?Q8Ei}I$CN;ux=QrRJQ{jHH>
zXzCbv%^@PVBkdve9{(Zw0FwA_I{MB?ZASCF7(JKfwXK`cLY4M9+gvr0g*=%&&WgM-
zwpKE`(iwUzq_R?HSEX6xRk`%>jlk8F)FTW(`$bmp)CbMAJLq1PZ9_{C#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@ -5,7 +5,7 @@ import socket
 # TCP_IP = '127.0.0.1'
 # TCP_PORT = 5006
 BUFFER_SIZE = 1024
-UDP_IP = "127.0.0.1"
+UDP_IP = "10.193.163.46"
 UDP_PORT = 5005
 # MESSAGE = "Hello, World!"
 
diff --git a/server/gns3_helper.py b/server/gns3_helper.py
index 2ef94a2..950d34e 100644
--- a/server/gns3_helper.py
+++ b/server/gns3_helper.py
@@ -1,4 +1,7 @@
 import gns3
+import getpass
+import sys
+import telnetlib
 
 project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc'
 
@@ -10,6 +13,8 @@ def update_nodes_info():
     node_info = {}
     node_info['name'] = node.get('name')
     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'):
       node_info['platform'] = node.get('properties').get('platform')
     nodes_info.append(node_info)
@@ -26,4 +31,18 @@ def update_links_info():
     link_info['node_id_2'] = link.get('nodes')[1].get('node_id')
     links_info.append(link_info)
   print('link updated')
-  return links_info
\ No newline at end of file
+  return links_info
+
+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
diff --git a/server/index_instruction.txt b/server/index_instruction.txt
index f02ed0b..2616d20 100644
--- a/server/index_instruction.txt
+++ b/server/index_instruction.txt
@@ -1,18 +1,19 @@
 Provided functionality in GNS3:
 
-0: version information
-1: nodes information
-2: links information
-3: create node
-4: delete node
-5: create link
-6: delete link
-7: start all nodes
-8: stop all nodes
-9: suspend all nodes
+00: version information
+01: nodes information
+02: links information
+03: create node
+04: delete node
+05: create link
+06: delete link
+07: start all nodes
+08: stop all nodes
+09: suspend all nodes
 10: start one node
 11: stop one node
 12: suspend one node
+13: console command
 -1: close connection
 
 Enter index:
\ No newline at end of file
diff --git a/server/server.py b/server/server.py
index 21081ac..97b3d20 100644
--- a/server/server.py
+++ b/server/server.py
@@ -10,7 +10,7 @@ import time
 # TCP_PORT = 5006
 BUFFER_SIZE = 1024  # Normally 1024, but we want fast response
 project_id ='83b40958-ede6-4dec-9cff-e11bdf38bbcc'
-UDP_IP = "127.0.0.1"
+UDP_IP = "10.193.163.46"
 UDP_PORT = 5005
 
 sock = socket.socket(socket.AF_INET, # Internet
@@ -33,43 +33,44 @@ while 1:
   msg, addr = sock.recvfrom(BUFFER_SIZE)
   index = msg.decode('utf-8')
 
-  if index == '0':
+  if index == '00':
     data = str(gns3.get_version())+str('END')
 
-  elif index == '1':
+  elif index == '01':
     nodes_info = gn.update_nodes_info()
     data = str(nodes_info)+str('END')
 
-  elif index == '2':
+  elif index == '02':
     links_info = gn.update_links_info()
     data = str(links_info)+str('END')
 
-  elif index == '3':
+  elif index == '03':
     nodes_info = gn.update_nodes_info()
     name = len(nodes_info)+1
-    # gns3.node_create(project_id, name)
-    data = str(gns3.node_create(project_id, name))+str('END')
-    # data = str('OK 4')
+    gns3.node_create(project_id, name)
+    data = str('okEND')
 
-  elif index == '4':
+  elif index == '04':
     nodes_info = gn.update_nodes_info()
     node_id = nodes_info[-1].get('node_id')
     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')
 
-  elif index == '8':
+  elif index == '08':
     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')
 
+  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':
     data = str('close')+str('END')
 
-  elif not index: break
-
   else:
     data = str('incorrect msg')+str('END')
   sock.sendto(data.encode('utf-8'), addr)  # echo
diff --git a/server/telnet_console.py b/server/telnet_console.py
new file mode 100644
index 0000000..b83447c
--- /dev/null
+++ b/server/telnet_console.py
@@ -0,0 +1,13 @@
+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
-- 
GitLab