In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to remotely control Windows server by Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to remotely control the Windows server by Python"!
In many enterprises, idle Windows machines are used as temporary servers, and sometimes we want to call programs or view log files remotely.
Windows's built-in service "winrm" can meet our needs.
It is a firewall-friendly protocol based on the standard simple object access Protocol (SOAP) that allows hardware and operating systems from different vendors to interoperate.
Official website address
1. Windows of controlled terminal
Take the Windows 10 system machine as an example.
The specific steps are as follows:
Start the winrm service
Open the CMD command window with administrator privileges and enter the following command to start the winrm service
# start winrm service winrm quickconfig-Q
If the operation reports an error, it prompts that the network type setting is abnormal.
You can use the shortcut key win+i to enter the network and Internet, change the network configuration file, and switch from public to private
Check winrm service snooping status
Continue to enter the following command on the command line to view the status of the winrm service
PS: note that the port number Port value here will be used for the connection
# View the status of winrm service winrm e winrm/config/listener# result Listener Address = * Transport = HTTP Port = 5985 Hostname Enabled = true URLPrefix = wsman CertificateThumbprint ListeningOn = * * View winrm configuration information (optional)
You can view all winrm configuration information, client client configuration information, and service server configuration information through the following command
# all winrm get winrm/config# Clientwinrm get winrm/config/client# Servicewinrm get winrm/config/service configuration winrm client# configuration winrm clientwinrm set winrm/config/client @ {AllowUnencrypted= "true"} winrm set winrm/config/client @ {TrustedHosts= "*"} winrm set winrm/config/client/auth @ {Basic= "true"} configure winrm service
After configuring winrm service and winrm client, we make sure that the configuration file is in effect by viewing the configuration file through steps 1-3
# configure winrm servicewinrm set winrm/config/service @ {AllowUnencrypted= "true"} winrm set winrm/config/service/auth @ {Basic= "true"} 2. Control end
On the control side, such as Mac OSX and Linux, we only need to install the "pywinrm" dependency package
The control side installs the dependency package pip3 install pywinrm3. Let's have a real fight.
After preparing for the above, we can write code to control Windows.
First, we need the ip address, port number, user name and password to connect to the Windows controller
# connection windowsimport winrm...# ip address: Port number # winrm server port number # auth: username and password self.session = winrm.Session ("192.168.room.password winrm.Session", auth= ('username',' password'), transport='ntlm')..
In this way, we can simulate CMD and PowerShell input commands through the "run_cmd" and "run_ps" functions of the object
Take looking at the log files in a hard disk directory of Windows as an example.
# Connect to windowsimport winrmimport codecs... Def exec_cmd (self, cmd): "" execute the cmd command Get the return value: param cmd:: return: "" # CMD result = self.session.run_cmd (cmd) # powerShell # result = self.session.run_ps (cmd) # return code # code 0 means the call is successful code = result.status_code # according to the error code Get response content (bytes) content = result.std_out if code = 0 else result.std_err # convert to a string (try to decode via UTF8 or GBK) # result = content.decode ("utf8") # result = codecs.decode (content 'UTF-8') try: result = content.decode ("utf8") except: result = content.decode ("GBK") print (result) return result...# Open File D:/py/log/trade.log# windows use the type command Check the contents of the file result = self.exec_cmd ('D: & cd py\\ log & type trade.log') # check the result print (result) so far, I believe you have a deeper understanding of "how to remotely control the Windows server by Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.