Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()  
like image 274
zewOlF Avatar asked Sep 23 '13 13:09

zewOlF


People also ask

How do I run a python command on a remote computer?

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.

How do I SSH into a python script?

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.

How do I run a shell command in python?

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.

How do you use python to control an app?

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.


1 Answers

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 
like image 92
kenorb Avatar answered Sep 19 '22 06:09

kenorb