-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattach.py
65 lines (51 loc) · 1.69 KB
/
attach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import argparse
import os
from subprocess import Popen, PIPE
import re
def get_pid(node_name):
"""Get the system process ID for the node.
"""
node_pat = re.compile('.*bash .* mininet:{}'.format(node_name))
out, _ = Popen("ps aux".split(), stdout=PIPE).communicate()
for line in out.split('\n'):
match = node_pat.match(line)
if not match:
continue
pid = line.split()[1]
return pid
raise KeyError("No process found for {}".format(node_name))
def main():
"""Entry point for attach.
"""
parser = argparse.ArgumentParser("Connect to a mininet node.")
parser.add_argument('-n', '--node',
required=True,
help="The node's name (e.g., h1_1, R1, etc.)")
parser.add_argument('-d', '--daemon',
help="Connect directly to this FRR daemon.")
parser.add_argument('-c', '--cmd',
default=["sh"],
nargs="+",
help="Command to run on the node."
" Default is to start sh.")
args = parser.parse_args()
pid = get_pid(args.node)
cmd = ' '.join(args.cmd)
if args.daemon is not None:
port_dict = {
"zebrasrv": 2600,
"zebra": 2601,
"ripd": 2602,
"ripngd": 2603,
"ospfd": 2604,
"bgpd": 2605,
"ospf6d": 2606,
"ospfapi": 2607,
"isisd": 2608,
"staticd": 2616,
}
cmd = "telnet localhost {}".format(port_dict[args.daemon])
os.system("mnexec -a {} {}".format(pid, cmd))
if __name__ == '__main__':
main()