Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 1 | # Copyright (C) 2012, 2013 Holger Hans Peter Freyther |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 2 | # Copyright (C) 2013 Katerina Barone-Adesi |
| 3 | # This program is free software: you can redistribute it and/or modify |
| 4 | # it under the terms of the GNU General Public License as published by |
| 5 | # the Free Software Foundation, either version 2 of the License, or |
| 6 | # (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public License |
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | # |
| 17 | # VTY helper code for OpenBSC |
| 18 | # |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 19 | import re |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 20 | import socket |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 21 | import sys, subprocess |
Neels Hofmeyr | 2bdab3d | 2017-02-27 00:58:19 +0100 | [diff] [blame] | 22 | import os |
Neels Hofmeyr | abd4b7d | 2017-02-27 01:03:44 +0100 | [diff] [blame] | 23 | import time |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 24 | |
Kat | a8ee6bb | 2013-04-05 17:06:30 +0200 | [diff] [blame] | 25 | """VTYInteract: interact with an osmocom vty |
| 26 | |
| 27 | Specify a VTY to connect to, and run commands on it. |
| 28 | Connections will be reestablished as necessary. |
| 29 | Methods: __init__, command, enabled_command, verify, w_verify""" |
| 30 | |
Neels Hofmeyr | 2bdab3d | 2017-02-27 00:58:19 +0100 | [diff] [blame] | 31 | debug_tcp_sockets = (os.getenv('OSMOPY_DEBUG_TCP_SOCKETS', '0') != '0') |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 32 | |
| 33 | def cmd(what): |
| 34 | print '\n> %s' % what |
| 35 | sys.stdout.flush() |
| 36 | subprocess.call(what, shell=True) |
| 37 | sys.stdout.flush() |
| 38 | sys.stderr.flush() |
| 39 | print '' |
| 40 | sys.stdout.flush() |
| 41 | |
| 42 | def print_used_tcp_sockets(): |
Neels Hofmeyr | cb320b8 | 2017-02-27 01:11:13 +0100 | [diff] [blame] | 43 | global debug_tcp_sockets |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 44 | if not debug_tcp_sockets: |
| 45 | return |
Holger Hans Peter Freyther | f41db1e | 2017-09-13 15:42:15 +0800 | [diff] [blame^] | 46 | cmd('ls -l /proc/' + str(os.getpid()) + '/fd'); |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 47 | cmd('ss -tn'); |
| 48 | cmd('ss -tln'); |
| 49 | cmd('ps xua | grep osmo'); |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 50 | |
| 51 | class VTYInteract(object): |
Kat | a8ee6bb | 2013-04-05 17:06:30 +0200 | [diff] [blame] | 52 | """__init__(self, name, host, port): |
| 53 | |
| 54 | name is the name the vty prints for commands, ie OpenBSC |
| 55 | host is the hostname to connect to |
| 56 | port is the port to connect on""" |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 57 | |
| 58 | all_sockets = [] |
| 59 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 60 | def __init__(self, name, host, port): |
Neels Hofmeyr | 93a808e | 2017-02-24 20:49:21 +0100 | [diff] [blame] | 61 | print_used_tcp_sockets() |
| 62 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 63 | self.name = name |
| 64 | self.host = host |
| 65 | self.port = port |
| 66 | |
| 67 | self.socket = None |
Jacob Erlbeck | 41b0d30 | 2013-08-30 18:28:05 +0200 | [diff] [blame] | 68 | self.norm_end = re.compile('\r\n%s(?:\(([\w-]*)\))?> $' % self.name) |
| 69 | self.priv_end = re.compile('\r\n%s(?:\(([\w-]*)\))?# $' % self.name) |
| 70 | self.last_node = '' |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 71 | |
Neels Hofmeyr | 4e64b88 | 2017-02-27 01:31:02 +0100 | [diff] [blame] | 72 | def _connect_socket(self): |
| 73 | if self.socket is not None: |
| 74 | return |
Neels Hofmeyr | abd4b7d | 2017-02-27 01:03:44 +0100 | [diff] [blame] | 75 | retries = 30 |
| 76 | took = 0 |
| 77 | while True: |
| 78 | took += 1 |
| 79 | try: |
| 80 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 81 | self.socket.setblocking(1) |
| 82 | self.socket.connect((self.host, self.port)) |
| 83 | except IOError: |
| 84 | retries -= 1 |
| 85 | if retries <= 0: |
| 86 | raise |
| 87 | # possibly the binary hasn't launched yet |
| 88 | if debug_tcp_sockets: |
| 89 | print "Connecting socket failed, retrying..." |
| 90 | time.sleep(.1) |
| 91 | continue |
| 92 | break |
| 93 | |
Neels Hofmeyr | 4e64b88 | 2017-02-27 01:31:02 +0100 | [diff] [blame] | 94 | if debug_tcp_sockets: |
| 95 | VTYInteract.all_sockets.append(self.socket) |
Neels Hofmeyr | abd4b7d | 2017-02-27 01:03:44 +0100 | [diff] [blame] | 96 | print "Socket: in %d tries, connected to %s:%d %r (%d sockets open)" % ( |
| 97 | took, self.host, self.port, self.socket, |
Neels Hofmeyr | 4e64b88 | 2017-02-27 01:31:02 +0100 | [diff] [blame] | 98 | len(VTYInteract.all_sockets)) |
| 99 | self.socket.recv(4096) |
| 100 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 101 | def _close_socket(self): |
Neels Hofmeyr | cb320b8 | 2017-02-27 01:11:13 +0100 | [diff] [blame] | 102 | global debug_tcp_sockets |
Neels Hofmeyr | e349320 | 2017-02-27 01:12:14 +0100 | [diff] [blame] | 103 | if self.socket is None: |
| 104 | return |
| 105 | |
| 106 | if debug_tcp_sockets: |
Neels Hofmeyr | 8e9f30f | 2017-02-27 01:17:22 +0100 | [diff] [blame] | 107 | try: |
| 108 | VTYInteract.all_sockets.remove(self.socket) |
| 109 | except ValueError: |
| 110 | pass |
Neels Hofmeyr | e349320 | 2017-02-27 01:12:14 +0100 | [diff] [blame] | 111 | print "Socket: closing %s:%d %r (%d sockets open)" % ( |
| 112 | self.host, self.port, self.socket, |
| 113 | len(VTYInteract.all_sockets)) |
| 114 | self.socket.close() |
| 115 | self.socket = None |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 116 | |
| 117 | def _is_end(self, text, ends): |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 118 | """ |
| 119 | >>> vty = VTYInteract('OsmoNAT', 'localhost', 9999) |
| 120 | >>> end = [vty.norm_end, vty.priv_end] |
| 121 | |
| 122 | Simple test |
| 123 | >>> text1 = 'abc\\r\\nOsmoNAT> ' |
| 124 | >>> vty._is_end(text1, end) |
| 125 | 11 |
| 126 | |
| 127 | Simple test with the enabled node |
| 128 | >>> text2 = 'abc\\r\\nOsmoNAT# ' |
| 129 | >>> vty._is_end(text2, end) |
| 130 | 11 |
| 131 | |
| 132 | Now the more complicated one |
| 133 | >>> text3 = 'abc\\r\\nOsmoNAT(config)# ' |
| 134 | >>> vty._is_end(text3, end) |
| 135 | 19 |
| 136 | |
| 137 | Now the more complicated one |
| 138 | >>> text4 = 'abc\\r\\nOsmoNAT(config-nat)# ' |
| 139 | >>> vty._is_end(text4, end) |
| 140 | 23 |
| 141 | |
| 142 | Now the more complicated one |
| 143 | >>> text5 = 'abc\\r\\nmoo' |
| 144 | >>> vty._is_end(text5, end) |
| 145 | 0 |
Jacob Erlbeck | 41b0d30 | 2013-08-30 18:28:05 +0200 | [diff] [blame] | 146 | |
| 147 | Check for node name extraction |
| 148 | >>> text6 = 'abc\\r\\nOsmoNAT(config-nat)# ' |
| 149 | >>> vty._is_end(text6, end) |
| 150 | 23 |
| 151 | >>> vty.node() |
| 152 | 'config-nat' |
| 153 | |
| 154 | Check for empty node name extraction |
| 155 | >>> text7 = 'abc\\r\\nOsmoNAT# ' |
| 156 | >>> vty._is_end(text7, end) |
| 157 | 11 |
| 158 | >>> vty.node() is None |
| 159 | True |
| 160 | |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 161 | """ |
Jacob Erlbeck | 41b0d30 | 2013-08-30 18:28:05 +0200 | [diff] [blame] | 162 | self.last_node = None |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 163 | for end in ends: |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 164 | match = end.search(text) |
| 165 | if match: |
Jacob Erlbeck | 41b0d30 | 2013-08-30 18:28:05 +0200 | [diff] [blame] | 166 | self.last_node = match.group(1) |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 167 | return match.end() - match.start() |
| 168 | return 0 |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 169 | |
| 170 | def _common_command(self, request, close=False, ends=None): |
Neels Hofmeyr | cb320b8 | 2017-02-27 01:11:13 +0100 | [diff] [blame] | 171 | global debug_tcp_sockets |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 172 | if not ends: |
| 173 | ends = [self.norm_end, self.priv_end] |
Neels Hofmeyr | 4e64b88 | 2017-02-27 01:31:02 +0100 | [diff] [blame] | 174 | |
| 175 | self._connect_socket() |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 176 | |
| 177 | # Now send the command |
| 178 | self.socket.send("%s\r" % request) |
| 179 | res = "" |
| 180 | end = "" |
| 181 | |
| 182 | # Unfortunately, timeout and recv don't always play nicely |
| 183 | while True: |
| 184 | data = self.socket.recv(4096) |
| 185 | res = "%s%s" % (res, data) |
| 186 | if not res: # yes, this is ugly |
| 187 | raise IOError("Failed to read data (did the app crash?)") |
| 188 | end = self._is_end(res, ends) |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 189 | if end > 0: |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 190 | break |
| 191 | |
| 192 | if close: |
| 193 | self._close_socket() |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 194 | return res[len(request) + 2: -end] |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 195 | |
Alexander Chemeris | 2f48313 | 2015-05-30 10:07:53 -0400 | [diff] [blame] | 196 | """A generator function yielding lines separated by delim. |
| 197 | Behaves similar to a file readlines() method. |
| 198 | |
| 199 | Example of use: |
| 200 | for line in vty.readlines(): |
| 201 | print line |
| 202 | """ |
| 203 | def readlines(self, recv_buffer=4096, delim='\n'): |
| 204 | buffer = '' |
| 205 | data = True |
| 206 | while data: |
| 207 | data = self.socket.recv(recv_buffer) |
| 208 | buffer += data |
| 209 | |
| 210 | while buffer.find(delim) != -1: |
| 211 | line, buffer = buffer.split('\n', 1) |
| 212 | yield line |
| 213 | return |
| 214 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 215 | # There's no close parameter, as close=True makes this useless |
| 216 | def enable(self): |
| 217 | self.command("enable") |
| 218 | |
| 219 | """Run a command on the vty""" |
Kat | a8ee6bb | 2013-04-05 17:06:30 +0200 | [diff] [blame] | 220 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 221 | def command(self, request, close=False): |
| 222 | return self._common_command(request, close) |
| 223 | |
| 224 | """Run enable, followed by another command""" |
| 225 | def enabled_command(self, request, close=False): |
| 226 | self.enable() |
| 227 | return self._common_command(request, close) |
| 228 | |
| 229 | """Verify, ignoring leading/trailing whitespace""" |
| 230 | # inspired by diff -w, though not identical |
| 231 | def w_verify(self, command, results, close=False, loud=True): |
| 232 | return self.verify(command, results, close, loud, lambda x: x.strip()) |
| 233 | |
| 234 | """Verify that a command has the expected results |
| 235 | |
| 236 | command = the command to verify |
| 237 | results = the expected results [line1, line2, ...] |
| 238 | close = True to close the socket after running the verify |
| 239 | loud = True to show what was expected and what actually happend, stdout |
| 240 | f = A function to run over the expected and actual results, before compare |
| 241 | |
| 242 | Returns True iff the expected and actual results match""" |
| 243 | def verify(self, command, results, close=False, loud=True, f=None): |
| 244 | res = self.command(command, close).split('\r\n') |
| 245 | if f: |
| 246 | res = map(f, res) |
| 247 | results = map(f, results) |
| 248 | |
| 249 | if loud: |
| 250 | if res != results: |
| 251 | print "Rec: %s\nExp: %s" % (res, results) |
| 252 | |
| 253 | return res == results |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 254 | |
Jacob Erlbeck | 41b0d30 | 2013-08-30 18:28:05 +0200 | [diff] [blame] | 255 | def node(self): |
| 256 | return self.last_node |
| 257 | |
Holger Hans Peter Freyther | 99bbea7 | 2013-06-25 08:17:59 +0200 | [diff] [blame] | 258 | if __name__ == "__main__": |
| 259 | import doctest |
| 260 | doctest.testmod() |