How to get the remote machine command prompt output in python? - python

I am trying to connect the windows remote machine and executing the .exe in the command prompt. However, not able to capture the command prompt output
Working: Its connected windows remote machine using WMI and executed the .exe through Win32_Process
Not Working: Not able to capture the .exe output which is printing in the command prompt
import wmi, subprocess
import os, datetime, inspect, sys
import Trigger_Campaign_Sub as fn
from socket import *
...
...
connection = wmi.WMI(ip, user=username, password=password)
process_id, return_value = connection.Win32_Process.Create(CommandLine="cmd.exe /c " + execommand)
process_id, return_value = connection.Win32_Process.Create(CommandLine="cmd.exe /c " + execommand)
This one give the process id for the execution, however i need the output logs printing in the command prompt.

I would take a look at this post:
Run a command in a windows remote server and get back the console output in C# .NET
Seems that running with psexec, returns the shell output on both ends, with the right setup.

Related

Execute a program using python on windows

I am new to windows python. I am trying to run a command line tool using python. This tool will flash the firmware connecting to IP address of the machine. I could open cmd prompt and use the command
C:\ToolsSuite>sdi --ip 172.23.240.41 --fwdl "c:\BUILDS\firmware_image.zip
.This works for me very well.
But when I try to execute using the python script on windows, I am not able to do that. Python script looks like this.
import subprocess
import os
os.chdir(r"C:\ToolsSuite")
#os.system('cd c:\mydir')
os.system("sdi --ip 192.92.48.32 --fwdl C:\firmware_image.zip")
#subprocess.Popen(r'sdi --ip 192.92.48.32 --fwdl "c:\firmware_image.zip"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
The exception thrown is "Could not find file". I am not getting how to give the path of the firmware file when it is stored in some location, say for example 'C' drive or in some folder location of windows.
If the sdi executable is in "C:\ToolsSuite", this should work:
subprocess.call(['sdi', '--ip 192.92.48.32', r'--fwdl "c:\firmware_image.zip"'])
If you want to call a Windows command, you need to give the full path to the command.
You can try:
import subprocess
import os.path
# C:\ToolsSuite>sdi --ip 172.23.240.41 --fwdl "c:\BUILDS\firmware_image.zip"
cmd = os.path.join("C:\\ToolsSuite", "sdi")
args = [cmd,
'--ip', '172.23.240.41',
'--fwdl', 'c:\\BUILDS\\firmware_image.zip']
subprocess.check_call(args)
Here, check_call is useful to replace non-zero exit code by an exception. Of course, you can also choose another function of the same family.

PuTTY Windows pywinauto background

I did a script on Windows using PuTTY:
from pywinauto.application import Application
app = Application().Start(cmd_line='C:\Program Files (x86)\PuTTY\putty.exe -l user -pw **pwd** -load Proxy_10.153.1.250 '+ ip +' -ssh')
putty = app.PuTTY
putty.Wait('ready')
time.sleep(7)
cmd1 = "show log "+ "{ENTER}"
This script will be executed for many switchs, but when it is executed, I cannot do other tasks on Windows else script will be interrupted? Is it possible to be executed in background?
You need a proper tool for CLI automation. Just run subprocess.call('ssh user#host <the rest of cmd>') or use Paramiko to run remote SSH command.
BTW, pywinauto's code is incomplete, I don't see .type_keys(cmd1). You may try .send_chars(cmd1) instead and use putty.minimize() first. But send_chars is not guaranteed to work with every app (and it's experimental). So you can just try.

Python script ending in SSHing the terminal into a remote server

I am writing a python script which I want to end by SSHing the terminal into a remote machine. I'd read about sub processes, but I don't think these would be appropriate. I would like the user then to interact with the terminal, as if they had typed ssh user#server.path into the terminal.
I am only conserned with it running under Ubuntu.
Thank you
>>> import os
>>> os.execlp('ssh', 'ssh', 'user#server')
or
import subprocess
proc = subprocess.Popen(['ssh', 'user#server'])
result = proc.wait()
print result

converting the psftp command written on windows on to linux in python

I am working on python, actually i am trying to connect to sftp and get some files after connecting.
The process which i followed on windows is below
import os
psftpCmd='psftp sftp.example.com -l user -pw pass -b client_configurations\lifebridge.scr -batch'
os.system(psftpCmd)
The code in lifebrige.scr is
cd lifebridge
lcd feeds\lifebridge
get jobs.xml
bye
So i am able to fetch the file successfully i want to do the same process on linux(fedora) machine and i tried the following
import os
psftpCmd='psftp sftp.example.com -l user -pw pass -b client_configurations\lifebridge.scr -batch'
os.system(psftpCmd)
Result:
sh: psftp: command not found
Here i can expect that psftp is putty command so need to do something else on linux fro the same, Can anyone let me now how to write the same command in linux
On Linux, the command is sftp.

How execute script from command prompt in python

I have mysql dump command that I would like to run from from windows shell
or command prompt. I have used shell it does work.
d= 'BkSql_'+datetime.datetime.now().strftime("%Y-%m-%d")+".sql"
fn = dn+d
cmd="""mysqldump -u hapopdy -p > %s""" %fn
print cmd
Edit:::::::
The -p needs to be a raw input.
Using the subprocess module
import subprocess
subprocess.call(cmd)
If you're running a shell command add shell=True
subprocess.call(cmd, shell=True)
You should save the password in mysql's local configuration file for the user.(In Unix it's ~/.my.cnf) or you can give it on the command line with --password=MYPASSWORD.
Either way, the password will be visible to a large audience. In the .my.cnf case, it will be visible to anyone with read access to the file. In the second case, it will be visible to anyone who can get a process listing on the system, in addition to those who have read access to your script.

Categories

Resources