Three Methods of of in linux Remote Control of windows System

  • 2021-07-06 12:08:45
  • OfStack

Sometimes we need to run programs on windows systems remotely on linux.

Method 1:

Through the winrm module in python, the precondition is to set up winrm service in advance. How to set up please Baidu, winRM service is the remote management service of PowerShell under windows server. The Python script manipulates the windows command line by connecting the winRM module.


import winrm
def cmd_views(ip,cmd_comand):
  win = winrm.Session('http://'+ip+':5985/wsman', auth=('user', 'password'))# Parameters are user name and password 
  r = win.run_cmd(cmd_comand) #  Execute cmd Command =
  return r.std_out #  Print the acquired 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 I test this module can only execute 1 simple command, that is, basically 1 input can immediately respond to the results of the command. When you encounter something slightly complicated, the process hangs up.

Method 2:

The telnetlib library in python is used to execute the operation, and the telnet settings in windows system are set. 1. Install telnet client and server. 2 Configure telnet user rights, which will not be set by Baidu.


# -- 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 ends.

Method 3

Using wmi module, defects can only pass 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(" Current execution bat  The command is :",cmd_callbat)
    conn.Win32_Process.Create(CommandLine=cmd_callbat)
  except Exception as e:
    print(e)
cmdname= r"xxx.bat"# Run command 
sys_version(ipaddress="xxx.xx.xx.xx", user="xx", password="xxx",cmdname=cmdname)#

Command is invoked, program ends, no wait, different from method 2, flaw cannot install library on linux

The application scenario is practical. Now I need to execute the program on linux, and transfer a file in PDF format on windows system to linux?


# -- coding: utf-8 --
import winrm
def job():
  #  Get a connection 
  t = winrm.Session("xxx.xx.xx.xx", auth=("xx", "xxx"))
  #  Obtain a.pdf Content 
  r = t.run_cmd(r'type C:\xxx\xxx\Desktop\test\a.pdf')
  #  Convert content to string 
  s0 = str(r.std_out)
  # print(s0)
  with open("c.pdf","wb")as f:
        f.write(s0)
  print(" Write complete ")
job()

Summarize


Related articles: