How to connect to a remote Windows machine to execute commands using python?

后端 未结 11 2092
青春惊慌失措
青春惊慌失措 2020-11-29 20:20

I am new to Python and I am trying to make a script that connects to a remote windows machine and execute commands there and test ports connectivity.

Here is the cod

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 21:00

    The best way to connect to the remote server and execute commands is by using "wmiexec.py"

    Just run pip install impacket

    Which will create "wmiexec.py" file under the scripts folder in python

    Inside the python > Scripts > wmiexec.py

    we need to run the wmiexec.py in the following way

    python  TargetUser:TargetPassword@TargetHostname ""
    

    Pleae change the wmiexec.py location according to yours

    Like im using python 3.8.5 and my wmiexec.py location will be C:\python3.8.5\Scripts\wmiexec.py

    python C:\python3.8.5\Scripts\wmiexec.py TargetUser:TargetPassword@TargetHostname ""
    

    Modify TargetUser, TargetPassword ,TargetHostname and OS command according to your remote machine

    Note: Above method is used to run the commands on remote server.

    But if you need to capture the output from remote server we need to create an python code.

    import subprocess
    command = 'C:\\Python36\\python.exe C:\\Python36\\Scripts\\wmiexec.py TargetUser:TargetPassword@TargetHostname "ipconfig"'
    command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    stdout= command.communicate()[0]
    print (stdout)
    

    Modify the code accordingly and run it.

提交回复
热议问题