Sometimes we need to remotely run programs on the Windows system on Linux. Method 1: Through the winrm module in python, the prerequisite is to set up the winrm service in advance. Please search Baidu for how to set it up. The winRM service is the remote management service of PowerShell under Windows Server. The Python script operates the windows command line by connecting to the winRM module. import winrm def cmd_views(ip,cmd_comand): win = winrm.Session('http://'+ip+':5985/wsman', auth=('user', 'password'))#Parameters are username and password r = win.run_cmd(cmd_comand) # Execute cmd command = return r.std_out # Print the obtained information ip="xxx.xxx.xx.xx" cmd_comand=r"ipconfig"#Run command a=cmd_views(ip, cmd_comand) print(cmd_comand) print(type(a)) print(a) After my test, this module can only execute some simple commands, that is, the kind of commands that can basically respond to results immediately after input. If you encounter something slightly more complicated, the process will crash. Method 2: The operation is performed through the telnetlib library in Python. The prerequisite is to set the telnet settings in the windows system. 1. Install the telnet client and server. 2 Configure telnet user permissions. If you don’t know how to do it, search on Baidu and set it up yourself. # -- coding: utf-8 -- import telnetlib,time def telnetlib_views(ipaddress,user,password,cmdname): tn = telnetlib.Telnet(ipaddress) a=tn.read_until(b'login:') tn.write(user.encode('ascii') + b'\r\n') tn.read_until(b'password:') time.sleep(5) tn.write(password.encode('ascii') + b'\r\n') time.sleep(2) tn.write(cmdname.encode('ascii') + b'\r\n') tn.close() cmdname=r'ifconfig'#Run command telnetlib_views(ipaddress="xxx.xxx.xxx.xxx", user="xxx", password="xxxx",cmdname=cmdname) Wait for the command call to complete and the program to end. Method 3 Using the wmi module, the defect can only be solved through windows-windows, linux-windows does not work, and linux-related modules cannot be installed. import wmi def sys_version(ipaddress, user, password,cmdname): conn = wmi.WMI(computer=ipaddress, user=user, password=password) try: cmd_callbat = r"cmd /c call %s" % cmdname print("The currently executed bat command is:",cmd_callbat) conn.Win32_Process.Create(CommandLine=cmd_callbat) except Exception as e: print(e) cmdname= r"xxx.bat"#Run commandsys_version(ipaddress="xxx.xx.xx.xx", user="xx", password="xxx",cmdname=cmdname)# The command is called and the program ends without waiting. This is different from method 2. The defect is that the library cannot be installed on Linux. The application scenario is practical. Now I need to execute a program on Linux to transfer a PDF file on the Windows system to Linux? # -- coding: utf-8 -- import winrm def job(): # Get the connection t = winrm.Session("xxx.xx.xx.xx", auth=("xx", "xxx")) # Get the content of a.pdf r = t.run_cmd(r'type C:\xxx\xxx\Desktop\test\a.pdf') # Convert the content into a string s0 = str(r.std_out) # print(s0) with open("c.pdf","wb")as f: f.write(s0) print("Writing completed") job() Summarize The above is the program (three methods) for remote controlling Windows system from Linux that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Explanation of mysql transaction select for update and data consistency processing
>>: Summary of various methods for Vue to achieve dynamic styles
mysql gets all dates or months in a time period 1...
How to change the image hyperlink when the mouse p...
Table of contents Objectives for this period 1. F...
MySQL escape Escape means the original semantics ...
This article example shares the specific code of ...
Frequently asked questions Access denied for user...
This article example shares the specific code of ...
Table of contents 1. What is Ts 2. Basic Grammar ...
Recently, an error occurred while starting MySQL....
I hope to align the title on the left and the dat...
This article collects the fonts used in the logos...
1. Problem Sometimes when we log in to Mysql and ...
Many people have encountered this error when star...
The latest Insider version of Visual Studio Code ...
Table of contents Drag and drop implementation Dr...