Statistics
| Branch: | Tag: | Revision:

mininet / mininet / term.py @ ab8c4e91

History | View | Annotate | Download (2.71 KB)

1 15b482e3 Brandon Heller
"""
2
Terminal creation and cleanup.
3 a0f69d98 Bob Lantz
Utility functions to run a terminal (connected via socat(1)) on each host.
4 15b482e3 Brandon Heller

5 a0f69d98 Bob Lantz
Requires socat(1) and xterm(1).
6 15b482e3 Brandon Heller
Optionally uses gnome-terminal.
7
"""
8
9 a0f69d98 Bob Lantz
from os import environ
10 15b482e3 Brandon Heller
11
from mininet.log import error
12 a0f69d98 Bob Lantz
from mininet.util import quietRun, errRun
13 15b482e3 Brandon Heller
14 a0f69d98 Bob Lantz
def tunnelX11( node, display=None):
15
    """Create an X11 tunnel from node:6000 to the root host
16
       display: display on root host (optional)
17
       returns: node $DISPLAY, Popen object for tunnel"""
18 4316be95 Brian O'Connor
    if display is None and 'DISPLAY' in environ:
19 a0f69d98 Bob Lantz
        display = environ[ 'DISPLAY' ]
20 4316be95 Brian O'Connor
    if display is None:
21
        error( "Error: Cannot connect to display\n" )
22
        return None, None
23 a0f69d98 Bob Lantz
    host, screen = display.split( ':' )
24
    # Unix sockets should work
25
    if not host or host == 'unix':
26
        # GDM3 doesn't put credentials in .Xauthority,
27
        # so allow root to just connect
28
        quietRun( 'xhost +si:localuser:root' )
29
        return display, None
30
    else:
31
        # Create a tunnel for the TCP connection
32 477e84ad Bob Lantz
        port = 6000 + int( float( screen ) )
33 a0f69d98 Bob Lantz
        connection = r'TCP\:%s\:%s' % ( host, port )
34 477e84ad Bob Lantz
        cmd = [ "socat", "TCP-LISTEN:%d,fork,reuseaddr" % port,
35 7a3159c9 Bob Lantz
                "EXEC:'mnexec -a 1 socat STDIO %s'" % connection ]
36 a0f69d98 Bob Lantz
    return 'localhost:' + screen, node.popen( cmd )
37 82b72072 Bob Lantz
38 5b818ad7 Joseph Beshay
def makeTerm( node, title='Node', term='xterm', display=None, cmd='bash'):
39 a0f69d98 Bob Lantz
    """Create an X11 tunnel to the node and start up a terminal.
40 15b482e3 Brandon Heller
       node: Node object
41
       title: base title
42 de901ef0 Bob Lantz
       term: 'xterm' or 'gterm'
43 a0f69d98 Bob Lantz
       returns: two Popen objects, tunnel and terminal"""
44 3660e6d0 cody burkard
    title = '"%s: %s"' % ( title, node.name )
45 15b482e3 Brandon Heller
    if not node.inNamespace:
46
        title += ' (root)'
47 de901ef0 Bob Lantz
    cmds = {
48 a0f69d98 Bob Lantz
        'xterm': [ 'xterm', '-title', title, '-display' ],
49
        'gterm': [ 'gnome-terminal', '--title', title, '--display' ]
50 de901ef0 Bob Lantz
    }
51
    if term not in cmds:
52 15b482e3 Brandon Heller
        error( 'invalid terminal type: %s' % term )
53
        return
54 a0f69d98 Bob Lantz
    display, tunnel = tunnelX11( node, display )
55 4316be95 Brian O'Connor
    if display is None:
56
        return []
57 c5779dee Bob Lantz
    term = node.popen( cmds[ term ] +
58
                       [ display, '-e', 'env TERM=ansi %s' % cmd ] )
59 a0f69d98 Bob Lantz
    return [ tunnel, term ] if tunnel else [ term ]
60 15b482e3 Brandon Heller
61 e3d07bc1 Bob Lantz
def runX11( node, cmd ):
62
    "Run an X11 client on a node"
63 33e39a24 Bob Lantz
    _display, tunnel = tunnelX11( node )
64 4316be95 Brian O'Connor
    if _display is None:
65
        return []
66 e3d07bc1 Bob Lantz
    popen = node.popen( cmd )
67
    return [ tunnel, popen ]
68
69 15b482e3 Brandon Heller
def cleanUpScreens():
70 a0f69d98 Bob Lantz
    "Remove moldy socat X11 tunnels."
71
    errRun( "pkill -9 -f mnexec.*socat" )
72 15b482e3 Brandon Heller
73 f583900d Bob Lantz
def makeTerms( nodes, title='Node', term='xterm' ):
74 15b482e3 Brandon Heller
    """Create terminals.
75
       nodes: list of Node objects
76
       title: base title for each
77 a0f69d98 Bob Lantz
       returns: list of created tunnel/terminal processes"""
78
    terms = []
79
    for node in nodes:
80
        terms += makeTerm( node, title, term )
81
    return terms