How to automate processes done through screen /dev/ttyUSB0 using paramiko - ssh

I have some devices being managed via an RPi. I'm able to SSH to the RPi using PuTTY and send custom commands to those devices but to do so I need to go through screen /dev/ttyUSB0 115200. I would like to automate this process with a Python script using paramiko.
So far I've succeeded in establishing a connection to the host in the usual way:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, 22, username, pw)
The problem comes in with the commands that I would normally issue via screen. Trying to use ssh.exec_command('screen /dev/ttyUSB0 115200', get_pty=True) followed by something like ssh.exec_command('my command') doesn't work and returns a bash command not recognized error. I've since tried to use a channel, using the following format:
chan = ssh.invoke_shell()
chan.settimeout(3)
chan.send('screen /dev/ttyUSB0 115200\n')
time.sleep(1)
chan.send('my command')
time.sleep(1)
if chan.recv_ready():
print(chan.recv(9999).decode('ascii'))
In PuTTY, sending the command would return a series of strings in the terminal. My goal is to capture those strings and print them through Python. When I use the channel approach I don't receive any notice of unrecognized commands, however the received data is limited to my input commands (expected) and some ANSI escape codes. The data I'm looking for does not get returned.
I'm a complete noob when it comes to networking and Linux so I've been feeling my way through the darkness with this. Based on the data I am receiving from the channel, I suspect I'm not connecting to the CLI in the same way I do using a screen in PuTTY. How might I go about doing this or sidestepping the screen entirely?

Related

SSH with Chilkat in VB6; but maybe generaly

I have written an application for SSH, but the originally used library is no longer developed and has old methods of encryption. Thus, it is not usable for contemporary devices. I am looking for alternatives and I found Chilkat.
I am able to start connecting to the device (Cisco switch for instance), but communication behaves in strange ways.
The basic principle of my program is based on talking client (my program) and server (Cisco switch). After the login is done, I get prompt saying that I would get into priviledged mode by command enable. I recongize this need by sign > at the end of prompt. I send command enable and awaiting prompt Password: After I get it I a send password. I am using two components and timer in this time :
success = ssh.ChannelSendString(channelNum, commandToSend & Chr(13), "ansi")
and
auxStr = ssh.GetReceivedText(channelNum, "ansi")
when I get word Password: in receiving text, I send password string like a commandToSend
but I get % Access denied
I am pretty sure that the password is OK; it seems like one extra enter is sent via connection, because during next returned text is this message :
Translating "passwordString"...domain server (255.255.255.255)
it indicates that passwordString was sent into the switch after message
% Access denied
not like an answer to Password: prompt.
I made debugging in the code to see what my program is sending to switch, but everything seems to be correct. Maybe some mistake in chilkat component ?
Has anybody similar experience ? Or explanation of this behaviour and advice how to solve it, please ?
Send the "enable" command (or just "ena") followed by a single CHR(13). Then get the output from the SSH server, which should be the "Password:" prompt. Then send the password followed by the CHR(13). Then get the output. The send each command terminated by a single CHR(13) and get the output after each command.

How to send control commands using python's Paramiko library

I need to SSH a remote machine and get onto the developer mode. To be specific, I want to execute the command 'Ctrl+gog' upon which I will be prompted for a password. I know how to execute the normal commands, for example chan.send("enable\n"). Please provide me with an answer.
chan.send("\x07\x0F\x07")
Above command worked fine for me.Just concatenate the Hexa equivalent for Ctrl-g,Ctrl-o,Ctrl-g which is, x07x0Fx07.

Only one PuTTY session working at a time?

