1

I've tried to write a very little script in order to connect to my raspberrypi as follows:

#!/usr/bin/python
#connect to raspberry pi -- ethernet
import os
os.popen('ssh -l pi 169.254.249.166')

but it returns just immediately prints the prompt for my password three times -- I can't write anything and I don't know why.

After the request I just can push Intro and it returns to me to the prompt.

The output of the script is as follows:

 xxx@xxx:~$ python connect_raspberry.py
 xxx@xxx:~$ pi@169.254.249.166's password: 
 xx@xx:~$ pi@169.254.249.166's password: 
 Permission denied, please try again.
 pi@169.254.249.166's password: 
 Permission denied, please try again.
 pi@169.254.249.166's password: 
 Permission denied (publickey,password).`

Why doesn't the script, or maybe the Raspberry as I guess from the prompt, let me type the password? It just answers "permission denied", but I type nothing.

What I really try to write is to run a script in konsole: python code.py and that it returns to me to the Rasp prompt (I suppose I should write the password into a variable but first, I try to log in automatically and write the password):

xx@xx:~$ python code.py
pi@169.254.249.166's password: 
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Sep 17 23:16:57 2017 from peces.local
pi@raspberrypi:~ $

On the other hand, any help about how to write the password into a variable in the Python script to login automatically, I mean:

xxx@xxx:~$ python connect_raspberry.py
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Sep 17 23:16:57 2017 from peces.local
pi@raspberrypi:~ $

For the latter, I don't understand so well the ssh command's -f option (this flag implies the -n flag). I am a little confused about if it is what I need for logging in automatically.

tripleee
  • 175,061
  • 34
  • 275
  • 318
donostialdea
  • 75
  • 1
  • 7
  • hi @donostialdea, alhtough `os.popen` still exits in Python, it is advised to not used it. The library `subprocess` is more fit to that. **Note**: Although not stated in [Python3](https://docs.python.org/3/library/os.html#os.popen), [`popen`](https://docs.python.org/2/library/os.html#os.popen) is marked as deprecated in Python2 – Andy K Dec 14 '17 at 13:47
  • you can use `paramiko` to do ssh for python – Andy K Dec 14 '17 at 14:34
  • @AndyK thanks, i dont know about paramiko now – donostialdea Dec 18 '17 at 13:01

2 Answers2

0

os.popen('command') doesn't connect the standard input of command to your console (your keyboard and display), it just starts a process inside the Python program and waits for Python to talk to it.

os.popen() is very old and long since obsolete; you want to avoid this, and use something more modern. The Python 3.6 documentation suggests subprocess.run() or something like that, but interacting with SSH with a bare subprocess is still tricky and cumbersome -- you really want to use a dedicated library which knows how to do that. Somebody suggested paramiko in the comments and I concur, though maybe also explore pexpect.

import paramiko

ssh = paramiko.SSHClient()
ssh.connect('169.254.249.166', 22, 'pi', 'xyzzy')
stdin, stdout, stderr = ssh.exec_command('python code.py')
# ... interact with your script
ssh.close()

Basically copy/pasted from here.

There is no "konsole" here, the only interface is a remote ssh session connected to the network instead of to a terminal window. (If your script is not interactive, a lot of this is unnecessary, really. Just ssh pi python code.py >output from the shell then!)

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Thanks for the usuful answers.

I had been writting differents codes and I have this:

#!/usr/bin/python
#connect to raspberry pi by ethernet

import paramiko

try:
    ssh=paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh.connect('169.254.249.166', 22, 'pi','raspberry')

except SSHException:
      print('error')

stdin, stdout, stderr= ssh.exec_command('cat /etc/motd')

print(stdout.read().splitlines())

var=''
stdin, stdout, stderr= ssh.exec_command('who')

data=stdout.read()
print(data)

while (var!='close'): # loop until close passphrase

print('type a command.')
var=input('>> ')

stdin, stdout, stderr= ssh.exec_command(var)

print('read --> ', stdout.read().splitlines())

print('error --> ', stderr.read())


ssh.close()

running this code I can logging into raspberrypi and execute a differents types of commands (but not all available commands).

For instance, I cannot move between folders, i mean, if I type: cd Videos into 'var'. stdout is [] and even the pwd command return always the same path: /home/pi

it is the output:

xx@xx:~$ python connect_raspberry.py 
[b'', b'The programs included with the Debian GNU/Linux system are free                   software;', b'the exact distribution terms for each program are described in the', b'individual files in /usr/share/doc/*/copyright.', b'', b'Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent', b'permitted by applicable law.']
b'pi       tty1         2017-09-20 10:17\npi       :0                2017-09-20 10:17 (:0)\n'

type a command.
>> cd Videos
read -->  []
error -->  b''

type a command.
 >> pwd
 read -->  [b'/home/pi']
 error -->  b''

 type a command.
 >> 

to be connected to the raspberry is that I am looking for, but Why it is not complety usuful?? i mean, why some commands doesnt work, like cd .. or cd /home or even sudo su

Also I have tried to copy files from raspberry to my pc but it doesnt run:

>> sudo cp salida.txt /home/my_user  
read -->  []
error -->  b''

type a command.
 >> 

in my pc isnt salida.txt

Again, Thanks for the answer, I got it that i needed but now new problems appeared

donostialdea
  • 75
  • 1
  • 7
  • If you have new questions, post a new question. Maybe link back to this question for context. – tripleee Dec 18 '17 at 13:05
  • Your `except` masks any login problem but the `ssh` handle will be uninitialized or invalid if the `try` fails. Your script is unable to proceed in this case but you are forcing it to try anyway. – tripleee Dec 18 '17 at 13:06
  • Each `exec_command()` runs its own remote shell. If you `cd`, the command executes but your state returns to the way they were before the `cd` when the command finishes. See e.g. https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script – tripleee Dec 18 '17 at 13:09