Execute command on remote windows machine and get output in python - python

I want to write a script that executes command on remote windows machine, and get output from that command. The problem is, I want to use built-in software in windows, so I can't unfortunately use for example SSH for that. I found "wmi" library, and I can log in to the machine and execute command, but i don't know how to recieve output from that command.
import wmi
c = wmi.WMI("10.0.0.2", user="administrator", password="admin")
process_startup = c.Win32_ProcessStartup.new()
process_id, result = c.Win32_Process.Create (r'cmd /c ping 10.0.0.1 -n 1 > c:\temp\temp.txt')
if result == 0:
print("Process started successfully: %d" % process_id)
print(result)
I tried to redirect output to file, but I can't find any way to get text file content either.
Is there any possible way to get output or text file content using wmi, or other python libraries?

For the application you're describing you may find RPyC is a good fit. It's a python remoting server you can connect to and issue commands; it will do anything that Python can do on the target machine. So you could, for example, use the subprocess module to run a windows command and capture the output then return the result.
The safe thing to do is to expose your remote functionality as a service -- basically, it's just a python script that RPyC can run for you. However you can also use RPyC classic mode, which is pretty much like running a python session directly on the remote machine. You should use it only when you are not worried about security, since classic mode can do anything on the remote machine -- but it's useful for prototyping.

Related

How to run a python script from local machine on a remote server and display the outputs live?

The only solution i found was this :
cat MyScript.py | ssh username#ip_addr python -
the problem with this is that it wont show the outputs of that script live, it waits until the program is finished and then it displays the output
I'm using scapy with in this script and sniff packets with it, and i have to run this on that remote server (and no, i cant copy it there or write it there)
so what is the solution? how can i view the outputs of a script live in my command line?
I'm using windows 10.
Important note: also i think ssh is using some sort of buffering, and sends the output after the amount of printed stuff gets more than buffer, since when the output is very large it does show part of it suddenly, i want the output of that function to come to my computer as soon as possible, not after reaching a buffer or something
You should first send the file to your remote machine using
scp MyScript.py username#ip_addre:/path/to/script
then SSH to your remote machine using
ssh username#ip_addr
ANd finally, you run you script normally
python path/to/MyScript.py
EDIT
To execute your script directly without copying it to the remote machine, use this command:
ssh user#ip_addr 'python -s' < script.py

Python to perform SSH logins and do something

I am trying to automate few routine tasks in Netapp storage using Python.
Since I am using Windows machine and Python I need to perform few tasks to connect storage using ssh session and perfom few things and I need view how it is acting . For example if we take we are connecting Linux box over ssh if I execute ls command I need to see the output of the ls.
To achieve this which tools I need to use ..I searched in web some are suggesting to use paramiko and pexpect but these are the modules to use connect ssh and perform tasks but we cannot view the output on terminal.
With the parallel-ssh library:
from pssh import ParallelSSHClient
# See also private key examples in documentation
client = ParallelSSHClient(['netapp.host'], user=<user>,
password=<password>)
output = client.run_command(<cmd>)
for line in output['netapp.host'].stdout:
print line
See parallel-ssh documentation. It uses paramiko under the hood so should work just as well on Windows.
stdin channel is also available in output for providing input like with pexpect, see run_command documentation for examples.
As a bonus, the client is parallel and can be used to run commands on any number of hosts.
Using paramiko directly is of course also possible, though a lot more code would be required which parallel-ssh does for you.

ssh session as python subprocess takes input but does not print it to stdout

