2013-10-10 00:14:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2013-10-11 00:04:59 +00:00
|
|
|
# ultimately, this should provide an interactive cli for navigating confluent
|
|
|
|
# tree and doing console with a socket. <ESC>]0;<string><BELL> can be used to
|
|
|
|
# present info such as whether it is in a console or other mode and, if in
|
|
|
|
# console, how many other connections are live and looking
|
|
|
|
# this means 'wcons' simply needs to make a terminal run and we'll take care of
|
|
|
|
# the title while providing more info
|
|
|
|
# this also means the socket interface needs to have ways to convey more
|
|
|
|
# interesting pieces of data (like concurrent connection count)
|
|
|
|
# socket will probably switch to a TLV scheme:
|
|
|
|
# 32 bit TL, 8 bits of type code and 24 bit size
|
|
|
|
# type codes:
|
|
|
|
# 0: string data
|
|
|
|
# 1: json data
|
|
|
|
# 24 bit size allows the peer to avoid having to do any particular parsing to
|
|
|
|
# understand message boundaries (which is a significant burden on the xCAT
|
|
|
|
# protocol)
|
|
|
|
|
2013-10-12 14:44:46 +00:00
|
|
|
# When in a console client mode, will recognize two escape sequences by
|
|
|
|
# default:
|
|
|
|
# Ctrl-E, c, ?: mimick conserver behavior
|
|
|
|
# ctrl-]: go to interactive prompt (telnet escape, but not telnet prompt)
|
|
|
|
# esc-( would interfere with normal esc use too much
|
|
|
|
# ~ I will not use for now...
|
|
|
|
|
2013-10-10 23:23:06 +00:00
|
|
|
import fcntl
|
2014-02-09 15:43:26 +00:00
|
|
|
import getpass
|
2013-10-10 00:14:34 +00:00
|
|
|
import optparse
|
2013-10-10 23:23:06 +00:00
|
|
|
import os
|
2014-02-22 17:13:51 +00:00
|
|
|
import readline
|
2013-10-10 23:23:06 +00:00
|
|
|
import select
|
2014-02-09 22:35:49 +00:00
|
|
|
import shlex
|
2013-10-10 00:14:34 +00:00
|
|
|
import socket
|
|
|
|
import ssl
|
|
|
|
import sys
|
2013-10-12 14:44:46 +00:00
|
|
|
import termios
|
2013-10-10 23:23:06 +00:00
|
|
|
import tty
|
2013-10-10 00:14:34 +00:00
|
|
|
|
2014-02-22 23:12:21 +00:00
|
|
|
exitcode = 0
|
2014-02-10 23:28:45 +00:00
|
|
|
consoleonly = False
|
2014-02-09 22:35:49 +00:00
|
|
|
target = "/"
|
2014-02-09 15:43:26 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.realpath(os.path.join(path, '..'))
|
|
|
|
sys.path.append(path)
|
|
|
|
|
|
|
|
import confluent.common.tlvdata as tlvdata
|
|
|
|
|
2013-10-14 13:21:55 +00:00
|
|
|
SO_PASSCRED = 16
|
2013-10-12 14:44:46 +00:00
|
|
|
conserversequence = '\x05c' # ctrl-e, c
|
|
|
|
|
|
|
|
oldtcattr = termios.tcgetattr(sys.stdin.fileno())
|
|
|
|
server = None
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def prompt():
|
2014-02-22 17:13:51 +00:00
|
|
|
try:
|
|
|
|
return raw_input(target + ' -> ')
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print ""
|
|
|
|
return ""
|
|
|
|
except EOFError: # ctrl-d
|
|
|
|
print("exit")
|
|
|
|
return "exit"
|
|
|
|
# sys.stdout.write(target + ' -> ')
|
|
|
|
# sys.stdout.flush()
|
|
|
|
# username = raw_input("Name: ")
|
|
|
|
|
|
|
|
valid_commands = [
|
|
|
|
'start',
|
|
|
|
'cd',
|
|
|
|
'show',
|
|
|
|
'set',
|
|
|
|
'create',
|
|
|
|
]
|
|
|
|
|
|
|
|
candidates = None
|
|
|
|
|
|
|
|
def completer(text, state):
|
|
|
|
try:
|
|
|
|
return rcompleter(text, state)
|
|
|
|
except:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
def rcompleter(text, state):
|
|
|
|
global candidates
|
|
|
|
global valid_commands
|
|
|
|
cline = readline.get_line_buffer()
|
|
|
|
if len(text):
|
|
|
|
cline = cline[:-len(text)]
|
|
|
|
args = shlex.split(cline, posix=True)
|
|
|
|
currpos = len(args)
|
|
|
|
if currpos and cline[-1] == ' ':
|
2014-02-22 19:43:12 +00:00
|
|
|
lastarg = ''
|
2014-02-22 17:13:51 +00:00
|
|
|
currpos += 1
|
2014-02-22 22:31:08 +00:00
|
|
|
elif currpos:
|
2014-02-22 19:43:12 +00:00
|
|
|
lastarg = args[-1]
|
2014-02-22 17:13:51 +00:00
|
|
|
if currpos <= 1:
|
|
|
|
foundcount = 0
|
|
|
|
for cmd in valid_commands:
|
|
|
|
if cmd.startswith(text):
|
|
|
|
if foundcount == state:
|
|
|
|
return cmd
|
|
|
|
else:
|
|
|
|
foundcount += 1
|
2014-02-22 19:43:12 +00:00
|
|
|
candidates = None
|
2014-02-22 17:13:51 +00:00
|
|
|
return None
|
2014-02-22 17:24:22 +00:00
|
|
|
cmd = args[0]
|
2014-02-22 17:13:51 +00:00
|
|
|
if candidates is None:
|
|
|
|
candidates = []
|
2014-02-22 19:43:12 +00:00
|
|
|
targpath = fullpath_target(lastarg)
|
2014-02-22 17:13:51 +00:00
|
|
|
for res in send_request('retrieve', targpath, server):
|
|
|
|
if 'item' in res: # a link relation
|
|
|
|
if type(res['item']) == dict:
|
|
|
|
candidates.append(res['item']["href"])
|
|
|
|
else:
|
|
|
|
for item in res['item']:
|
|
|
|
candidates.append(item["href"])
|
|
|
|
foundcount = 0
|
|
|
|
for elem in candidates:
|
2014-02-22 17:24:22 +00:00
|
|
|
if cmd == 'cd' and elem[-1] != '/':
|
|
|
|
continue
|
2014-02-22 17:13:51 +00:00
|
|
|
if elem.startswith(text):
|
|
|
|
if foundcount == state:
|
|
|
|
return elem
|
|
|
|
else:
|
|
|
|
foundcount += 1
|
2014-02-22 19:43:12 +00:00
|
|
|
candidates = None
|
2014-02-22 17:13:51 +00:00
|
|
|
return None
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2013-10-10 00:14:34 +00:00
|
|
|
def parseservervalue(serverstring):
|
|
|
|
if serverstring.find(']:') != -1:
|
|
|
|
server, port = serverstring[1:].split(']:')
|
|
|
|
elif serverstring[0] == '[':
|
|
|
|
server = serverstring[1:-1]
|
|
|
|
port = 4001
|
|
|
|
elif -1 != opts.server.find(':'):
|
|
|
|
server, port = opts.server.split(":")
|
|
|
|
else:
|
|
|
|
server = serverstring
|
|
|
|
port = 4001
|
|
|
|
return (server, port)
|
|
|
|
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def parse_command(command):
|
|
|
|
args = shlex.split(command, posix=True)
|
|
|
|
return args
|
2014-02-22 19:43:12 +00:00
|
|
|
|
2014-02-10 00:26:48 +00:00
|
|
|
|
|
|
|
currchildren = None
|
|
|
|
|
2014-02-23 01:22:23 +00:00
|
|
|
def send_request(operation, path, server, parameters=None):
|
|
|
|
payload = {'operation': operation, 'path': path}
|
|
|
|
if parameters is not None:
|
|
|
|
payload['parameters'] = parameters
|
|
|
|
tlvdata.send_tlvdata(server, payload)
|
2014-02-10 00:26:48 +00:00
|
|
|
result = tlvdata.recv_tlvdata(server)
|
|
|
|
while '_requestdone' not in result:
|
|
|
|
yield result
|
|
|
|
result = tlvdata.recv_tlvdata(server)
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
def do_command(command, server):
|
2014-02-22 23:12:21 +00:00
|
|
|
global exitcode
|
2014-02-09 22:35:49 +00:00
|
|
|
global target
|
2014-02-10 23:28:45 +00:00
|
|
|
global currconsole
|
2014-02-10 00:26:48 +00:00
|
|
|
global currchildren
|
2014-02-22 23:12:21 +00:00
|
|
|
exitcode = 0
|
2014-02-09 22:35:49 +00:00
|
|
|
command = command.lower()
|
|
|
|
argv = parse_command(command)
|
|
|
|
if len(argv) == 0:
|
|
|
|
return
|
2014-02-23 01:10:13 +00:00
|
|
|
if argv[0] == 'exit':
|
2014-02-09 22:35:49 +00:00
|
|
|
server.close()
|
|
|
|
sys.exit(0)
|
2014-02-23 01:10:13 +00:00
|
|
|
elif argv[0] == 'cd':
|
2014-02-09 22:35:49 +00:00
|
|
|
otarget = target
|
2014-02-23 00:28:41 +00:00
|
|
|
if len(argv) > 1:
|
|
|
|
target = fullpath_target(argv[1], forcepath=True)
|
|
|
|
else: # cd by itself, go 'home'
|
|
|
|
target = '/'
|
2014-02-10 00:26:48 +00:00
|
|
|
for res in send_request('retrieve', target, server):
|
2014-02-22 23:12:21 +00:00
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
2014-02-10 00:26:48 +00:00
|
|
|
if 'error' in res:
|
2014-02-22 23:12:21 +00:00
|
|
|
sys.stderr.write(target + ': ' + res['error'] + '\n')
|
2014-02-10 00:26:48 +00:00
|
|
|
target = otarget
|
2014-02-22 23:12:21 +00:00
|
|
|
elif argv[0] in ('cat', 'show', 'ls', 'dir'):
|
2014-02-11 00:36:18 +00:00
|
|
|
if len(argv) > 1:
|
|
|
|
targpath = fullpath_target(argv[1])
|
|
|
|
else:
|
|
|
|
targpath = target
|
|
|
|
for res in send_request('retrieve', targpath, server):
|
|
|
|
if 'item' in res: # a link relation
|
|
|
|
if type(res['item']) == dict:
|
|
|
|
print res['item']["href"]
|
|
|
|
else:
|
|
|
|
for item in res['item']:
|
|
|
|
print item["href"]
|
|
|
|
else: # generic attributes to list
|
|
|
|
for key in res.iterkeys():
|
2014-02-23 00:12:42 +00:00
|
|
|
notes = []
|
2014-02-22 19:18:20 +00:00
|
|
|
if res[key] is None:
|
2014-02-23 00:12:42 +00:00
|
|
|
attrstr = '%s=""' % key
|
|
|
|
elif type(res[key]) == list:
|
|
|
|
attrstr = '%s=["%s"]' % (key, '","'.join(res[key]))
|
|
|
|
elif 'value' in res[key] and res[key]['value'] is not None:
|
|
|
|
attrstr = '%s="%s"' % (key, res[key]['value'])
|
|
|
|
elif 'value' in res[key] and res[key]['value'] is None:
|
|
|
|
attrstr = '%s=""' % key
|
2014-02-11 00:36:18 +00:00
|
|
|
else:
|
|
|
|
if 'isset' in res[key] and res[key]['isset']:
|
2014-02-23 00:12:42 +00:00
|
|
|
attrstr = '%s="********"' % key
|
2014-02-11 00:36:18 +00:00
|
|
|
else:
|
2014-02-23 00:12:42 +00:00
|
|
|
attrstr = '%s=""' % key
|
|
|
|
if res[key] is not None and 'inheritedfrom' in res[key]:
|
|
|
|
notes.append('Inherited from %s' % res[key]['inheritedfrom'])
|
|
|
|
if res[key] is not None and 'expression' in res[key]:
|
|
|
|
notes.append('Derived from expression "%s"' % res[key]['expression'])
|
|
|
|
notestr = '(' + ', '.join(notes) + ')'
|
|
|
|
output = '{0:<40} {1:>39}'.format(attrstr, notestr)
|
|
|
|
print output
|
2014-02-22 23:12:21 +00:00
|
|
|
elif argv[0] == 'start':
|
2014-02-10 14:16:29 +00:00
|
|
|
targpath = fullpath_target(argv[1])
|
2014-02-10 23:28:45 +00:00
|
|
|
currconsole = targpath
|
2014-02-10 14:16:29 +00:00
|
|
|
tlvdata.send_tlvdata(server, {'operation': 'start', 'path': targpath})
|
|
|
|
status = tlvdata.recv_tlvdata(server)
|
|
|
|
if 'error' in status:
|
2014-02-22 23:12:21 +00:00
|
|
|
if 'errorcode' in status:
|
|
|
|
exitcode = status['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + status['error'] + '\n')
|
2014-02-10 14:16:29 +00:00
|
|
|
return
|
|
|
|
print '[console session started]'
|
|
|
|
startconsole()
|
|
|
|
return
|
2014-02-23 01:10:13 +00:00
|
|
|
elif argv[0] == 'set':
|
|
|
|
setvalues(argv[1:])
|
2014-02-22 23:12:21 +00:00
|
|
|
else:
|
|
|
|
sys.stderr.write("%s: command not found...\n" % argv[0])
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-02-23 01:10:13 +00:00
|
|
|
def setvalues(attribs):
|
|
|
|
if '=' in attribs[0]: # going straight to attribute
|
|
|
|
resource = attribs[0][:attribs[0].index("=")]
|
2014-02-23 02:22:45 +00:00
|
|
|
if '/' in resource:
|
|
|
|
lastslash = resource.rindex('/')
|
|
|
|
attribs[0] = attribs[0][lastslash + 1:]
|
2014-02-23 01:10:13 +00:00
|
|
|
else: # an actual resource
|
|
|
|
resource = attribs[0]
|
|
|
|
attribs = attribs[1:]
|
|
|
|
keydata = {}
|
|
|
|
for attrib in attribs:
|
|
|
|
if '=' not in attrib:
|
|
|
|
sys.stderr.write("Invalid syntax %s" % attrib)
|
|
|
|
return
|
|
|
|
key = attrib[:attrib.index("=")]
|
|
|
|
value = attrib[attrib.index("=") + 1:]
|
|
|
|
keydata[key] = value
|
|
|
|
targpath = fullpath_target(resource)
|
2014-02-23 01:22:23 +00:00
|
|
|
for res in send_request('update', targpath, server, keydata):
|
|
|
|
if 'error' in res:
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
sys.stderr.write('Error: ' + res['error'] + '\n')
|
2014-02-23 01:10:13 +00:00
|
|
|
|
|
|
|
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-02-11 00:36:18 +00:00
|
|
|
def fullpath_target(path, forcepath=False):
|
2014-02-09 22:35:49 +00:00
|
|
|
global target
|
2014-02-22 19:43:12 +00:00
|
|
|
if path == '':
|
|
|
|
return target
|
2014-02-09 22:35:49 +00:00
|
|
|
pathcomponents = path.split("/")
|
2014-02-22 22:31:08 +00:00
|
|
|
if pathcomponents[-1] == "": # preserve path
|
|
|
|
forcepath=True
|
2014-02-09 22:35:49 +00:00
|
|
|
if pathcomponents[0] == "": # absolute path
|
2014-02-10 14:16:29 +00:00
|
|
|
ntarget = path
|
2014-02-09 22:35:49 +00:00
|
|
|
else:
|
|
|
|
targparts = target.split("/")[:-1]
|
|
|
|
for component in pathcomponents:
|
|
|
|
if component in ('.',''): # ignore these
|
|
|
|
continue
|
|
|
|
elif component == '..':
|
|
|
|
if len(targparts) > 0:
|
|
|
|
del targparts[-1]
|
|
|
|
else:
|
|
|
|
targparts.append(component)
|
2014-02-22 22:31:08 +00:00
|
|
|
if forcepath and targparts[-1] != "":
|
2014-02-11 00:36:18 +00:00
|
|
|
targparts.append('')
|
2014-02-10 14:16:29 +00:00
|
|
|
ntarget = '/'.join(targparts)
|
2014-02-11 00:36:18 +00:00
|
|
|
if forcepath and (len(ntarget) == 0 or ntarget[-1] != '/'):
|
2014-02-10 14:16:29 +00:00
|
|
|
ntarget += '/'
|
|
|
|
return ntarget
|
2014-02-09 22:35:49 +00:00
|
|
|
|
|
|
|
def startconsole():
|
2014-02-10 14:16:29 +00:00
|
|
|
global inconsole
|
2014-02-09 22:35:49 +00:00
|
|
|
tty.setraw(sys.stdin.fileno())
|
2014-02-22 17:13:51 +00:00
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK)
|
2014-02-10 14:16:29 +00:00
|
|
|
inconsole = True
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2013-10-12 14:44:46 +00:00
|
|
|
def exit(code=0):
|
2014-02-10 23:28:45 +00:00
|
|
|
global consoleonly
|
|
|
|
global inconsole
|
|
|
|
global currconsole
|
2014-02-22 17:13:51 +00:00
|
|
|
currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK)
|
2013-10-12 14:44:46 +00:00
|
|
|
termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, oldtcattr)
|
2014-02-10 23:28:45 +00:00
|
|
|
if consoleonly:
|
|
|
|
server.shutdown(socket.SHUT_RDWR)
|
|
|
|
server.close()
|
|
|
|
sys.exit(code)
|
|
|
|
else:
|
|
|
|
tlvdata.send_tlvdata(server, {'operation': 'stop', 'path': currconsole})
|
|
|
|
inconsole = False
|
2013-10-12 14:44:46 +00:00
|
|
|
|
|
|
|
def conserver_command(fh, command):
|
|
|
|
while not command:
|
|
|
|
ready, _, _ = select.select((fh,), (), (), 1)
|
|
|
|
if ready:
|
|
|
|
command += fh.read()
|
|
|
|
if command[0] == '.':
|
|
|
|
print("disconnect]\r")
|
|
|
|
exit()
|
|
|
|
elif command[0] == '?':
|
|
|
|
print("help]\r")
|
|
|
|
print(". disconnect\r")
|
|
|
|
print("<cr> abort command\r")
|
|
|
|
elif command[0] == '\x0d':
|
|
|
|
print("ignored]\r")
|
|
|
|
else: #not a command at all..
|
|
|
|
print("unknown -- use '?']\r")
|
|
|
|
|
|
|
|
|
|
|
|
def check_escape_seq(input, fh):
|
|
|
|
while conserversequence.startswith(input):
|
|
|
|
if input.startswith(conserversequence): # We have full sequence
|
|
|
|
sys.stdout.write("[")
|
|
|
|
sys.stdout.flush()
|
|
|
|
return conserver_command(fh, input[len(conserversequence):])
|
|
|
|
ready, _, _ = select.select((fh,), (), (), 3)
|
|
|
|
if not ready: # 3 seconds of no typing
|
|
|
|
break
|
|
|
|
input += fh.read()
|
|
|
|
return input
|
|
|
|
|
|
|
|
|
2013-10-14 13:21:55 +00:00
|
|
|
def connect_unix_server(sockpath):
|
|
|
|
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
server.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1)
|
|
|
|
server.connect(sockpath)
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
2013-10-10 00:14:34 +00:00
|
|
|
def connect_tls_server(serverstring):
|
|
|
|
host, port = parseservervalue(serverstring)
|
2013-10-14 13:21:55 +00:00
|
|
|
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
|
|
|
|
socket.SOCK_STREAM):
|
2013-10-10 00:14:34 +00:00
|
|
|
af, socktype, proto, cononname, sa = res
|
|
|
|
try:
|
|
|
|
server = socket.socket(af, socktype, proto)
|
2014-02-22 19:04:43 +00:00
|
|
|
server.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
2013-10-10 00:14:34 +00:00
|
|
|
except:
|
|
|
|
server = None
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
server.settimeout(5)
|
|
|
|
server.connect(sa)
|
|
|
|
except:
|
|
|
|
server.close()
|
|
|
|
server = None
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
if server is None:
|
|
|
|
sys.stderr.write("Failed to connect to %s\n" % serverstring)
|
|
|
|
sys.exit(1)
|
|
|
|
secserver = ssl.wrap_socket(server)
|
|
|
|
return secserver
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("-s", "--server", dest="server",
|
|
|
|
help="TLS server to connect to", metavar="SERVER:PORT")
|
|
|
|
parser.add_option("-u", "--unixsocket", dest="unixsock",
|
|
|
|
help="TLS server to connect to", metavar="UNIXDOMAINSOCKET")
|
|
|
|
opts, args = parser.parse_args()
|
|
|
|
if opts.server: # going over a TLS network
|
|
|
|
server = connect_tls_server(opts.server)
|
2013-10-14 13:21:55 +00:00
|
|
|
elif opts.unixsock:
|
|
|
|
server = connect_unix_server(opts.unixsock)
|
2014-02-11 00:41:06 +00:00
|
|
|
elif os.path.exists("/var/run/confluent/api.sock"):
|
|
|
|
server = connect_unix_server("/var/run/confluent/api.sock")
|
2013-10-10 00:14:34 +00:00
|
|
|
|
|
|
|
#Next stop, reading and writing from whichever of stdin and server goes first.
|
|
|
|
#see pyghmi code for solconnect.py
|
2013-10-10 23:23:06 +00:00
|
|
|
|
2014-02-09 15:43:26 +00:00
|
|
|
banner = tlvdata.recv_tlvdata(server)
|
|
|
|
authinfo = tlvdata.recv_tlvdata(server)
|
|
|
|
while authinfo['authpassed'] != 1:
|
|
|
|
username = raw_input("Name: ")
|
2014-02-22 17:13:51 +00:00
|
|
|
readline.clear_history()
|
2014-02-09 15:43:26 +00:00
|
|
|
passphrase = getpass.getpass("Passphrase: ")
|
|
|
|
tlvdata.send_tlvdata(server,
|
|
|
|
{'username': username, 'passphrase': passphrase})
|
|
|
|
authinfo = tlvdata.recv_tlvdata(server)
|
2013-10-12 14:44:46 +00:00
|
|
|
# clear on start can help with readable of TUI, but it
|
|
|
|
# can be annoying, so for now don't do it.
|
|
|
|
# sys.stdout.write('\x1b[H\x1b[J')
|
|
|
|
# sys.stdout.flush()
|
2014-02-09 22:35:49 +00:00
|
|
|
|
2014-02-22 17:13:51 +00:00
|
|
|
readline.parse_and_bind("tab: complete")
|
2014-02-22 22:39:05 +00:00
|
|
|
readline.parse_and_bind("set bell-style none")
|
2014-02-22 17:13:51 +00:00
|
|
|
readline.set_completer(completer)
|
2013-10-12 14:44:46 +00:00
|
|
|
doexit = False
|
2014-02-09 22:35:49 +00:00
|
|
|
inconsole = False
|
|
|
|
pendingcommand = ""
|
2014-02-10 14:50:17 +00:00
|
|
|
if len(args) == 1: # a node name, go straight to trying to console
|
2014-02-10 23:28:45 +00:00
|
|
|
consoleonly = True
|
2014-02-10 14:50:17 +00:00
|
|
|
do_command("start /node/%s/console/session" % args[0], server)
|
2013-10-12 14:44:46 +00:00
|
|
|
while not doexit:
|
2014-02-22 17:13:51 +00:00
|
|
|
if inconsole:
|
|
|
|
rdylist, _, _ = select.select((sys.stdin, server), (), (), 60)
|
|
|
|
for fh in rdylist:
|
|
|
|
if fh == server:
|
|
|
|
# this only should get called in the
|
|
|
|
# case of a console session
|
|
|
|
# each command should slurp up all relevant
|
|
|
|
# recv_tlvdata potential
|
|
|
|
#fh.read()
|
|
|
|
try:
|
|
|
|
data = tlvdata.recv_tlvdata(fh)
|
|
|
|
except Exception:
|
|
|
|
data = None
|
|
|
|
if type(data) == dict:
|
|
|
|
print repr(data)
|
|
|
|
continue
|
|
|
|
if data is not None:
|
|
|
|
sys.stdout.write(data)
|
|
|
|
sys.stdout.flush()
|
|
|
|
else:
|
|
|
|
doexit = True
|
|
|
|
sys.stdout.write("\r\n[remote disconnected]\r\n")
|
|
|
|
break
|
2013-10-10 23:23:06 +00:00
|
|
|
else:
|
2014-02-09 22:35:49 +00:00
|
|
|
input = fh.read()
|
|
|
|
input = check_escape_seq(input, fh)
|
|
|
|
if input:
|
|
|
|
tlvdata.send_tlvdata(server, input)
|
2014-02-22 17:13:51 +00:00
|
|
|
else:
|
|
|
|
command = prompt()
|
|
|
|
do_command(command, server)
|
2014-02-11 00:52:37 +00:00
|
|
|
consoleonly = True
|
2013-10-12 19:04:26 +00:00
|
|
|
exit()
|