Open CMD and execute linux commands after bash command - python

I run Windows 10 and I'm trying to execute linux sommands in CMD.
I have installed ubuntu so that I can run bash in CMD
Now I want to be able to run commands in this CMD window.
In this case a simple ping command.
First. I can not figure how to reuse the CMD window, I don't want to open a new CMD for every loop.
Second. I want to make sure that the command is done before executing the next in the loop.
cmd_shell = subprocess.Popen('start cmd /K bash', shell=True)
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = "ping -c4 "+site
# this is where I need help to execute the ping command in the already opend CMD window
After I have open and executed bash, I cant find a good way to write in the CMD window.
I did a not so nice solution using pyautogui but I think there's a much better way to do it, but I dont know how.
Help please.
/ BR BaconFlip

So I realized that I did not need a CMD window to do this.
I could just run it in the script like this:
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = '"ping -c4 '+site+'"'
command = check_output('c:\\Windows\\System32\\bash.exe -c '+cmd_line)
print(command)

Related

Python subprocess.Popen() does not find executable

I'm working on a script which should run a command in a new subprocess. At the moment im using the python subprocess.Popen() for this. My problem is that I can run commands like "dir" but not a installed program. When I open the cmd prompt and write "program -h" I am getting the normal help outprint. When I try to open it with Popen() I am getting an error that the program can not the found. It is like he is not finding the executable even if the cmd promt can find it when I look for it manually. For the test i used ncat as an example program.
import time
from subprocess import *
import subprocess
execom = "ncat -h" # DOES NOT WORK IN PYTHON BUT MANUALLY IN CMD
execom1 = "ncat -h -e cmd" # DOES NOT WORK IN PYTHON BUT MANUALLY IN CMD
execom2 = "dir" # WORKS
p = Popen(execom, shell=True)
out = p.communicate()[0]
Does someone know how to fix that?
EDIT:
I got the solution with the hint of 2e0byo. I added the paths to the systemvariabels but the system needed an extra restart to pass it. It worked without restart from cmd prompt but not from python script. After restart it works from both now.
Per the comments, the problem is just that ncat isn't in your PATH, so you need to add it:
import os
env = os.environ.copy()
env["PATH"] = r"C:\Program Files (x86)\Nmap;" + env["PATH"]
p = Popen(execom, env=env)
...
See this question for modifying the env.
As to why ncat isn't in your path I really don't know; I don't use Micro$oft Windoze very much. Perhaps someone will be along with a canonical answer to that question, or you can ask it over on superuser and see if someone knows how to set it up. If I needed this to be portable I would just check a bunch of folders for ncat and bail if I couldn't find it.

How to run CMD commands via subprocess.call() without opening CMD window?

I'm able to run commands through CMD with Python with subprocess.call('insert command here') but it opens the CMD window then closes right away. Is there a way to not open it at all?
You can set shell=True when calling subprocess.call:
subprocess.call('insert command here', shell=True)

Can I start command prompt from my python script and send multiple outputs to it?

I am running a script and I want the script to start cmd.exe each time it runs. I want to print out information to the user. I need cmd because the script may be compiled as an executable and run as a background task so output may not be available.
I tried this, but I am hoping there's a better way
import os
os.system('start cmd /k echo checking in progress...')
os.system('cmd /k echo checking completed!!!')
The last line didn't print.

How to open a 2nd terminal from 1st one and run command in it? (without it being child process)

I am trying to write a script in python which should change my desktop wallpaper on my raspberry pi. I am a beginner in both python and linux, have been stuck on this problem the whole day. Would love to hear from you guys <3
This is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg
Concerning only the linux terminal syntax: i would like to open a second terminal and run a command in it, all initiated from the first terminal. If i type into my first terminal:
pi#raspberrypi:~ $ lxterminal &
it opens a new terminal window which stays open, and is not a child process right? In this 2nd terminal my change wallpaper command works. The following command does not work and if i put a "&" next to gnome-terminal it opens a new terminal but does not execute the command that was specified with -e and gives me an error.
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'
How do you open a new terminal with a command passed with -e which is also not a child process?
I know you are new so I want to introduce some concepts to you before I can answer your question.
The "&" operator in shell/unix is not meant to open a new terminal. It is an operator that invokes unix's handy little job control protocol, which allows the parallelization of complex programs! It's awesome. It makes that command a background process, which basically means it starts a new shell (or "terminal" in the language of your OP) which runs that process and leaves you in control of your current shell (or terminal). The shell you are still in control of is called the foreground process.
now, what you have going on with gnome-terminal is a little more complicated. gnome-terminal is executing a bash terminal (which has a shell for each process you run inside it) in the GNOME environment. -e is the command you want to send to this terminal. So, you put the ampersand (&) at the end of that command if that is the command you desire to send to the background.
Now, let's look at the command you want to run:
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'
-e indicates the command you want to run in the new terminal. bash-c (commmand) is changing your wallpaper. Ok, cool. exec bash is probably any weird error thrown, if I had to guess. But that line should perform nothing at all.
What it sounds like to me is you don't really need to send anything to the background.
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg
should change your wallpaper. But, to answer the question completely, just place the & AFTER whichever command you wish to send in the background.

How to Open Cmd With Python Then open a file in the new Cmd - Windows

Basically what I'm looking to do is to have python code that I can run from cmd that opens a new cmd window then types commands into it. I have been able to open and run commands but the commands always run in the first cmd window. I think I might need to switch the cursor or something like that, but I don't know how.
Here is my current code.
import subprocess
subprocess.call('start', shell=True)
import time
time.sleep(5)
cmd=['', ''] ### In the second '' you can type in cmd.
for each in cmd:
process=subprocess.Popen(each,stdout=subprocess.PIPE,shell=True)
output=process.communicate()[0]
print(output.decode('utf-8'))
- Thanks
EDIT :
NVM Its in the similar one

Categories