I'm trying to run a script that runs putty and within the putty terminal that gets created, run a command. I've been able to start putty from my script using Python's subprocess module with check_call or Popen. However, I'm confused as to how I can run a command within the subprocess Putty terminal from my script. I need to be able to run this command in putty and analyze its output on the Putty terminal. Thanks for any help.
You need to set the stdin argument to PIPE and use the communicate function of Popen to send data to stdin.
from subprocess import Popen, PIPE
p = Popen('/the/command', stdin=PIPE, stdout=PIPE, stderr=PIPE)
std_out, std_err = p.communicate('command to putty')
That being said, it may be easier to use a python library that implements the ssh protocol (like paramiko) rather than going through putty.
Related
I want to open a PuTTY window, login to some server and enter few commands right after that and at the end save the output. I want to see the PuTTY window open and all activity that I am doing via program (hence need PuTTY GUI).
Here is what I have tried:
I am able to open a new window and login in it. But using stdin.write I am not able to enter further commands in the same window. What am I doing wrong here? I am very new to python. Please help.
import subprocess
from subprocess import Popen, PIPE, STDOUT
p = subprocess.Popen("putty.exe demo#Test.Rebex.Net -pw password", stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write("ls".encode("utf-8"))
print(p.stdout.readlines())
I have tried Paramiko, and don't want to use it, as it fails to display/print the output of commands like top.
PuTTY is a GUI application, it does not use a standard input. It is not intended for automation. For automation, you can use Plink, what is a console equivalent of PuTTY.
See also Pipe PuTTY console to Python script
Though you should use Paramiko. It does not fail to display/print the output of commands like "top". It' not what it is for. You have to attach it to a terminal to achieve what you want. See Running interactive commands in Paramiko.
Or for automation, you better run the top non-interactively. See your other question Collect output from top command using Paramiko in Python.
I have embedded system which runs on linux. I need to send some strings from this system using python script to other device, which is visible as USB serial COM port. Both devices connected to the same PC and are visible as serial COM ports. The data lines is physically connected between the devices.
When I write to the terminal this line
echo Hello! > /dev/ttyS1
I am successfully receiving the message on another COM port (terminal). How I can do same transmission using python? I saw that is used subprocess module for this task, and I think if I could fit it successfully, I would just stay with it, because I don't need to install a third party libraries on a low resource embedded system.
Now what I was trying to do using this module, F.e. when I tried to run ls -l command using subprocess, I get the correct output in the open embedded system terminal:
import subprocess
subprocess.call(["ls", "-l"])
When an Echo command is launched
import subprocess
subprocess.call(["echo", "Hello!"])
print("Executed")
but how can I use echo Hello! > /dev/ttyS1 command in this python script? I tried to implement it analogously but not very successful.
Try this:
proc = subprocess.Popen('echo Hello! > /dev/ttyS1', shell=True, stdout=subprocess.PIPE)
print(proc.communicate())
I want to call shell script from python code. The shell script which I am trying to call is having multiple database (DB2) call in it; means it connects to DB2 database multiple times and execute different database sqls. I tried using subprocess.call method like (subprocess.call(['./<shell script with full path>'])); but it seems before the script connects to database and executes the commands mentioned within the script, it is terminating. But when I am calling the shell script as a standalone script from command line, then it is working good.
Is there any other way this can be handled?
subprocess: The subprocess module allows you to spawn new processes,
connect to their input/output/error pipes, and obtain their return
codes.
http://docs.python.org/library/subprocess.html
Usage:
import subprocess
process = subprocess.Popen(command, shell=True)
process.wait()
print process.returncode
Side note: It is best practice to avoid using shell=True as it is a security hazard.
Actual meaning of 'shell=True' in subprocess
I want to run a shell script from python.
The shell script is something which runs a server, which needs ctrl+c to break.
How to do that is there a way to run such type of scripts from python.
Just send the SIGINT signal to your app:
proc = subprocess.Popen(stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# When it needs to be stopped
proc.send_signal(signal.SIGINT)
I'm issuing a script remotely via SSH python paramiko
stdin, stdout, stderr = host['connection'].exec_command(command)
However, I notice stdout does not get flushed until the remote command is completed. The remote script takes up to 60 seconds to complete but I can really use the data during that time to make decisions. Is it possible to force flush the stdout buffer during the remote command execution?
Try running the script using "python -u script_name.py" From python's command line args: -u "unbuffered binary stdout and stderr;"