summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2016-10-24 17:18:43 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-10-25 22:40:49 +0900
commit09e57c5993bd2fe30761a0b0c4c6de0bf982459c (patch)
tree0de91d6f140a0aa9e43e3ce5e1253d47274ee2ab
parent252591aaaea24cbdcb847cc4669a6618691a3137 (diff)
bgp/operator/ssh: Enable to quit gracefully
Currently, socket.error will be raised when 'quit' command is typed. This patch enables to quit SSH session without tracebacks. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/services/protocols/bgp/operator/ssh.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/ryu/services/protocols/bgp/operator/ssh.py b/ryu/services/protocols/bgp/operator/ssh.py
index 35d6a7b6..c228a899 100644
--- a/ryu/services/protocols/bgp/operator/ssh.py
+++ b/ryu/services/protocols/bgp/operator/ssh.py
@@ -110,6 +110,8 @@ class SshServer(paramiko.ServerInterface):
self.transport.add_server_key(host_key)
self.transport.start_server(server=self)
+ self.is_connected = True
+
# For pylint
self.buf = None
self.chan = None
@@ -350,11 +352,15 @@ class SshServer(paramiko.ServerInterface):
def _execute_cmd(self, cmds):
result, _ = self.root(cmds)
LOG.debug("result: %s", result)
+ if cmds[0] == 'quit':
+ self.is_connected = False
+ return result.status
self.prompted = False
self._startnewline()
output = result.value.replace('\n', '\n\r').rstrip()
self.chan.send(output)
self.prompted = True
+ self._startnewline()
return result.status
def end_session(self):
@@ -377,7 +383,7 @@ class SshServer(paramiko.ServerInterface):
self.chan.send(self.WELCOME)
self._startnewline()
- while True:
+ while self.is_connected:
c = self.chan.recv(1)
c = c.decode() # For Python3 compatibility
@@ -477,9 +483,14 @@ class SshServer(paramiko.ServerInterface):
self.history.insert(0, self.buf)
self.histindex = 0
self._execute_cmd(cmds)
+ else:
+ LOG.debug("no command is interpreted. "
+ "just start a new line.")
+ self._startnewline()
else:
- LOG.debug("blank buf. just start a new line.")
- self._startnewline()
+ LOG.debug("blank buf is detected. "
+ "just start a new line.")
+ self._startnewline()
LOG.debug("curpos: %d, buf: %s, prompted: %s", self.curpos,
self.buf, self.prompted)