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
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 <wmiexec.py location> TargetUser:TargetPassword@TargetHostname "<OS command>"
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 "<OS command>"
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.
is it too late?
I personally agree with Beatrice Len, I used paramiko maybe is an extra step for windows, but I have an example project git hub, feel free to clone or ask me.
https://github.com/davcastroruiz/django-ssh-monitor
pypsrp - Python PowerShell Remoting Protocol Client library
At a basic level, you can use this library to;
Execute a cmd command
Run another executable
Execute PowerShell scripts
Copy a file from the localhost to the remote Windows host
Fetch a file from the remote Windows host to the localhost
Create a Runspace Pool that contains one or multiple PowerShell pipelines and execute them asynchronously
Support for a reference host base implementation of PSRP for interactive scripts
REF: https://github.com/jborean93/pypsrp
You can use pywinrm library instead which is cross-platform compatible.
Here is a simple code example:
#!/usr/bin/env python
import winrm
# Create winrm connection.
sess = winrm.Session('https://10.0.0.1', auth=('username', 'password'), transport='kerberos')
result = sess.run_cmd('ipconfig', ['/all'])
Install library via: pip install pywinrm requests_kerberos
.
Here is another example from this page to run Powershell script on a remote host:
import winrm
ps_script = """$strComputer = $Host
Clear
$RAM = WmiObject Win32_ComputerSystem
$MB = 1048576
"Installed Memory: " + [int]($RAM.TotalPhysicalMemory /$MB) + " MB" """
s = winrm.Session('windows-host.example.com', auth=('john.smith', 'secret'))
r = s.run_ps(ps_script)
>>> r.status_code
0
>>> r.std_out
Installed Memory: 3840 MB
>>> r.std_err
For connection
c=wmi.WMI('machine name',user='username',password='password')
#this connects to remote system. c is wmi object
for commands
process_id, return_value = c.Win32_Process.Create(CommandLine="cmd.exe /c <your command>")
#this will execute commands
do the client machines have python loaded? if so, I'm doing this with psexec
On my local machine, I use subprocess in my .py file to call a command line.
import subprocess
subprocess.call("psexec {server} -c {}")
the -c copies the file to the server so i can run any executable file (which in your case could be a .bat full of connection tests or your .py file from above).