This question already has answers here:
Execute multiple commands in Paramiko so that commands are affected by their predecessors
(2 answers)
Closed 2 years ago.
I want to run multiple commands with same shell.
Executing command of paramiko library is not with same shell.
Commands :
Ssh cloud-user#node -i /path/to/bcmt_rsa
Cat /etc/ssh/sshd_config
Change permitRootLogin yes to no
Restart sshd server
Log to file.
I am new in python. I have tried many ways but not working
Not clear questing but i have example lets say you want to edit file then read it
have variable
command="echo data >> path_to_file/filename.txt"
command+="cat path_to_file/filename.txt"
then execute ssh normal and read the stdout lines ,or you can use && in the command
like command="echo data >file_path.txt && cat file_path.txt"
Do you want to runultiple comand at a time in single sell?I think its not possible.But you can splot your terminal and can run differend comand in didferent partifion of a terminal.For this you can use Terminator,a great bash terminal.
Besides python is a single threded language.So you can not run multiple function same time in a script.Python will exicute function by function
Related
This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 3 years ago.
In my Python script, I create a .bat file (many actually), and I run them via
os.startfile(blah)
Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.
How?
You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:
os.startfile("cmd.exe /k blah.bat")
Documentation of the available command line arguments can be found here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
This question already has answers here:
How can I run an external command asynchronously from Python?
(10 answers)
Closed 4 years ago.
I want to execute a 3 separate bash commands on 3 separate core processors of a node of a supercomputer.
I have a python script that sends a bash command via os.system to the command line, however it seems to execute the bash command to completion before going to the next line in the python script. I want to just send bash commands to the command line one after another.
for i in range(0, len(core), 8) :
os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i]))+" 0.01"+" y")
os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i+1]))+" 0.01"+" y")
os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i+2]))+" 0.01"+" y")
Consider building a shell script file from your python program and then executing that shell script. As you build the shell script, place an & at the end of each line. It is also useful to put the wait command at the end of the program so the shell script will pause until all of the background commands (running in parallel) are complete.
#/bin/bash
a &
b &
c &
wait
I could have given you a more specific answer had your code been formatted so that I could copy and paste it and then execute it.
#Mark is on the right track, but just to explain what is happening: your code isn't doing anything to run the commands in parallel. Ways to do this include, but are not limited to:
Background jobs as in #Mark's solution
make -j
Python multiprocessing or threading
GNU Parallels
This question already has answers here:
Run multiple python scripts concurrently
(10 answers)
Closed 4 years ago.
I was wondering if there is a way to run two python scripts at the same time through the command prompt.
I also am wondering if there is a way to run a second python script after another one has already been executed and is currently running.
Thanks
To execute 2 script at the same time, execute python script1.py & python script2.py
To execute one after the other (without waiting for the first one to finish), you could simply open 2 command prompt windows. Execute each script in each window.
To execute one after the other (after waiting for the first one to finish successfully), execute python script1.py && python script2.py
This question already has an answer here:
Passing input to an executable using Python subprocess module
(1 answer)
Closed 6 years ago.
I have a bash script that I invoke from my python script through subprocess.call; however, that bash script when run alone asks for an user input. I want to provide this input through my python script itself rather than having an user answering it manually.
So I basically want to invoke a bash script from python script and then provide an input through the code itself to one of the questions that gets generated on the execution of that bash script.
Here is what I have currently:
subprocess.call("/bin/bash "+sample.sh ,shell=True)
Executing this, sample.sh asks for user prompt:
what is your name?
and I want to pass that name from the python code itself.
I would be grateful for any pointers. Thank you.
pexpect seems to do exactly what you need. As an example :
child = pexpect.spawn('scp foo myname#host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)
Here's the github repo. You can install pexpect with :
pip install pexpect
You can solve that issue using bash and expect. Here is an example to do a remote ssh connection and executing ls -l /:
export IP=<ip>
export PAS=<passwd>
expect -c 'spawn ssh root#$env(IP) "ls -l /"; expect "password:"; send "$env(PAS)\n"; interact;'
When expect receives string "password:" it sends the content of variable PASS (the password), so you can do the ssh connection and execute the remote command without writing the password explicitly.
This question already has answers here:
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Closed 6 years ago.
I am able to open cmd.exe using script
import subprocess
subprocess.call("C:\Windows\System32\cmd.exe",shell=True)
But I am unable to send input command to cmd.exe open.
I want to achieve something like below mentioned using script
1) script give input command like python to cmd.exe open
2) After that script give input command like print "hello" to python prompt comes
Why not use Python to create a batch file that prints what you want, then execute that batch file? You could either return immediately or keep the command interpreter open: it depends on the command switches you use to run the batch file.
You could create a file hello.py containing print "hello", and run it through cmd ?
from subprocess import call
call(["python", "hello.py"])