3 # Usage: bootstrap_client.py <address>[:port] <command> ...
5 # <command> and the following arguments are concatenated (separated by a space)
6 # and passed to a shell on the server.
17 # interpret command line args
19 sys
.exit('Usage: ' + sys
.argv
[0] + ' <address>[:<port>] <command>')
22 portIndex
= address
.find(':')
24 port
= int(address
[portIndex
+ 1:])
25 address
= address
[:portIndex
]
27 commandToRun
= " ".join(sys
.argv
[2:])
29 # create sockets and connect to server
30 controlConnection
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
31 stdioConnection
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
32 stderrConnection
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
35 controlConnection
.connect((address
, port
))
36 stdioConnection
.connect((address
, port
))
37 stderrConnection
.connect((address
, port
))
38 except socket
.error
, msg
:
39 sys
.exit('Failed to connect to %s port %d: %s' % (address
, port
, msg
[1]))
41 # send command length and command
42 controlConnection
.send("%08d" % len(commandToRun
))
43 controlConnection
.send(commandToRun
)
45 # I/O loop. We quit when all sockets have been closed.
47 connections
= [controlConnection
, stdioConnection
, stderrConnection
, sys
.stdin
]
49 while connections
and (len(connections
) > 1 or not sys
.stdin
in connections
):
50 (readable
, writable
, exceptions
) = select
.select(connections
, [],
53 if sys
.stdin
in readable
:
54 data
= sys
.stdin
.readline(bufferSize
)
56 stdioConnection
.send(data
)
58 connections
.remove(sys
.stdin
)
59 stdioConnection
.shutdown(socket
.SHUT_WR
)
61 if stdioConnection
in readable
:
62 data
= stdioConnection
.recv(bufferSize
)
64 sys
.stdout
.write(data
)
66 connections
.remove(stdioConnection
)
68 if stderrConnection
in readable
:
69 data
= stderrConnection
.recv(bufferSize
)
71 sys
.stderr
.write(data
)
73 connections
.remove(stderrConnection
)
75 if controlConnection
in readable
:
76 data
= controlConnection
.recv(bufferSize
)
80 connections
.remove(controlConnection
)
82 # either an exit code has been sent or we consider this an error
84 sys
.exit(int(exitCode
))