forked from mininet/mininet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
213 lines (169 loc) · 6.49 KB
/
cli.py
File metadata and controls
213 lines (169 loc) · 6.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
A simple command-line interface for Mininet.
The Mininet CLI provides a simple control console which
makes it easy to talk to nodes. For example, the command
mininet> h27 ifconfig
runs 'ifconfig' on host h27.
Having a single console rather than, for example, an xterm for each
node is particularly convenient for networks of any reasonable
size.
The CLI automatically substitutes IP addresses for node names,
so commands like
mininet> h2 ping h3
should work correctly and allow host h2 to ping host h3
Several useful commands are provided, including the ability to
list all nodes ('nodes'), to print out the network topology
('net') and to check connectivity ('pingall', 'pingpair')
and bandwidth ('iperf'.)
Bugs/limitations:
- Interactive commands are not supported at the moment
"""
from subprocess import call
from cmd import Cmd
from mininet.log import info, output, error
class CLI( Cmd ):
"Simple command-line interface to talk to nodes."
prompt = 'mininet> '
def __init__( self, mininet ):
self.mn = mininet
self.nodelist = self.mn.controllers + self.mn.switches + self.mn.hosts
self.nodemap = {} # map names to Node objects
for node in self.nodelist:
self.nodemap[ node.name ] = node
Cmd.__init__( self )
info( '*** Starting CLI:\n' )
while True:
try:
self.cmdloop()
break
except KeyboardInterrupt:
info( 'Interrupt\n' )
for node in self.nodelist:
waitForNode( node )
def emptyline( self ):
"Don't repeat last command when you hit return."
pass
# Disable pylint "Unused argument: 'arg's'" messages, as well as
# "method could be a function" warning, since each CLI function
# must have the same interface
# pylint: disable-msg=W0613,R0201
def do_help( self, args ):
"Describe available CLI commands."
Cmd.do_help( self, args )
helpStr = ( 'You may also send a command to a node using:\n'
' <node> command {args}\n'
'For example:\n'
' mininet> h0 ifconfig\n'
'\n'
'The interpreter automatically substitutes IP '
'addresses\n'
'for node names when a node is the first arg, so commands'
' like\n'
' mininet> h2 ping h3\n'
'should work.\n'
'\n'
'Interactive commands are not supported yet.\n' )
if args is "":
self.stdout.write( helpStr )
def do_nodes( self, args ):
"List all nodes."
nodes = ' '.join( [ node.name for node in sorted( self.nodelist ) ] )
output( 'available nodes are: \n%s\n' % nodes )
def do_net( self, args ):
"List network connections."
for switch in self.mn.switches:
output( switch.name, '<->' )
for intf in switch.intfs.values():
name = switch.connection[ intf ][ 1 ]
output( ' %s' % name )
output( '\n' )
def do_sh( self, args ):
"Run an external shell command"
call( args, shell=True )
# do_py() needs to catch any exception during eval()
# pylint: disable-msg=W0703
def do_py( self, args ):
"""Evaluate a Python expression.
Node names may be used, e.g.: h1.cmd('ls')"""
try:
result = eval( args, globals(), self.nodemap )
if not result:
return
elif isinstance( result, str ):
info( result + '\n' )
else:
info( repr( result ) + '\n' )
except Exception, e:
info( str( e ) + '\n' )
# pylint: enable-msg=W0703
def do_pingall( self, args ):
"Ping between all hosts."
self.mn.pingAll()
def do_pingpair( self, args ):
"Ping between first two hosts, useful for testing."
self.mn.pingPair()
def do_iperf( self, args ):
"Simple iperf TCP test between two hosts."
self.mn.iperf()
def do_iperfudp( self, args ):
"Simple iperf UDP test between two hosts."
udpBw = args[ 0 ] if len( args ) else '10M'
self.mn.iperfUdp( udpBw )
def do_intfs( self, args ):
"List interfaces."
for node in self.nodelist:
output( '%s: %s\n' %
( node.name, ' '.join( sorted( node.intfs.values() ) ) ) )
def do_dump( self, args ):
"Dump node info."
for node in self.nodelist:
output( '%s\n' % node )
def do_link( self, args ):
"Bring link(s) between two nodes up or down."
args = args.split()
if len(args) != 3:
error( 'invalid number of args: link end1 end2 [up down]\n' )
elif args[ 2 ] not in [ 'up', 'down' ]:
error( 'invalid type: link end1 end2 [up down]\n' )
else:
self.mn.configLinkStatus( *args )
def do_exit( self, args ):
"Exit"
return 'exited by user command'
def do_quit( self, args ):
"Exit"
return self.do_exit( args )
def do_EOF( self, args ):
"Exit"
return self.do_exit( args )
def default( self, line ):
"""Called on an input line when the command prefix is not recognized.
Overridden to run shell commands when a node is the first CLI argument.
Past the first CLI argument, node names are automatically replaced with
corresponding IP addrs."""
first, args, line = self.parseline( line )
if len(args) > 0 and args[ -1 ] == '\n':
args = args[ :-1 ]
rest = args.split( ' ' )
if first in self.nodemap:
node = self.nodemap[ first ]
# Substitute IP addresses for node names in command
rest = [ self.nodemap[ arg ].IP()
if arg in self.nodemap else arg
for arg in rest ]
rest = ' '.join( rest )
# Run cmd on node:
node.sendCmd( rest, printPid=True )
waitForNode( node )
else:
self.stdout.write( '*** Unknown syntax: %s\n' % line )
# pylint: enable-msg=W0613,R0201
# This function may be a candidate for util.py
def waitForNode( node ):
"Wait for a node to finish, and print its output."
while node.waiting:
try:
data = node.monitor()
info( '%s' % data )
except KeyboardInterrupt:
node.sendInt()