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
If you are not committed to becoming an artist, t...
1. Vector Map Vector graphics use straight lines ...
Table of contents Demand background Thought Analy...
Table of contents introduce 1. Pica 2. Lena.js 3....
This article describes how to add or expand a dis...
Preface Since MySQL 5.1.6, a very unique feature ...
Arial Arial is a sans-serif TrueType font distribu...
Table of contents Install sakila Index Scan Sort ...
1. MySQL transaction concept MySQL transactions a...
Preface: js is a single-threaded language, so it ...
In web design, it is very important to use an org...
This article describes the deployment method of A...
After MySQL is installed, you can verify whether ...
XML/HTML CodeCopy content to clipboard < input...
nbsp   no-break space = non-breaking spa...