I am trying to automate a ssh connection with pywinauto. I am connecting via Putty to my server and can execute commands. However, I need to get the output of the Putty to verify what exactly is going on the server. Below you find my example code so far which I find on stackoverflow:
import time
import sys
app = Application ().Start (cmd_line=u'putty -ssh root#host')
putty = app.PuTTY
putty.Wait ('ready')
time.sleep (1)
putty.TypeKeys ("cd{SPACE}/")
putty.TypeKeys ("{ENTER}")
time.sleep (1)
putty.TypeKeys ("ls")
putty.TypeKeys ("{ENTER}")
I need a function that gives me the outputstream of the putty window. Something like:
stream = putty.ReadConsoleOutputStream()
lines = stream.readLines()
I tried it with:
print(putty.window_text())
but it only gives me the title of the window back. I have heard about paramiko but since the connection is passwordless, I couldn't access it via paramiko because i couldn't find a way to connect it without a password.
Thank you in advance for your help.
Related
I have an Onion Omega2 device acting as a linux server that has a UART stream from an Arduino chip. Through the terminal on my laptop I can connect via SSH and stream the data from the UART coming into the device. I then attempted to create an SSH shell in Python using Paramiko. Code shown below:
import paramiko
def ssh_comm(ip, usr, passwd):
client = paramiko.SSHClient();
client.set_missing_host_key_policy(paramiko.AutoAddPolicy());
client.connect(ip, username=usr, password=passwd);
channel = client.invoke_shell();
channel.send("screen /dev/ttyS1 9600 \n");
print("/n");
points = 0;
while points < 100:
if channel.recv_ready():
print(channel.recv(1024));
points = points + 1;
channel.shutdown(2);
client.close();
return;
ssh_comm("192.xxx.x.x", "root", "password");
First time around it connects well and all data is streamed back to my laptop. However when I let the shell close and then re-open it I only recieve a few packets every now and then back from the Omega2. (It still connects fine though) After connecting through python the transmission is also intermittent when forming the SSH connection on the terminal using: ssh root#192.xxx.x.x.
Restarting the Omega 2 fixes this however since I can repeatedly connect though the terminal with no issues I beleive the problem must be to do with closing the session within the python code. Or not configuring it properly. Having looked through the paramiko docs and tried my best to configure it correctly I still get the same issue. Any ideas as to what could be causing it?
I found that the error is not to do with the SSH configuration but rather not closing the screen command before closing the channel. this was done by sending CTRL-A then k then y.
channel.send("\x01");
channel.send("k");
channel.send("y");
The \x01 represents CTRL-A. Without this re-running the program causes a second screen to be created and they both fight over the UART stream. Solution was found with reference to: Python Paramiko send CTRL+C to an ssh shell And provides a second method of fixing the problem.
I am trying to write application which uses WSH scripting under Python for automation purpose. I used win32com.client module to get shell and run the putty application to access routers. Iam able to access the devices and send the key strokes and commands to putty GUI window. But the problem is,I want to track the ongoing command status which was sent using COM object.How can I read/get the buffer data of application initiated by COM object.Is there any easy way?Can I can get the data in python variable which is returned by device in putty ! Please help..`
import time
import win32api
import win32com.client
shell = win32com.client.gencache.EnsureDispatch("WScript.Shell")
shell.Run("putty")
time.sleep(1)
shell.SendKeys("192.168.1.x")
shell.SendKeys(r"show version | no-more") #command to run on putty
console
time.sleep(2)
shell.SendKeys("~")
Thanks,
Sat
Issue : I'm a noob with Paramiko, trying to run some commands from a python script (on personal machine) on a remote server. The remote server doesn't need a password to connect to.
For example, if I do
root#[IPaddress] on my Mac, I'm successfully able to connect to the remote server via MacbookPro terminal.
However, I'm trying to do this inside a Python script using Paramiko, and no matter what I do, I get an Authentication error or No Authentication methods available.
I went through Paramiko AuthenticationException issue but the answers there are vague for me to implement without significant experience with Paramiko. Help?
This is my code:
import paramiko
import os
from paramiko import SSHClient
#Borrowed from the linked post
class SSHClient_noauth(SSHClient):
def _auth(self, username, *args):
self._transport.auth_none(username)
return
#How do I implement?
ssh = SSHClient()
sshc = SSHClient_noauth()._auth(username="root") #Where's the ssh obj passed?
sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshc.connect("10.xxx.xxx.xxx")
Well, not to let the negative vote bog me down.
Tried doing sshc.connect(remoteIP, username=username, password="") and it worked. In case someone has been stuck for over an hour or two trying to get this working, especially for work, you might want to try putting in a "" instead of None.
I'm using motion to run a rudimentary livestream. It works perfectly when i start it in server side with:
sudo motion -c livestream.conf
This starts a video server in 8081 port and i can access perfectly from wherever i want inside my network.
The issue comes when i want to write a little script which will ssh using paramiko to server, start motion with the same command and open default browser directly in video stream url. Here the sample code:
import paramiko
import subprocess
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.111', username = 'pi', password = 'raspberry')
ssh.exec_command('sudo motion -c livestream.conf')
time.sleep(4)
subprocess.call('xdg-open "http://192.168.1.111:8081"', shell= True)
ssh.close()
a pidof motion in server shows that the service is running,but i can't access it!!! Because motion is running, i think is not the common problem with sudo/paramiko, but i don't have any idea why this does not work.
WORKAROUND
Motion has a daemon mode. Enabling it from
/etc/default/motion
it starts on boot and i can call it perfectly with:
subprocess.call('xdg-open "http://192.168.1.111:8081"', shell= True)
But is not exactly what i'm looking for, because i'd like to launch(and close, but this will be another thread sure!!) the daemon, not just access the stream.
This workaround executes
/etc/motion/motion.conf
as daemon.I copied my motion script in there and everything good.
But when i try to start the script as daemon (not on boot, with the code above), it tells me that it can't create PID file. Everything done as root. I'm getting close to the answer by myself, just a little more.
I have an application where Python acts as a client talking to another shell (tcl). I would like to have Python connect automatically - I have it all working now but I have to manually do a sock.connect in python. The problem is the tcl shell is started from the Python script in an os.system call:
def start_tcl():
exit_tcl() #Close any existing connections
server.reset(2540) #Create new socket
os.system("vivado -mode tcl -source server.tcl") #Open tcl server and connect to socket
So if I put connect right after that, the server isn't actually opened yet, and I get endpoint not terminated. Is there a way to have Python wait to connect to the socket once the server is connected? I'm sure I don't fully understand sockets, am I going about this all wrong?
I ended up using a while loop in case anyone is interested:
while ( server.connect() is "None" ):
time.sleep(1)
– Kaleb Droskiewicz