Sending a command to terminal - python

Mpsyt is a terminal based library for Python. It provides to searching and playing music on youtube. This code which provides searching on youtube:
os.system("mpsyt search creep")
But then, i need to send a command to terminal which is "1". Because this "1" plays first music on searching list. How will i send "1" command to shell which is exist?

Try the following code..
import subprocess
p = subprocess.Popen(['mpsyt', 'search', 'creep'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.communicate(b'1')[0].strip().decode('UTF-8'))
You can read the output of the subprocess by p.stdout.readline() command. Make sure there is something to be printed in console before writing that command. Otherwise your will program will stuck.

Related

How to run more than one command with Python subprocess

I'm trying to run three commands with subprocess.Popen(), i don't know which is the problem.
The commands are running properly on the terminal but not on the code. Here is the code and the output.
str1 = os.path.join(install_path, "components", "esptool_py", "esptool", "esptool.py")
print(install_path) #/home/laura/esp/esp/idf
print(str1) #/home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py
str_result = "error has occurred, check if the credential in the \"input.config\" are correct"
cmd1 = f"cd {install_path}; . ./export.sh; python3 {str1} flash_id"
cmd = subprocess.Popen(cmd1, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
gui.write_on_output_text("1.0", "checking database for incorrect input on device code")
out, err = cmd.communicate()
out_str = str(out.decode("utf-8"))
THE OUTPUT:
/home/laura/esp/esp-idf
/home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py
/bin/sh: 22: ./export.sh: [[: not found
COMMAND IN TERMINAL THAT WORKS PROPERLY:
cd /home/laura/esp/esp-idf ; . ./export.sh ; python3 /home/laura/esp/esp-idf/components/esptool_py/esptool/esptool.py flash_id`
I don't know why in the terminal works, but in the code not. And i don't know if the error is that is NOT FINDING THE FILE or THE COMMAND.
Thank you :)
I already tried many ways to do it.
First i used the command gnome-terminal but it was not correct, because i don't want a new terminal I just want to send these 3 commands.
I know that in the terminal works because the response after send it is good, is what i expected, but in the code is not working, and I'm not sure if it's because Python cannot find the commands on /bin/sh, or if cannot find the file "export.sh".
I have this problem with the 3rd command too, it cannot find the "esptool.py"
I fix it with one of the ideas/resolutions on the comments.
subprocess.Popen(..., **executable='/bin/bash'**)
Just adding this :) The problem as they said, is that, i was trying to run with 'sh'

How to run commands in a external file through command prompt using python?

My Requirement is -
I have set of commands written in a "commands.ftp" file, to upload a "request.txt" file to FTP server.
I want to call the command prompt from my python file and run the commands in the "commands.ftp" file one by one .
Once all the command executed successfully I need the output in my python file.
Suppose if the file is uploaded successfully or if any error occurred.
I saw many answers where it was recommended to use subprocess. but I am not getting exact answer to use this.
command.ftp
ftp
open hostname.com
usename
password
quote site recfm=fb lrecl=750 blocksize=27750 cyl pri=120 sec=60
put "File_Name.txt"
QUIT
The above code is to upload a file to Mainframe CMS.
I am able to upload the file successfully.
But After uploading the file the response which we will receive how to capture that . I am not able to capture the output response.
Below is the code I am writing to to execute the command.ftp file.
from subprocess import Popen, PIPE
commands = open('local.ftp', 'r').read()
process = Popen("cmd.exe", shell=False, universal_newlines=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out = process.communicate(commands)
print(out)
Here the out variable is not giving the output
Python has an FTP module built-in (import ftplib). That would be WAY smarter than trying to pass commands into the command-line "ftp" command. Windows, for example, doesn't include an "ftp" command in the box at all.

Running a .bat file in python that requires a user input

I am trying to get an output from a .bat file in a python script, code works fine if I hard code a variable value in .bat file, but I want that value to be dynamic.
This is code I am using to execute the external file.
command = 'C:/this/this.bat'
p = subprocess.Popen(command, universal_newlines=True,
shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
text = p.stdout.read()
retcode = p.wait()
file this.bat requires a user input but I am not sure how to provide it inside python script, e.g. from a variable. Thanks for the help.
I've dealt with something similar. The way I solved it was to prompt the user for values in python. Then using subprocess pass those values into the .bat file.
command = [shutil.which('C:/this/this.bat') ,sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]]
subprocess.Popen(command).wait()
This assumes you can change the .bat file to take parameters instead of prompts.

How to get output from netsh using python subprocess?

Can anyone please advise what am I doing wrong, that there is no output showed when executing netsh command on windows using python subprocess library?
Example:
p = subprocess.run('netsh dhcp show server', shell=True, stdout=subprocess.PIPE)
print(p.stdout.decode('utf-8'))
Output: empty string
When I execute some other command, etc. echo Hi, I get an output:
p = subprocess.run('echo Hi', shell=True, stdout=subprocess.PIPE)
print(p.stdout.decode('utf-8'))
My intention is to get list of our DHCP servers and parse the output.
Thanks!
Try to change the code page of the process to UTF-8 before executing sub-processes.
chcp 65001

call external program in python, watch output for specific text then take action

i'm looking for a way in python to run an external binary and watch it's output for: "up to date" If "up to date" isn't returned i want to run the original command again, once "up to date" is displayed i would like to be able to run another script. So far I've figured out how to run the binary with options using subprocess but thats as far as I've gotten. Thanks!
Use Popen from subprocess like this
process = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE)
Then use process.stdout to read from program's stdout (like reading from any other file like object).

Categories