Linux remote control windows system program (three methods)

Linux remote control windows system program (three methods)

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!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed method of packaging Python3 program under Windows and Linux
  • How to cross-compile Golang on Mac, Linux, and Windows
  • How to package Python 3 on Windows and Linux
  • Installation and verification of pytorch in linux or windows environment (solving runtimeerror problem)
  • Analysis of the method of uploading zip compressed packages under Windows to Linux system using compression
  • How to upload and download files between Linux server and Windows system
  • Detailed explanation of accessing MySQL database in Linux virtual machine under Windows environment
  • Remote Desktop Connection between Windows and Linux

<<:  Explanation of mysql transaction select for update and data consistency processing

>>:  Summary of various methods for Vue to achieve dynamic styles

Recommend

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...

JavaScript to achieve product query function

This article example shares the specific code of ...

Mysql anonymous login cannot create a database problem solution

Frequently asked questions Access denied for user...

js code to realize multi-person chat room

This article example shares the specific code of ...

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...

How to solve the abnormal error ERROR: 2002 in mysql

Recently, an error occurred while starting MySQL....

Examples of using the Li tag in HTML

I hope to align the title on the left and the dat...

Collection of 25 fonts used in famous website logos

This article collects the fonts used in the logos...

VScode Remote SSH remote editing and debugging code

The latest Insider version of Visual Studio Code ...

Vue implements a visual drag page editor

Table of contents Drag and drop implementation Dr...