I can open a new cmd window and connect with plink and serial port through Python.
import os
import subprocess
os.system("start cmd /k plink.exe -serial COM4 -sercfg 115200,8,n,1,N")
Still good here, but when I want to run ifconfig, it didn't work.
os.system("ifconfig")
First, consider using native Python serial connection implementation, like pySerial, instead of running a console application (plink).
See Full examples of using pySerial package.
Anyway if you run a Windows batch file that does what your Python code, it won't do what you want either:
start cmd /k plink.exe -serial COM4 -sercfg 115200,8,n,1,N
ifconfig
The ifconfig is not a top-level command. It's something that needs to be executed by Plink.
You have to feed the command to the plink standard input, see:
Execute a command on device over serial connection with Plink
Related
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 embedded system which runs on linux. I need to send some strings from this system using python script to other device, which is visible as USB serial COM port. Both devices connected to the same PC and are visible as serial COM ports. The data lines is physically connected between the devices.
When I write to the terminal this line
echo Hello! > /dev/ttyS1
I am successfully receiving the message on another COM port (terminal). How I can do same transmission using python? I saw that is used subprocess module for this task, and I think if I could fit it successfully, I would just stay with it, because I don't need to install a third party libraries on a low resource embedded system.
Now what I was trying to do using this module, F.e. when I tried to run ls -l command using subprocess, I get the correct output in the open embedded system terminal:
import subprocess
subprocess.call(["ls", "-l"])
When an Echo command is launched
import subprocess
subprocess.call(["echo", "Hello!"])
print("Executed")
but how can I use echo Hello! > /dev/ttyS1 command in this python script? I tried to implement it analogously but not very successful.
Try this:
proc = subprocess.Popen('echo Hello! > /dev/ttyS1', shell=True, stdout=subprocess.PIPE)
print(proc.communicate())
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 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.
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)