Python script ending in SSHing the terminal into a remote server - python

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

Related

Execute Commands in Kali-Linux's Terminal through Python

I want to execute commands in the terminal through a python scripts.
i want to create a script which takes data from a .txt file adds that in a list and then one by one execute them in the terminal.
what i am looking for is a process to execute commands in the terminal in Kali Linux, I couldn't find anything online.
like in windows we use import subprocess or import os
Thank you.
example command is like
python3 app.py
Try this:
import subprocess
command = "python3 app.py"
subprocess.call(command, shell=True)
You can use the os.system function. It returns the return value of the command run.
E.g.,
status = os.system('echo hello')

How to get the remote machine command prompt output in 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.

Start a background shell script from python

I would like to connect a remote machine and run background script in that machine from python.
I tried:
os.system("ssh root#10.0.0.1 \' nohup script.sh & \')
But it seems not working. And if I put nohup in script.sh, and simply run
os.system("ssh root#10.0.0.1 \' script.sh \'")
The nohup command would not work in either cases.
I'm confused why so, and is there anybody knows how to do background job from python or it's just impossible doing it this way?
What kind of errors are you getting? What version of Python are you using?
You should take a look at this Python subprocess - run multiple shell commands over SSH
import subprocess
sshProcess = subprocess.Popen(["ssh", "root#10.0.0.1"],
stdin=subprocess.PIPE,
stdout = subprocess.PIPE,
universal_newlines=True,
bufsize=0)
sshProcess.stdin.write("nohup script.sh &")
For example you have a local script (python, bash, etc. Here I am demonstrating you using a python script)
First you create a python file locally. Lets say hello.py
# 'hello.py'
import os
print os.system('hostname')
Secondly now a python script which would execute the above hello.py on a remote machine
import pathos
copy = pathos.core.copy('hello.py', destination='abc.remote.com:~/hello.py')
exec = pathos.core.execute('python hello.py', host='.remote.com')
print exec.response()

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.

python: os.spawn . cannot start bash process on background

Task is to execute bash script from python script and let it execute on background, even if python script will finish. I need UNIX solution and i do not care if it will be not working on Win.
Python script :
#!/usr/bin/env python
import os, commands
command = '/usr/bin/ssh localhost "/home/gd/test/python/back.sh " '
print os.spawnlp(os.P_NOWAIT,command)
print "Python done"
/home/gd/test/python/back.sh :
#!/usr/bin/bash
/bin/echo "started"
/bin/sleep 80
/bin/echo "ended"
The issue is, when python script starts , i see PID of spawned process printed. But there is no process on background. When i use P_WAIT i see exit code 127 which means that command not found in the path. But i already provided all paths that already possible?
These scripts works perfectly with commands.getouput.
Something like this should work
#!/usr/bin/env python
import os
command = ['/usr/bin/ssh', 'ssh', 'localhost', '/home/gd/test/python/back.sh']
print os.spawnlp(os.P_NOWAIT, *command)
print "Python done"
Note that it's preferable to use the subprocess module here instead of spawn
#!/usr/bin/env python
from subprocess import Popen
command = ['/usr/bin/ssh', 'localhost', '/home/gd/test/python/back.sh']
print Popen(command)
print "Python done"

Categories

Resources