Execute remote script windows

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.

11 Answers 11

You can connect one computer to another computer in a network by using these two methods:

Here is the example to connect using wmi module:

netuse

The second method is to use netuse module.

By Netuse, you can connect to remote computer. And you can access all data of the remote computer. It is possible in the following two ways:

Connect by virtual connection.

Mount remote computer drive in local system.

To unmount remote computer drive in local system:

Before using netuse you should have pywin32 install in your system with python also.

You can use pywinrm library instead which is cross-platform compatible.

Here is a simple code example:

Install library via: pip install pywinrm requests_kerberos .

Here is another example from this page to run Powershell script on a remote host:

Maybe you can use SSH to connect to a remote server.

Install freeSSHd on your windows server.

SSH Client connection Code:

Execution Command and get feedback:

I have personally found pywinrm library to be very effective. However, it does require some commands to be run on the machine and some other setup before it will work.

I don’t know WMI but if you want a simple Server/Client, You can use this simple code from tutorialspoint

Server:

Client

it also have all the needed information for simple client/server applications.

Just convert the server and use some simple protocol to call a function from python.

P.S: i’m sure there are a lot of better options, it’s just a simple one if you want.

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.

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).

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.

pypsrp — Python PowerShell Remoting Protocol Client library

Many answers already, but one more option

It’s a python clone of the famous psexec. Works without any installation on the remote windows machine.

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

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

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.

How to execute a .sh script on remote server (linux & ubuntu) using PuTTY in windows

Read this article in other language

PuTTY doesn’t need any introduction as its one of the widely remote console utilities. PuTTY is an SSH and telnet client.

Although use the console is already simple and easy you are most likely to make mistakes sometimes (and erase the project folder accidentaly). You can automate your server tasks without type all that you need everyday (go to a folder, do something there, then exit, navigate to other etc).

Make it easy for you and your team. In this article, you’ll learn how to execute a shell script from windows to a remote server using PuTTY and the windows command prompt.

Creating a .sh file

Typically a .sh file is a shell script which you can execute in a terminal. This file format is commonly used for Unix shell files, created by the Unix shar utility program.

How safe is it?

You may find it weird that you must perform another task manually in order to execute a file. But this is partially because of strong need for security.

Basically when you download and run a bash script, it is the same thing as somebody telling you «run all these commands in sequence on your computer, i promice that results will be good». Ask yourself if you trust the party that has supplied this file, ask yourself if you are sure that have downloaded the file from the same place as you thought, maybe even have a glance inside to see if something looks out of place (although that requires that you know something about *nix commands and bash programming).

Conclusion : execute only commands that you know , and only if you know how they work.

To get started, create a file somewhere with .sh extension (command_file.sh) somewhere. Then, write some shell script.

In this example (the most basic), we are going to print the date in the Putty console.

That was , really easy isn’t?.

If you want , start creating your own, the following example shows a basic interaction with commands that you normally execute using putty (subversion command svn update ):

Note: as is your first time creating a .sh file to be executed, be sure of what do you to prevent any damage that you can do to your server. We recommend you to test only with the previous script and when you have more experience, try with your own.

Start reading more about all the things that you can do with .sh files in your server. Now that our script is ready, we just need to learn how to execute it automatically.

Executing the script using PuTTY from the windows command prompt

Now that your script file is ready and (theoretically) you know what you want to achieve with it, we just need to execute it and save many precious time.

The structure of the command is the following:

T he -m option expects to be given a local file name, and it will read a command from that file.
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
This option is not available in the file transfer tools PSCP and PSFTP.

The entire command can be executed on the command prompt (cmd.exe) in windows easily, e.g:

Note : instead of use the full path to the putty executable, you can define an environment variable for putty in windows and then use it as %putty% .

Open the cmd.exe and execute the command replacing respectively the credentials and parameters :

Finally, after the execution of your command in the cmd.exe , putty will be opened and your script will be executed as we made with the first example, the date of the server will be printed in the console :

Tip : you can create an interface using another language like C# to automate even more with a single click

Capture putty output (optional)

If your bash script is not straight-forward (it can generate output with information about errors, information etc), you may want to save the content to analyze it when the script execution is closed.

