-1

I'm trying to create a script on my computer that will SSH into my Raspberry Pi and automatically configure some WiFi options (over USB→serial cable, with screen). I am running into issues getting SSH to give the Raspberry Pi its password when it asks for it.

These are the commands I've tried in my .sh file:

screen /dev/cu.usbserial 115200
pi
raspberry
cd Desktop

This opens the screen, but then I have to press Enter and give the Pi its password and login manually. Is there any way I can configure my .sh file to give the Pi its login and password?

1

3 Answers 3

2

The simplest way (without the need to install any additional stuff) to accomplish this, is to use ssh keys.

On your machine where you are running from run ssh-keygen and take all the default answers. On your Raspberry, run the same thing. This will create and populate a directory ~/.ssh with correct permissions.

Once you have that, you need to copy the contents of ~/.ssh/id_rsa.pub from your source computer to ~/.ssh/authorized_keys on your Pi.

After that you are good to go - no password needed.

1

SSH does not run on the serial port, it runs a getty directly. You can interact with it in any program that can talk to a serial device like using pyserial with python. Using this, you can open a serial connection and send it the characters you would normally type to interact with the getty. You can read and parse responses as well to make it less error prone.

But, this is just scripting an interactive prompt, it is not the most reliable way to set up a Pi. For example, if a character gets dropped (which can happen on a serial line) then it can mess up the commands you are trying to enter.

If you are on, or have, a Linux system available (you can use VirtualBox and Vagrant to easily create one), it is better to chroot into the image and set it up from your main PC. See this guide for more detailed instructions on how to do this with the archlinuxarm image, though the steps are similar for Raspbian (mostly you can skip to the chroot steps). Also, this guide for a more detailed explication of creating the chroot.

0

You can use the sshpass utility to login to the raspberry pi.

  • You can install the sshpass using $sudo apt-get install sshpass
  • Then you can add the following line to your script for automated login to raspberry pi sshpass -p "password" ssh username@hostname
  • For example - sshpass -p raspberry ssh [email protected]

Sample Screenshot

Hope it helps. Happy Coding Cheers!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.