I am trying to open an interactive ssh shell through fabric.
Requirements:
Use fabrics hosts in the connection string to remote
Open fully interactive shell in current terminal
Works on osx and ubuntu
No need for data transfer between fabric/python and remote. So fabric task can end in background.
So far:
fabfile.py:
def test_ssh():
from subprocess import Popen
Popen('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
In terminal:
localprompt$ fab test_ssh
localprompt$ tcsetattr: Input/output error
[remote ubuntu welcome here]
remoteprompt$ |
Then if I try to input a command on the remote prompt it is executed locally and I drop back to the local prompt.
Does anyone know a solution?
Note: I am aware of fabrics open_shell, but this does not work for me since the stdout lags behind, rendering this unusable.
A slight modification does the trick:
def test_ssh():
from subprocess import call
call('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
As the answer to this question suggests the error suggests the error comes from the inability of ssh to connect to the stdin/out of a process in the background.
With call the fabric task does not end in the background, but I am fine with that as long as as it is not interfering with my stdin/out.
Related
I'm using a python script to control tcpdump. I can start a process of tcpdump just fine from my script. However, when I want to kill the tcpdump process via python:
import subprocess
pid = 9669 # pid of the tcpdump process
subprocess.call(["sudo", "kill", "-9", f"{pid}"])
I receive this error message:
kill: (9669): Permission denied
However, when I open a shell and enter sudo kill -9 9669 it kills the process just fine. The system is configured so that neither sudo tcpdump nor sudo kill will prompt for a password. To my understanding the subprocess.call command and the terminal command should be identical, yet one works and the other doesn't. What am I doing wrong?
PyCharm had been installed via snap. This has put PyCharm behind AppArmor. I removed PyCharm via snap, downloaded the PyCharm tar file and did the installation myself. Now my script for stopping tcpdump works as intended from within PyCharm.
Honestly I find it a bit baffling to put a developer tool into a sandbox where some things just won't work due to the restrictive nature of the sandbox.
I am trying to write a python script that will SSH to a server and execute a command. I am using Python 2.6 on Windows, and have installed plink and paegent (for ssh keys) and added them all to my path.
If I go to the command prompt and type:
plink username#host -i key.ppk
open vnc://www.example.com/
I see the desired behavior-- a VNC viewer opens on my Mac (server).
However, if I have tried two approaches to do this programmatically through Python and neither is working:
Approach 1 (os):
import os
ff=os.popen("plink user#host -i key.ppk",'w')
print >>ff, r"open vnc://www.example.com"
ff.flush()
Approach 2 (subprocess):
import subprocess
ff=subprocess.Popen("plink user#host -i key.ppk",shell=False,stdin=subprocess.PIPE)
ff.stdin.write(r"open vnc://www.example.com")
ff.stdin.flush()
Neither approach produces an error, but neither opens the VNC window. However, I believe they both successfully connect to the remote host.
What am I doing wrong?
In the second approach, use
ff.communicate("open vnc://www.example.com\n")
I use fabric for automation of runnning commands via SSH on a remote PC.
I'd try :
Popen("plink user#host -i key.ppk", shell=True)
Popen("open vnc://www.example.com", shell=True)
I have a python script that uses Popen to create an appium server for a simulator on a mac
self.appium_process = subprocess.Popen(["/usr/local/bin/appium", "-a", self.ip, "--nodeconfig", self.node_file_path, "--relaxed-security", "-p", str(appium_port), "-dc", default_capabilities], stdout=log_file, stderr=subprocess.STDOUT)
I created a bash shell script that calls the python script. When I run the script from the local box it works and the appium logs show the connection.
I need to run this remote via ssh however. So I use the following to call the script:
ssh 10.18.66.99 automation_fw/config/testscript.sh
This, however, always ends up with the log showing:
env: node: No such file or directory
I checked and the node app has an extra slash before its called:
$ which node
/usr/local/bin//node
$
I tried changing the path on the machine but no change. How can I get this to run from ssh in the same way as it can run locally on that same box
A
When you are running a command vis SSH you are not starting what's called a login shell (more about that here).
From the details you've shared, I would say it's some thing in your environment (running outside a logged-in shell), more specifically a problem with your $PATH variable. You might want to check /etc/environment or similar paths (depending on your Linux flavour) for the wrong value.
I want to execute a Python script as Administrator. I'm using the following command to do so:
powershell Start-Process python -ArgumentList "C:\Users\myuser\python_script.py","-param1", "param1","-param2","param2" -Verb "runAs"
This command works fine if I'm using traditional SSH using a terminal.
Target machine is Windows RS4 and I'm using the new native SSH server available since RS3.
My client's Python code is:
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=myhost, username=myuser, password='trio_012')
stdin, stdout, stderror = ssh_client.exec_command("powershell Start-Process python -ArgumentList \"C:\Users\myuser\python_script.py\",\"-param1\", \"param1\",\"-param2\",\"param2\" -Verb \"runAs\"")
print 'stdout:', stdout.readlines()
print 'stderror:', stderror.readlines()
The output I'm getting:
stdout: []
stderror: []
I don't see the script is running on the other side and it seems like nothing happens.
I don't know what is the problem cause I'm getting no output.
I'm using Paramiko 1.18.5 (I can't use the new v2, I'm having issues with known_hosts file and Windows when using paramiko.AutoAddPolicy() policy)
The process, started with Start-Process, closes, when an SSH channel that started it is closed.
The "exec" channel (exec_command) closes immediately after the powershell process finishes, what is almost instantaneous. So I believe that your python process is actually started, but is taken down almost immediately.
It should help, if you add -Wait switch to Start-Process.
stdin, stdout, stderror =
ssh_client.exec_command("powershell Start-Process python -ArgumentList \"C:\Users\myuser\python_script.py\",\"-param1\", \"param1\",\"-param2\",\"param2\" -Verb \"runAs\" -Wait")
It "works" from an SSH terminal, because the terminal (SSH "shell" channel) stays open after powershell process finishes. But if you close the terminal before the python process finishes, it would terminate it too.
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
I am trying to write a python script that will SSH to a server and execute a command. I am using Python 2.6 on Windows, and have installed plink and paegent (for ssh keys) and added them all to my path.
If I go to the command prompt and type:
plink username#host -i key.ppk
open vnc://www.example.com/
I see the desired behavior-- a VNC viewer opens on my Mac (server).
However, if I have tried two approaches to do this programmatically through Python and neither is working:
Approach 1 (os):
import os
ff=os.popen("plink user#host -i key.ppk",'w')
print >>ff, r"open vnc://www.example.com"
ff.flush()
Approach 2 (subprocess):
import subprocess
ff=subprocess.Popen("plink user#host -i key.ppk",shell=False,stdin=subprocess.PIPE)
ff.stdin.write(r"open vnc://www.example.com")
ff.stdin.flush()
Neither approach produces an error, but neither opens the VNC window. However, I believe they both successfully connect to the remote host.
What am I doing wrong?
In the second approach, use
ff.communicate("open vnc://www.example.com\n")
I use fabric for automation of runnning commands via SSH on a remote PC.
I'd try :
Popen("plink user#host -i key.ppk", shell=True)
Popen("open vnc://www.example.com", shell=True)