Accessing USB IO Modules remotely by SSH (Secure Shell)
The LucidControl USB IO modules operate with Windows and Linux operating systems and they are the perfect solution turning your computer into a data acquisition and control center.
The USB IO modules are able to acquire and control analog and digital signals. They can for example measure temperatures, switch analog as well as digital lines and generate analog voltages and currents. The USB analog output module can be used in order to build 0-10V or 4-20mA interfaces.
Since the Raspberry Pi B+ and Raspberry Pi 2 B have overcome some issues regarding the USB function and provide now 4 reliable USB ports, these devices are very interesting for building network device servers. Network devices servers allow to access the LucidControl USB IO modules remotely and to forward measured values or control some electronic consumers via a network.
There are several ways how to make a USB IO module available via a network. One solution is e.g. to install a web server like Tomcat on the Raspberry Pi and develop a web interface controlling the USB IO module. Another very handy solution is the secure shell (SSH) which is available for Linux since ages.
The secure shell is a network client-server protocol which provides a remote command-line login and allows the execution of applications on the remote system.
LucidIoCtrl is the command-line tool which is a simple but convenient interface that is able to run all commands of a LucidControl USB IO module. LucidIoCtrl is a console based application that is available for Windows and Linux including a version for the Raspberry Pi and the Beagle Bone Black. It does not need any libraries and runs out of the box on all systems.
In the following I describe how to setup the secure shell and how to use the USB IO modules remotely.
Setting up the Secure Shell
Check in advance if SSH is available on your system by executing the ssh command. If the tool is not presently installed it can be done by:
After the installation has finished the secure shell package is ready to use on your computer, and a connection to a remote computer can be started by:
In order to log in the user needs to enter the password for authentication.
In this example the user klaus logged in on the client computer DEC-VBOX-1 connects to the remote computer RPI-AZ-1.
Since in the example no explicit user name has been passed to the SSH command, the user name of the user already logged in on the client computer is used on the remote server, too. A user account for the remote login user must be set up in advance.
After the password has been entered, the command prompt of the remote system appears and commands can be executed on the remote system.
In this example the device information of the LucidControl USB IO module connected to /dev/ttyACM0 are queried by the LucidIoCtrl tool.
Key based automated Secure Shell Calls with Key Pairs
A drawback of the explained method is that a password is needed for authentication and it must be entered in order to log in to the remote system.
This SSH command is running on the client and is executing the LucidIoCtrl command on the remote computer. But for authentication the password must be entered and the attention of the user is needed every time the command is executed what prevents automation of SSH calls.
Automated calls to remote systems can be achieved by using a key pair which consists of a public key which can be published to any remote system the computer needs to connect to. The private key remains securely stored on the client computer.
A key pair can be created on the client system by:
The command asks for a directory and a passphrase which are both optional. The passphrase is a password that protects access to the private key. The explained method is also secure if the passphrase is skipped. The generated key pair is stored in ~/.ssh where id_rsa and id_rsa.pub can be found.
Note: When a passphrase was entered it protects accessing the private key what makes it necessary to enter the passphrase at least once when the private key is accessed the first time. Using the ssh-agent can do this for you. The ssh-agent service sends the passphrase to the SSH on request.
Once the public key (id_rsa.pub) and the private key (id_rsa) have been generated, the public key can be distributed to the remote system.
The program ssh-copy-id copies the key to a remote system. The distribution of the key needs the password of the user before the key can be copied to the remote system.
In this example the LucidIoCtrl application is executed on the remote computer RPI-AZ-1. The LucidIoCtrl command queries the device information from the USB IO module connected to /dev/ttyACM0 and returns that a USB analog input module with the voltage range 0-10V is connected to the port.
Using SSH with a Windows Client Computer
Linux provides SSH as a set of standard tools which are not available on Windows computers. PuTTY is an open source tool that extends Windows with several useful programs in order to interact with Linux systems. A download can be found here.
The screenshot shows the main window of the PuTTY application. PuTTY is a client application for several protocols like Telnet and Rlogin and also SSH. In the main window of PuTTY settings for a connection can be made and saved as a session name. Session names are names that relates to the remote computer connection and bring more explanation to the user. The session names are used by the command line tool plink.
Clicking on the open button connects to the remote system and asks for a user password.
The procedure shown here is the same as we saw earlier with the Linux SSH client. After entering the password for the specified user name, access is granted by the SSH server and e.g. applications can be started on the remote server system.
As you can see we opened now with PuTTY a SSH connection to RPI-AZ-1 and queried the device information of the USB analog input module.
In the next steps I will explain how to automate SSH calls with the PuTTY client. The principle is the same as we did earlier with the Linux SSH client and is based on a public and private key pair.
Note: During the first SSH connection a security breach message appears. The reason for this is that the client does not know the host key of the server. This message can be confirmed the very first time but if it appears again it is possible that a security breach occurred.
PuTTY comes with the PuTTY Key Generator which is able to create SSH key pairs. The keys generated by ssh-keygen cannot be used with PuTTY or at least I found no easy way to reuse the previously generated key pair with PuTTY.
It is useful to specify a comment related to the key as well as a passphrase protecting the private key, but both is optional and can be skipped for a test. Especially when automated calls without user interaction are what you want to do, keys without passphrase should be considered. Otherwise, the system will ask for the passphrase at least once in order to give access to the private key.
In our case we work with the default settings and generate a SSH-2 RSA 2048 bit key. It is up to you to decide if this is sufficient for your application.
Clicking onto the Generate button starts the creation of the key pair. During the generation of the keys it is necessary to move the mouse what creates some kind of randomness.
The generated keys are stored in separate files by the save key buttons. You should save both keys securely. Especially the private key should not be made accessible to others and must not be distributed to any other system.
The stored public key must now be appended to the authorized_keys file of the SSH server. Using a Linux client ssh-copy-id takes care on this and distributes the public key to the remote system. For Windows such a tool is not available and it must be done manually.
Login with PuTTY to your SSH server with the account for that you intend to copy the public key.
Check if the ~/.ssh directory is present in your home directory. If not create it and grant read/write access to the user.
chmod 700 .ssh
Change into the ~/.ssh directory and create the file authorized_keys by opening it with e.g. nano
Select the public key in the PuTTY key generator windows edit control and copy it to the clipboard. Select the PuTTY Window with the authorized_keys file opened and insert the public key to the file by a right mouse click. Save the authorized_keys file when leaving nano with CTRL+X.
Finally, the permissions of the authorized_keys file must be set.
In the last step the private key must be linked to the PuTTY session and the changes have to be saved for the current session name.
When you connect to the remote server the next time you will get a command prompt immediately without the need of a password.
Now we want to realize automated calls. PuTTY comes with the Plink application which is the actual replacement of the SSH command under Linux. For automatic calls it is not necessary to start the graphical user interface but Plink in the command line instead.
In the example above we connected to RPI-AZ-1 and executed the LucidIoCtrl command which requests device information form the analog input module connected to /dev/ttyACM0.
In this article I described how a SSH connection can be set up and automated requests can be sent to our LucidControl USB IO modules. On Linux computers the SSH tools are available and I show how to install and how to use them on the client and server computers. In a next step I described how to establish a SSH connection with a Windows client and PuTTY.
The final goal of the article is to show how to login to a remote system by using key pairs which grant secure access to the remote system without the need of entering a password. This makes automatic requests possible executing a program on a remote computer.
By accessing the LucidIoCtrl command on the remote server extends it with device server functions. In our case the Raspberry Pi is used as a device server making the USB IO modules available e.g. over the Internet. For this it only needs the standard SSH software package installed and the LucidIoCtrl command line tool.
In the next articles I will describe how a device server can be realized without any third party program installed on the remote server. Linux has standard tools which allow sharing devices by directing traffic over the network directly to a Linux device like our USB IO modules.