As putty normally closes the console if there’s no statement that pause it when the last line of your script is executed and there’s no way to redirect the output (but achievable with plink utility), we need to specify this task to the PuTTY application.

1) Open putty, click on the top icon and select change settings.

2) Click on Logging item in the right side list. Now select the All session output option button in the Session loggin area.

Finally, choose a path to putty.log file in your system and click on apply.

All the output generated by putty will be written in this log file.

Execute a command on a remote computer

I use this to force all of my workstations to update Group Policy changes immediately (as noted in the example listed in the comments).

Source Code

16 Comments

Worked well. It enabled me to get quick changes posted on all machines.

Fantastic! Thanks for this. Excellent addition to my IT toolbox and makes supporting my remote users that much easier.

very nice, thanks Joe

Thanks Joe, great for our domain where changes are made constantly.

Can someone tell me how to install this? I can’t find a howto on it.

Glad you guys are finding this useful! @serpentkisst Are you asking how to run a .vbs file, or something else? This is a script file, it does not require any installation, though you must save it as a .vbs file before running it.

I was hoping I could set it up as a tool, like ping, trace route, rdp, etc.

In Spiceworks, or on your computer? You’re not being very clear with what you are asking (ping and tracert are command line tools, and RDP is a protocol, so that description didn’t really clarify your question for me). I don’t think it could be made to work from inside Spiceworks (not without a ton of work, and defeating the purpose of a script anyway). As far as on your workstation goes, sure. Save the file somewhere in your path (like C:\windows), and then you can run it from the command line, just like ping. The syntax for it will change depending on what command you want to run. Look at the header in the code for an example of how to do exactly this with gpupdate.

Not really a comment on the script, but SysInternal’s PsExec does something similar. Although, I’m not sure how PsExec connects to PCs to run commands. http://technet.microsoft.com/en-us/sysinternals/bb897553

Sorry, yes, I meant in Spiceworks. See this related question topic: http://community.spiceworks.com/topic/127890?page=1#entry-653994 Screenshots are included.

No I don’t see that working. This is not a utility like what is being used there, this is a script file. You could add cscript.exe there I guess, and then call it with this script and it’s parameters (which would potentially change at every use), but that would be kind of laborious. I suppose you could use something like Admin Script Editor to package this script as an executable, and then use it like you are wanting, but again I think that would be more work than it’s worth.

What would the command be to run this script against a list of computers? Or would you run it against an OU? This is great though! Thanks for this script!

It’s not designed to be run against a list of computers. It could be fairly easily modified to do so, you just need a For Each. loop wrapping the execution code.

How can I remotely execute a script in Windows?

I would like to have a Windows 2003 server fire a script to fire another script in a separate Windows Server 2008 computer.

I have been told that Powershell can do that, and that’s fine, but I need more specific details.

Does anyone have any tips for this?

5 Answers 5

Look into the syntax for the AT command. You can use it to schedule a process to run on a remote machine.

The AT command schedules commands and programs to run on a computer at a specified time and date. The Schedule service must be running to use the AT command.

easiest way that is use will be in two steps

a. installing cygwin to remote pc

b. run ssh hudson@mcs ‘/cygdrive/c/path_to_script.bat’

Speaking about PsExec , I would strongly suggest to use Cygwin/OpenSSH instead.

SSH has multiple advantages (over tools like PsExec or even custom-made services).
For example, try to use with PsExec and implement what these bash / ssh command lines do:

SSH transfers (!) remote stdout / stderr / exit status to local shell for inspection
(killer feature and common requirement to integrate remote execution into logic of local scripts)

Cygwin/OpenSSH provides standard POSIX shell environment
(efficient time investment, fundamental tools, cross-platform ready, compatible habits, etc.)

You can still/always run all native Windows application
(including automatic execution of *.bat files by cmd processor)

You can configure password-less auth using public keys
(think about unattended automated tasks)

Tip

There was one requirement I had problems with initially:
background sshd service had to execute apps in user’s graphical session
(to make application window appear in desktop environment).

The acceptable solution for me was running sshd service directly in user’s GUI session
(start automatically when user logs in, follow the link to see configuration file changes):

Читайте также:  Захват windows movie maker
Оцените статью