SSH access

Now that we have two Linux machines (EV3 and your computer) on the same network, it is time for some SSH. We use the SSH utility to shell into EV3 and access a terminal running within it. The username is root and the password is empty. Not the safest choice, but simplicity counts for something, and you can, of course, beef up security yourself later.

So, to SSH into EV3, run ssh [email protected], press Enter when it asks for a password, and voilà! You have root access to the LeJOS terminal. As any Linux aficionado knows, this means that you now have absolute control over the OS. Since you SSH-ed in as the root, you start off in the /home/root folder, but you can cd to any place that takes your fancy. To leave the LeJOS terminal, just run exit.

Setting up the SSH configuration

Laziness is the guiding force of programmers, and typing usernames, IP addresses, and passwords (even empty ones) over and over again is hard work; so, we will set up an SSH config to do it for us.

This requires you to edit the ~/.ssh/config file on your computer (create it if it doesn't exist). Add the following section at the end of the file:

Host ev3
   User root
   Hostname 10.0.1.1

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Once you have saved the changes, you can SSH into the EV3 (running LeJOS) by simply running ssh ev3 (much more concise).

It still asks for the password though. The next step is to use public-key cryptography in order to get rid of this nuisance. I recommend that you perform the following actions in the ~/.ssh folder since it is a good location for storing your public and private cryptographic keys, especially the ones that you use for SSH access. Run ssh-keygen and enter ev3 when it asks what to name the file. Press Enter when you are asked for a password (twice) to leave the password blank. This will come in handy when we automate SSH commands. This will create two files in the current folder: ev3 and ev3.pub. These contain the private and public keys, respectively.

The next step is to place the public key in EV3 so that it knows that your private key is allowed to access the system over SSH. Issue the following sequence of commands on your computer; you will be asked for a password after each, and you just need to press Enter every time:

ssh ev3 "mkdir ~/.ssh"
scp ev3.pub ev3:.ssh/
ssh ev3 "cat ~/.ssh/ev3.pub >> ~/.ssh/authorized_keys"

If a shell command is placed in quotes after an ssh command, it is executed on the remote machine. The first command creates the ~/.ssh folder on EV3. The second uses scp (cp over the SSH tunnel) to copy the public key file to the .ssh folder on EV3. Note how we use ev3 as the remote machine's hostname. The scp command such as ssh reads configurations from ~/.ssh/config and can be instructed using hostnames defined in this file. Without the ssh config entry, this command will have to take the form scp ev3.pub [email protected]:.ssh/.

The third command simply concatenates the contents of the public key to the authorized_keys file. Only SSH attempts using private keys whose corresponding public keys are in authorized_keys are given access to the system. To test the setup, run the following:

ssh -i ~/.ssh/ev3 ev3

The -i switch tells SSH to use the specified file as the private key for cryptographic authentication. You should have gained access to a terminal running inside EV3 without having to type in a password (since we created the key pair with a blank password).

The final step is to edit ~/.ssh/config and add the path of the private key to the ev3 configuration block:

Host ev3
   User root
   Hostname 10.0.1.1
   IdentityFile ~/.ssh/ev3

Running ssh ev3 now will log you in to EV3 using the private key, which doesn't require a password. We now have a single command, ssh ev3, that can be used in automated scripts.

Limitations

This method requires that the EV3 be connected to the computer using the mini USB cable. This might become a hassle if the robot you are testing has to wander about. For every code change, you will have to bring the robot back and connect it via the cable.

Additionally, for a moving a robot where the cable has to be disconnected when it is running, it won't be possible to monitor the output log of the program as it is being executed.

Finally, in the case of some robots, the construction might render the mini USB port (on the top face of EV3) inaccessible. These aren't major hindrances in your ability to use the cable to connect to EV3, but the next two connection techniques will remove these limitations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.91