Mac remotely connects to Windows computer to execute bat script

The first time I came into contact with Linux to remotely execute Windows scripts, at the beginning, a large number of domestic data were actually Windows as the remote execution of other machines.

Later, I came into contact with the open source framework pywinrm, and thought about ansible's control of Windows. It seems to be achievable. It is later to see that both use the winrm module.

Requirements:

Refer to this address: Click to open the link  Although it mentions ansible, I think it is very good.

The requirements for Windows are as follows. Anyway, I don't understand Windows, so I had to take it. All three conditions are best met.

(1) The management machine must be a Linux system and the Python Winrm module must be pre-installed

(2) The underlying communication is based on PowerShell, the version is 3.0+, and the version of Management Framework is 3.0+

(3) The remote host starts the Winrm service

There is a demo.bat script on the D drive of my Windows machine, the content is as follows:

Mac host code

# coding:utf-8
try:
    import winrm
except ImportError,e:
    print e
try:
    s = winrm.Session('192.168.1.101', auth=('Administrator', 'DEMAxiya520!'))
    # r = s.run_cmd('ipconfig')
    # r = s.run_cmd('ipconfig', ['/all'])
    get_dir = s.run_cmd('d: & demo.bat')
except Exception,e:
    print e
    print "connection in windows was failed!"
print ("status_code",get_dir.status_code)
print ("std_out",get_dir.std_out)
print ("std_err",get_dir.std_err)

Results of the:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/feixiaohui/github/untitled/demo_pywinrm.py
('status_code', 0)
('std_out', '"hello world!"\r\n')
('std_err', '')

Process finished with exit code 0

The second, third and fourth lines show the result of execution.


It finally came true!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815197&siteId=291194637