Missing 'read' prompt in bash when using ssh? - python

Please tell me I'm missing something really obvious here:
$ cat ~/bashplay/f
#!/bin/bash
read -p 'RDY> ' x
echo $x
$ ~/bashplay/f
RDY> direct execution
direct execution
$ ssh somehost ~/bashplay/f
indirect via ssh
indirect via ssh
Note the missing "RDY>" prompt when using ssh. I see the same thing in python when using the "readline" package. Anyone know why?

From man bash:
-p prompt
Display prompt on standard error, without a trailing new‐
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
Use the ssh option -t which forces pseudo tty allocation:
ssh -t somehost ~/bashplay/f

Related

Open tabs in gnome-terminal from python script

I'd like to open different gnome-terminal tabs and connect via ssh to different servers. To do this, I used to use a little bash script.
#!/bin/sh
hosts=("172.16.1.183" "172.16.1.184")
pass=Password123!
for host in "${hosts[#]}"; do
gnome-terminal --tab -- sshpass -p $pass ssh -o StrictHostKeyChecking=no root#$host
done
I'm forcing myself to start scripting in python, as I would like to learn it. That's why now I'm trying to do this with this python script instead.
#!/usr/bin/env python3
import subprocess
for servidor in ['172.16.1.183', '172.16.1.184']:
subprocess.run(['gnome-terminal', '--tab', '--', 'sshpass', '-p', 'Password123!', 'ssh', '-o', 'StrictHostKeyChecking=no', 'root#',servidor])
It seems that for a second the tabs do open but then they close. As I don't know how to see any kind of log, it is hard for me to understand what's happening.
Could you please help me to understand what's happening? Is there any way to run these scripts with a "verbose" option like in bash with "-x" or things like -v, -vv, -vvv for instance?
Thank you very much!
edit: I'm using PYTHONVERBOSE=1 but I can't understand that output :/ is not like I can see an "error" anywhwere or even follow the step-by-step execution to understand what's happening.
You are passing the username and hostname as two list elements. Instead it should be just one element. Change 'root#',servidor to 'root#'+servidor
Your code can be written in a better way as follows
cmd = 'gnome-terminal --tab -- sshpass -p sshpass ssh -o StrictHostKeyChecking=no root#{}'
for servidor in ['172.16.1.183', '172.16.1.184']:
subprocess.run(cmd.format(servidor).split())

Bash script: Open Python, execute some lines, then let user take control

I have a Python program, A.py, that creates binary data upon completion. To help users analyze the output, I want to add a small script, B.sh, to the output directory that fires up a Python console and executes some commands, C, that load the data and prepare them such that a user sees what is available. After executing C, the script B.sh should keep the Python console open.
First attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python
opens a Python console and keeps it open but doesn't execute anything within that console.
Second attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python -i C.py
executes C.py (I'd prefer not to have to write an additional file for the startup commands, but I could live with that) and keeps the window open, but doesn't show what was done. More specifically, the user would be presented with the outputs of C, but not the command that were used to achieve the outputs.
Instead,I'd like the user to be presented with a console like this:
>>> [info,results] = my_package.load(<tag>)
>>> my_package.plot(results)
>>> print(info)
<output>
>>> my_package.analyze(results)
<output>
>>>
Save this in a file called demo.tcl
#!/usr/local/bin/expect -f
# Spawn Python and await prompt
spawn /usr/local/bin/python3
expect ">>>"
# Send Python statement and await prompt
send "print('Hello world!')\n"
expect ">>>"
# Pass control to user so he can interact with Python
interact
Then make it executable with:
chmod +x demo.tcl
And run with:
xterm -e ./demo.tcl
In the picture, you can see I went on after the "Hello world" to print the system version info.
Your paths for Python and expect may be different, so check and alter to suit.
For anyone who happens to be using macOS (a.k.a. OSX), you can install expect with homebrew as follows:
brew install expect
And, since Macs don't ship with X11 any more, rather than install XQuartz and run xterm, you can start a new Terminal and run the Python in there quite simply with:
open -a Terminal.app demo.tcl
As suggested by user #pask I simply printed the commands before executing them.
Furthermore, I added a -c switch to be able to put the Python commands directly in B.sh instead of having to write a file C.py
Here is the B.sh I am using now:
#!/bin/sh
xterm -e python -i -c "print('>>> import my_package');import my_package;print('>>> [info,results] = my_package.load()');[info,results] = my_package.load()"

Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"

I am using Paramiko to SSH and edit a config file. The file itself needs sudo permissions to edit. This hasn't been a problem so far, as I've just done echo <sudopw> | sudo <command> for other sudo commands in my script.
When I try to edit this file using sed, though, nothing happens. stderr produces: sudo: no tty present and no askpass program specified
Here is my code:
stdin, stdout, stderr = client.exec_command
('echo <sudopassword> | sudo sed -i -e \"\\$aAllowUsers\" /etc/ssh/sshd_config)')
I have tried solutions using invoke_shell but nothing seems to be working. Any solution to edit this file would be helpful.
EDIT: This has been solved! Don't use get_pty. Use the -S option of sudo right after "sudo".
If you read the error message
sudo: no tty present and no askpass program specified
then you can easily find the solution: add the -t option to your ssh command:
-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
This has been discussed before:
How to fix 'sudo: no tty present and no askpass program specified' error?
sudoers NOPASSWD: sudo: no tty present and no askpass program specified
Regarding Paramiko, there have been related questions, with a couple of different approaches:
use the get_pty method of the ssh Channel to obtain a pseudo-terminal (which is analogous to telling ssh to do this)
use the -S option of sudo, and send the password on your standard output.
For discussion, see the suggested answers here:
Paramiko and Pseudo-tty Allocation
Nested SSH session with Paramiko

nc.traditional -e script.py Not printing anything

I am starting a basic netcat server with the command nc.traditional -l -e server.py -p 4567
Problem is when I'm connecting to it (telnet 127.0.0.1 4567), the script starts but nothing gets on screen.
I have print instructions on the beginning of the script that are read by the interpreter (I tested that it starts via file manipulation) but nothing is written on my telnet terminal.
Moreover, it stays stuck on a raw_input instruction. I can write in the telnet terminal, but nothing seems to be sent to the python script.
I've tried with a bash script replacing the python one and this works, it prints things on screen and read inputs.
I've also tried connecting via ftp instead of telnet without results.
So, I finally found the solution
In the shebang, add the option -u to the interpreter to unbuffer stdin stdout and stderr
Shebang line:
#!usr/bin/python -u

exec sudo commands on ssh in python

I need a python lib to execute ssh command. I also need the output.
I tried paramiko: It was exactly what i needed but no way to execute sudo commands there. there are some online posts for that but none seem to work.
I also tried fabric: The problem is there is no way to capture output also sometimes it shows error while setting env.
Can anybody suggest something. A example of exec some sudo cmd over ssh will be good enough.
Fabric's operation.run captures stdout and also stderr if you pass combine_stderr to run(). See http://docs.fabfile.org/en/1.3.4/api/core/operations.html#fabric.operations.run

Categories