1

I'm trying to run netsh command on remote windows hosts (windows domain environment with admin rights). The following code works fine on local host but I would like to run it on remote hosts as well using python.

import subprocess

netshcmd=subprocess.Popen('netsh advfirewall show rule name=\”all\”', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE )
output, errors =  netshcmd.communicate()

The problem is that I'm no sure how/what method to use to initiate the connection to remote hosts and then run the subprocess commands. I cannot use ssh or pstools and would like try to implement it using existing pywin32 modules if possible.

I have used WMI module in a past which makes it very easy to query remote host but I couldn't find any way to query firewall policies over WMI and that's why using subprocess.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
Ray
  • 11
  • 1
  • 3
  • See the second answer to [this question](http://stackoverflow.com/questions/861148/how-can-i-remotely-execute-a-script-in-windows) – Roland Smith Apr 15 '13 at 13:41

2 Answers2

1

First you login the remote host machine using of pxssh modules Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko

remote login of windows:

child = pexpect.spawn('ssh tiger@172.16.0.190 -p 8888')
child.logfile = open("/tmp/mylog", "w")
print child.before
child.expect('.*Are you sure you want to continue connecting (yes/no)?')
child.sendline("yes")

child.expect(".*assword:")
child.sendline("tiger\r")
child.expect('Press any key to continue...')
child.send('\r')
child.expect('C:\Users\.*>')
child.sendline('dir')
child.prompt('C:\Users\.*>')

Python - Pxssh - Getting an password refused error when trying to login to a remote server

and send your netsh command

Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
  • Reegan thanks for your reply but I'm not sure how this is going to work in MS Windows environment. psexpect seems to work using ssh but as I stated earlier using ssh is not optional and all authentication must work using normal windows domain authentications methods. I have written other scripts in a past to manage Windows boxes and below examples of lines used to connect to remote host and was hoping there was something I could use under pywin32 modules. c = wmi.WMI(hostname) or c = win32evtlog.OpenEventLog(hostname,logtype) . – Ray Apr 15 '13 at 13:52
0

I will recommend using Fabric, it's a powerful python tool with a suite of operations for executing local or remote shell commands, as well as auxiliary functionality such as prompting the running user for input, or aborting execution:

  1. install fabric : pip install fabric
  2. write the following script named remote_cmd.py:
"""
Usage:
    python remote_cmd.py ip_address username password your_command
"""

from sys import argv
from fabric.api import run, env


def set_host_config(ip, user, password):
    env.host_string = ip
    env.user = user
    env.password = password

def cmd(your_command):
    """
    executes command remotely
    """
    output = run(your_command)
    return output


def main():
    set_host_config(argv[1], argv[2], argv[3])
    cmd(argv[4]))

if __name__ == '__main__':
    main()

Usage:

python remote_cmd.py ip_address username password command
Dhia
  • 10,119
  • 11
  • 58
  • 69