Background: The recent project will be automated framework for rectification, the middle of the automation agent is involved in this piece, the original Automation agent service is written in C + +, often run in the system session, resulting in the agent can not execute remote command, search on Baidu, found Python has rpyc module , and then tried to see if the requirements were met. A use to find RPYC really is very powerful, a few lines of code easy to fix, so here with you to share under, also welcome suggestions and better methods.
Objective: On the remote actuator A, can use the command, on the B machine to query the corresponding information
On the code:
Client code:
1 #-*-coding:utf-8-*-2 ##############################################################3 #RPYC The call of the module4 #The purpose is to execute the cmd command on machine B and get the return value .5 #RPYC Client6 ##############################################################7 8 ImportRpyc9 Ten ImportOS, subprocess One A #set up a connection to the server # server IP to connect to, here is my native IP, I should be running on the server on the local, you can use localhost -conn = Rpyc.connect ("192.168.0.100", 6677) - the #Exposed_execcmd is a method provided by the service side -GETCMDRSP = CONN.ROOT.EXPOSED_EXECCMD ("dir") - PrintGETCMDRSP - +GETCMDRSP = CONN.ROOT.EXECGETCMDRSP ("tasklist | findstr python*") - PrintGETCMDRSP + A Print "\n\n\n\n*********************************************" at -GETCMDRSP = CONN.ROOT.SUBPROCESSCMD ("tasklist | findstr python*") - PrintGETCMDRSP - -GETCMDRSP = CONN.ROOT.SUBPROCESSCMDRSP ("tasklist | findstr python*") - PrintGETCMDRSP inConn.close ()
Service-Side code:
1 #-*-coding:utf-8-*-2 ##############################################################3 #RPYC The call of the module4 #The purpose is to execute the cmd command on machine B and get the return value .5 #RPYC Service Side6 ##############################################################7 8 fromRpycImportService9 fromRpyc.utils.serverImportThreadedserverTen ImportOS, subprocess One A classTest_threadedserver (Service): - #for the server, only the "exposed_*" can be called by the client, so the method to be provided to the client is "Exposed_" - the #returns 0 and 1; 0=true,1=false - defExposed_execcmd (self,cmd): - returnos.system (cmd) - + #returns the query result of the cmd command - defEXPOSED_EXECGETCMDRSP (self,cmd): + returnos.popen (cmd). Read () A at #returns 0 and 1; 0=true,1=false - defExposed_subprocesscmd (self,cmd): - returnsubprocess.call (cmd) - - #returns the query result of the cmd command - defexposed_subprocesscmdrsp (self, cmd): in returnSubprocess. Popen (cmd, shell=true, stdout=subprocess. PIPE, stderr=subprocess. STDOUT). Stdout.read () - toTest_server = Threadedserver (Test_threadedserver, port=6677, auto_register=False) +Test_server.start ()
Directly running the server code, will start a Python process or pythonw process in the background (as if it is related to the startup mode)
After the server starts, it listens for messages from Port 6677 (note the client and server port to correspond, the port number is arbitrary, as long as not occupied on the line)
: Basic functionality required for automation (can be compiled into EXE files and added to self-launcher)
RPYC Module Application: Execute commands on the remote and get query results