def _add(argv): global _gPshellServers global _gMaxLocalName global _gMaxRemoteName global _gMulticast global _gMaxMulticastKeyword global _gRemoteNameLabel global _gLocalNameLabel global _gKeywordLabel if (PshellServer.isHelp()): PshellServer.printf() PshellServer.showUsage() PshellServer.printf() PshellServer.printf(" where:") PshellServer.printf( " <localName> - Local logical name of the server, must be unique" ) PshellServer.printf( " <remoteServer> - Hostname or IP address of UDP server or name of UNIX server" ) PshellServer.printf( " <port> - UDP port number or 'unix' for UNIX server (can be omitted for UNIX)" ) PshellServer.printf( " <keyword> - Multicast group keyword, must be valid registered remote command" ) PshellServer.printf() elif (PshellServer.isSubString(argv[1], "server")): # default port port = PshellServer.UNIX if (len(argv) == 5): port = argv[4] if (not _isDuplicate(argv[2], argv[3], port)): if (len(argv[2]) > _gMaxLocalName): _gMaxLocalName = max(len(argv[2]), len(_gLocalNameLabel)) if (len(argv[3]) > _gMaxRemoteName): _gMaxRemoteName = max(len(argv[3]), len(_gRemoteNameLabel)) _gPshellServers.append({ "localName": argv[2], "remoteServer": argv[3], "port": port, "sid": PshellControl.connectServer(argv[2], argv[3], port, PshellControl.ONE_SEC * 5) }) PshellServer.addCommand( _controlServer, argv[2], "control the remote " + argv[2] + " process", "[<command> | ? | -h]", 0, 30, False) PshellServer._addTabCompletions() else: PshellServer.printf( "ERROR: Local name: %s, remote server: %s, port: %s already exists" % (argv[2], argv[3], argv[4])) elif (PshellServer.isSubString(argv[1], "multicast")): multicast = _getMulticast(argv[2]) if (multicast == None): # new keyword if (len(argv[2]) > _gMaxMulticastKeyword): _gMaxMulticastKeyword = max(len(argv[2]), len(_gKeywordLabel)) _gMulticast.append({"keyword": argv[2], "servers": []}) multicast = _gMulticast[-1] # add servers to this keyword for localName in argv[3:]: server = _getServer(localName) if (server != None): PshellControl.addMulticast(server["sid"], argv[2]) multicast["servers"].append(server) else: PshellServer.showUsage()
# need to set the first arg position to 0 so we can pass # through the exact command to our remote server for dispatching PshellServer._setFirstArgPos(0) # we tell the local server we are the special UDP/UNIX command # line client so it can process commands correctly and display # the correct banner information PshellServer._gPshellClient = True # supress the automatic invalid arg count message from the PshellControl.py # module so we can display the returned usage PshellControl._gSupressInvalidArgCountMessage = True # register our callback commands PshellServer.addCommand( _add, "add", "add a new remote server or multicast group entry", "{server <localName> <remoteServer> [<port>]} | {multicast <keyword> <localName1> [<localName2>...<localNameN>]}", 4, 30, False) PshellServer.addCommand(_show, "show", "show aggregated servers or multicast group info", "servers | multicast", 2, 2, True) PshellServer.addCommand( _multicast, "multicast", "send multicast command to registered server group", "<command>", 2, 30, False) # start our local pshell server PshellServer.startServer("pshellAggregator", PshellServer.LOCAL, PshellServer.BLOCKING)
elif (sys.argv[1] == "-local"): serverType = PshellServer.LOCAL else: showUsage() if len(sys.argv) == 3: PSHELL_DEMO_PORT = int(sys.argv[2]) else: showUsage() # register signal handlers so we can do a graceful termination and cleanup any system resources registerSignalHandlers() # register our callback commands, commands consist of single keyword only PshellServer.addCommand(function=helloWorld, command="helloWorld", description="command that prints out arguments", usage="[<arg1> ... <arg20>]", minArgs=0, maxArgs=20) # TCP or LOCAL servers don't need a keep-alive, so only add # this command for connectionless datagram type servers if ((serverType == PshellServer.UDP) or (serverType == PshellServer.UNIX)): PshellServer.addCommand( function=keepAlive, command="keepAlive", description="command to show client keep-alive ('C' client only)", usage="dots | bang | pound | wheel", minArgs=1, showUsage=False) PshellServer.addCommand(
traceFilterDemoSid = PshellControl.connectServer("traceFilterDemo", sys.argv[1], traceFilterDemoPort, PshellControl.ONE_SEC * 5) # add some multicast groups for our control sids, a multicast group is based # on the command's keyword PshellControl.addMulticast(pshellServerDemoSid, "trace") PshellControl.addMulticast(traceFilterDemoSid, "trace") PshellControl.addMulticast(pshellServerDemoSid, "test") PshellControl.addMulticast(traceFilterDemoSid, "test") # register our callback commands PshellServer.addCommand(pshellServerDemo, "pshellServerDemo", "control the remote pshellServerDemo process", "[<command> | ? | -h]", 0, 30, False) PshellServer.addCommand(traceFilterDemo, "traceFilterDemo", "control the remote traceFilterDemo process", "[<command> | ? | -h]", 0, 30, False) # add any "meta" commands here, meta commands can aggregate multiple discrete # pshell commands, either within one server or across multiple servers, into # one command PshellServer.addCommand(meta, "meta", "meta command, wraps multiple seperate functions", "<arg1> <arg2> <arg3>", 3, 3) # add an example command that uses the one-to-many multicast feature of # the control API
_gInteractive = True if (_gIsBroadcastAddr == False): # if not a broadcast server address, extract all the commands from # our unicast remote server and add them to our local server commandList = PshellControl.extractCommands(_gSid) if (len(commandList) > 0): commandList = commandList.split("\n") for command in commandList: splitCommand = command.split("-") if (len(splitCommand) >= 2): commandName = splitCommand[0].strip() description = splitCommand[1].strip() PshellServer.addCommand(_comandDispatcher, commandName, description, "[<arg1> ... <arg20>]", 0, 20) # configure our local server to interact with a remote server, we override the display settings # (i.e. prompt, server name, banner, title etc), to make it appear that our local server is really # a remote server _configureLocalServer() # now start our local server which will interact with a remote server via the pshell control machanism PshellServer.startServer("pshellServer", PshellServer.LOCAL, PshellServer.BLOCKING) else: print("PSHELL_ERROR: Could not connect to server: '%s:%s'" % (_gRemoteServer, _gPort))