Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Saurab Dulal | 576a419 | 2020-08-25 00:55:22 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2020, The University of Memphis, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | import sys |
| 25 | from os.path import isfile |
| 26 | from subprocess import call |
| 27 | from mininet.cli import CLI |
Alexander Lane | ea2d5d6 | 2019-10-04 16:48:52 -0500 | [diff] [blame] | 28 | from mn_wifi.cli import CLI as CLI_wifi |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 29 | |
| 30 | sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa'] |
| 31 | scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa'] |
| 32 | devnull = open('/dev/null', 'w') |
| 33 | |
| 34 | def ssh(login, cmd): |
| 35 | rcmd = sshbase + [login, cmd] |
| 36 | call(rcmd, stdout=devnull, stderr=devnull) |
| 37 | |
| 38 | def scp(*args): |
| 39 | tmp = [] |
| 40 | for arg in args: |
| 41 | tmp.append(arg) |
| 42 | rcmd = scpbase + tmp |
| 43 | call(rcmd, stdout=devnull, stderr=devnull) |
| 44 | |
| 45 | def copyExistentFile(node, fileList, destination): |
| 46 | for f in fileList: |
| 47 | if isfile(f): |
| 48 | node.cmd('cp {} {}'.format(f, destination)) |
| 49 | break |
| 50 | if not isfile(destination): |
| 51 | fileName = destination.split('/')[-1] |
| 52 | raise IOError('{} not found in expected directory.'.format(fileName)) |
| 53 | |
| 54 | def popenGetEnv(node, envDict=None): |
| 55 | env = {} |
| 56 | homeDir = node.params['params']['homeDir'] |
| 57 | printenv = node.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8') |
| 58 | for var in printenv.split('\n'): |
| 59 | if var == '': |
| 60 | break |
| 61 | p = var.split('=') |
| 62 | env[p[0]] = p[1] |
| 63 | env['HOME'] = homeDir |
| 64 | |
| 65 | if envDict is not None: |
| 66 | for key, value in envDict.items(): |
| 67 | env[key] = str(value) |
| 68 | |
| 69 | return env |
| 70 | |
| 71 | def getPopen(host, cmd, envDict=None, **params): |
| 72 | return host.popen(cmd, cwd=host.params['params']['homeDir'], |
| 73 | env=popenGetEnv(host, envDict), **params) |
| 74 | |
| 75 | class MiniNDNCLI(CLI): |
| 76 | prompt = 'mini-ndn> ' |
| 77 | def __init__(self, mininet, stdin=sys.stdin, script=None): |
| 78 | CLI.__init__(self, mininet, stdin, script) |
Alexander Lane | ea2d5d6 | 2019-10-04 16:48:52 -0500 | [diff] [blame] | 79 | |
| 80 | class MiniNDNWifiCLI(CLI_wifi): |
| 81 | prompt = 'mini-ndn-wifi> ' |
| 82 | def __init__(self, mininet, stdin=sys.stdin, script=None): |
| 83 | CLI_wifi.__init__(self, mininet, stdin, script) |