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 code that I am writing but it is not working. Basically, I want to and it returns with the local machine data, not the remote one.
import wmi import os import subprocess import re import socket, sys def main(): host="remotemachine" username="adminaam" password="passpass!" server =connects(host, username, password) s = socket.socket() s.settimeout(5) print server.run_remote('hostname') class connects: def __init__(self, host, username, password, s = socket.socket()): self.host=host self.username=username self.password=password self.s=s try: self.connection= wmi.WMI(self.host, user=self.username, password=self.password) self.s.connect(('10.10.10.3', 25)) print "Connection established" except: print "Could not connect to machine" def run_remote(self, cmd, async=False, minimized=True): call=subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT ) print call main()
Running commands remotely on another host from your local machinelink. Using the Paramiko module in Python, you can create an SSH connection to another host from within your application, with this connection you can send your commands to the host and retrieve the output.
SSH is widely used by network administrators for managing systems and applications remotely, allowing them to log in to another computer over a network, execute commands and move files from one computer to another. In python SSH is implemented by using the python library called fabric.
If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the RunShellCommand() function. The first option is easier to run one line of code and then exit, but it isn't as flexible when using arguments or producing text output.
You can install the pure-python-adb library using pip install pure-python-adb . Optional: To make things easier for us while developing our scripts, we can install an open-source program called scrcpy which allows us to display and control our android device with our computer using a mouse and keyboard.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With