I'm trying use python's cmd library to create a shell with limited commands. One requirement I have is to be able to run a command that executes an existing shell script which opens an ssh session on a remote machine and from there allows the user to interact with the remote shell as if it was a regular ssh session.
Simply using subprocess.Popen('[/path/to/connect.sh]') works well at least as a starting point except for one issue. You can interact with the remote shell but the input that you type is not shown on stdout...so for example you see the prompt on your stdout but when you type 'ls' you don't see it being typed but when you hit return it works as expected.
I'm trying to wrap my head around how to print the input to stdout and still send it along to the remote ssh session.
EDIT:
Actual code without using cmd was just the one line:
ssh_session = subprocess.Popen(['connect.sh'])
it was fired from a do_* method in a class which extended cmd.Cmd. I think I may end up using paramiko but would still be interested in anyone's input on this.
Assuming you are using a Unix like system, SSH detects if you are on a terminal or not. When it detects that you are not on a terminal, like when using subprocess, it will not echo the characters typed. Instead you might want to use a pseudo-terminal, see pexpect, or pty. This way you can get the output from SSH as if it was running on a true terminal.

Start a process on another computer on the network

I'm required to start a series of python scripts and/or other windows executables. Some of these require a Windows system, others require a Linux machine.
Currently there are designated machines to run the OS-dependent programs. So I know where I want to start which program.
Is there a way to start a python script (or a windows executable) from a python script, on the local network, on another computer (e.g. run 192.168.0.101:/dir/python_script_123.py?
The script, which should then run various programs may then look something like this in pseudo code..
linuxip = 192.168.0.101
linuxparam = "required parameter"
winip = 192.168.0.201
winparam = "required parameter"
#option 1 (run all), 2(run linux only), 3(run windows only), 4(run local only)
option = 1
if option == 1:
magic_things.run("linuxip:/dir/linux_script.py" + linuxparam)
magic_things.run("winip:C:\\dir\\windows_prog.exe" + winparam)
subprocess.call(["/dir/local_script.py","parameter"])
subprocess.call(["/dir/another_local_script.py","parameter"])
elif option ==2:
[...]
You need to connect to your server machine from your client. In case of the linux machine you could use SSH.
see http://en.wikipedia.org/wiki/Secure_Shell
Assuming you have a ssh server on the linux server running you could use the package paramiko (http://docs.paramiko.org/en/1.15/api/client.html) to connect to the machine and run your script there.
This could look something like this:
from paramiko.client import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.connect('linuxip', username='your_user', password='very_secret')
stdin, stdout, stderr = client.exec_command('python /home/your_user/your/path/to/scripty.py')
However please note that it's not very secure to store passwords in scripts and it's probably better to use a public/private key authentication (See the wiki article).
The paramiko package also offers the option for an ssh server, so this might be a solution for your windows machine, but I am not very sure as I don't run any windows machines any more.
Hope this was helpful!
David
install ipython and ipython kernel on the remote server, and ipython and ipython kernel on the local machine. Then you can connect to the remote server using the settings here: https://stackoverflow.com/a/48332182/4752883
and run any program that would run on the remote machine using subprocess or the os builtin libraries. Further this is OS independent, so it will work whether your client/server is linux or Windows or Mac

run ssh command remotely without redirecting output

I want to run a python script on my server (that python script has GUI). But I want to start it from ssh. Something like this:
ssh me#server -i my_key "nohup python script.py"
... > let the script run forever
BUT it complains "unable to access video driver" since it is trying to use my ssh terminal as output.
Can I somehow make my commands output run on server machine and not to my terminal... Basically something like "wake-on-lan functionality" -> tell the server you want something and he will do everything using its own system (not sending any output back)
What about
ssh me#server -i my_key "nohup python script.py >/dev/null 2>&1"
You can use redirection to some remote logfile instead of /dev/null of course.
? :)
EDIT: GUI applications on X usually use $DISPLAY variable to know where they should be displayed. Moreover, X11 display servers use authorization to permit or disallow applications connecting to its display. Commands
export DISPLAY=:0 && xhost +
may be helpful for you.
Isn't it possible for you to rather use python ssh extension instead of calling external application?
It would:
run as one process
guarantee that invocation will be the same among all possible system
lose the overhead from "execution"
send everything trough ssh (you won't have to worry about input like "; possibly local executed command)
If not, go with what Piotr Wades suggested.

Categories