I use PuTTY sessions to talk to an embedded device running QNX 6.4.1 using SSH over TCP/IP.
Today, one of my systems mysteriously won't allow me to have more than one PuTTY session open at a time. If I try to start a second session, I can authenticate with user name and password, but the sign on banner prints out with an extra blank line between each line and messes with my ability to hit enter. I can do nothing that looks remotely valid except Control-C or close the PuTTY window.
I suspected the text file that contains the banner had bad line
endings, but it does not.
I suspected terminal setting issues, but if I have one session open it
works. With no changes to settings, just trying to open a second session it
does not.
I wondered if the .profile was getting mangled, but that doesn't
seem to be the case either.
Now I'm down to "perhaps ssh is messed up and rebooting would fix
it?" But I am hesitant to reboot it because if we lose TCP/IP
connection to it, it's several hours worth of work (physical labor)
to restore.
Any thoughts about what is going wrong and how I can fix it?
I'm connecting using PuTTY 0.62 from 64-bit Windows 7 to QNX 6.4.1. The openssh/openssl version is modern.
UPDATE
The issue came back a few days later. Using Guntram Blohm's suggestion below, I was able to at least get past the "Press enter once you've read the banner" screen. I then ran stty sane ctrl-j as he recommended. Here is the output of stty:
Bad after I had run stty sane ctrl-j (And hand reformatted it to be readable)
Name: /dev/ttyp1
Type: pseudo
Opens: 3
+raw +echo
+osflow
intr=^C quit=^\ erase=^? kill=^U eof=^D start=^Q stop=^S susp=^Z
lnext=^V min=01 time=00 pr1=^[ pr2=5B left=44 right=43 up=41
down=42 ins=40 del=50 home=48 end=59
I then opened another PuTTY session immediately after this and it worked properly. This is confusing me how it works sometimes and doesn't work others. How can that happen? What is different?
Good
Name: /dev/ttyp2
Type: pseudo
Opens: 2
+edit
+osflow
intr=^C quit=^\ erase=^? kill=^U eof=^D start=^Q stop=^S susp=^Z
lnext=^V min=01 time=00 pr1=^[ pr2=5B left=44 right=43 up=41
down=42 ins=40 del=50 home=48 end=59
So right now I have a good PuTTY terminal open, and a bad one. What else can I do to isolate this issue?
It's probably another process that uses the pseudo-terminal, puts it in a special state, then crashes without restoring the state. vi comes to mind, or maybe a file upload/download program. These programs change the terminal mode to read each character indicidually, instead of line by line, and tweak a few other things as well. Normally, logging out/back in SHOULD fix that, but i'm not sure QNX handles it correctly.
One thing you could do to copy the parameters of a working terminal to the messed up one is stty -g on the good one, then paste that output to the command line of the bad one. Like this (on Linux, i don't have a QNX at the moment):
(on the good terminal)
gbl#bermuda$ stty -g
500:5:bf:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
(on the bad one)
gbl#bermuda$ stty 500:5:bf:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
These terminal modes are kept per pseudo tty device, that's why your /dev/ttyp1 can be messed up, while the /dev/ttyp2 that's allocated for the next ssh connection is ok.

What happens to a process in an EC2 instance when I get a 'Broken Pipe' error on ssh?

I am using some EC2 instances to run some large jobs I can not run locally. The issue I am seeing is that after a while (X hours since the process started) my connection on my shell gives me a broken pipe error
ubuntu#ip-10-122-xxx-xxx:~/stratto/ode$ Write failed: Broken pipe
The instance is still there because I can reconnect with no problems, but how can I reconnect and get back at seeing the logs of the process as before the 'Broken Pipe'
Any tip much appreciated,
Thanks!
Redirect your output to a file and then run the program "nohup ..." to ensure the disconnect doesn't kill it. Use "tail -f" to monitor the redirected file.
Note: Originally said to use "tee" but that won't work. I think a straight redirect and then tail on the file works.
You can use screen to run processes in the cloud even when you are not connected to the server.
sudo apt install screen
To specifically address the issue described in the original post (e.g. connecting to AWS EC2 instances) I a basic example and a more advanced example of using screen.
You can use "screen". Detach from it and ping to google.com. So there ssh session will be active through out the installation.

SSH "Framework" to write programs that will keep a connection open and feed commands to the servers

I am looking for some kind of framework that will allow me to do connect to multiple servers using SSH and keep the connection open, reopen it if it dies, and allow me to run commands to it and report back. For example, check disk space on all the machines right away, so I'd do results = object.run("df -h") and it returns an array with the response from all the machines (I am not looking for a monitoring system).
Anyone have any idea?
I would use Python and the Fabric framework. Lets you easily execute commands on a set of servers - like doing deployment
with Fabric you could do
from fabric import run, env
def getSpace(server):
env.host_string
run("df -h")
>>> fab getSpace("234.24.32.1")
One way to do this would be to use Python and the paramiko library. Writing the functionality that runs a given command on a specified set of servers is then a simple matter of programming.