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

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

9 great JavaScript framework scripts for drawing charts on the web

9 great JavaScript framework scripts for drawing ...

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

How to use lodop print control in Vue to achieve browser compatible printing

Preface This control will have a watermark at the...

MySQL multi-table query detailed explanation

Time always passes surprisingly fast without us n...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

Detailed explanation of MySQL precompilation function

This article shares the MySQL precompilation func...

Source code reveals why Vue2 this can directly obtain data and methods

Table of contents 1. Example: this can directly g...

What is a MySQL tablespace?

The topic I want to share with you today is: &quo...

Quickly solve the problem of slow Tomcat startup, super simple

Today I helped a classmate solve a problem - Tomc...

MySQL master-slave replication configuration process

Main library configuration 1. Configure mysql vim...

CentOS 7.5 deploys Varnish cache server function

1. Introduction to Varnish Varnish is a high-perf...