04-19-2020 11:12 AM - edited 04-20-2020 01:32 AM
Hi,
I have an application in which I have to communicate to a remote device through SSH protocol. I have implemented it thanks to expect scripting executed from LabVIEW with the system exec.vi. However, if I implement in the script the connection and I want to keep the channel opened in order to insert the rest of the commands directly through the system exec.vi, it is not possible.
Now my question. Is there any method to keep opened the SSH connection after the expect script is executed from LabVIEW with the system exec.vi? This would avoid to call an expect script each time I want to send a new command to the SSH server and to do all the following operations each time: Open the SSH connection, send the required command and close the SSH connection. An example:
This first script doesn't close the connection by itself, but after execute it from system exec.vi, it is impossible to continue with the communication.
#!/usr/bin/expect -f
set IPADDRESS "<IPADDRESS>"
set USERNAME "<USERNAME>"
set PASSWORD "<PASSWORD>"
set timeout -1
spawn ssh -tt $USERNAME@$IPADDRESS
match_max 100000
##If it is the first time you connect to the ssh server enable the following lines
#expect "Are you sure you want to continue connecting (yes/no)?"
#send "yes\r"
expect "*password: "
send -- "$PASSWORD\r"
expect "device@~$ "
interact
This second script do all the operations, but I have to modify it (I do it programmatically from LabVIEW), and call it every time I want to send a new command.
#!/usr/bin/expect -f
set IPADDRESS "<IPADDRESS>"
set USERNAME "<USERNAME>"
set PASSWORD "<PASSWORD>"
set COMMAND "<COMMAND>"
set timeout -1
spawn ssh -tt $USERNAME@$IPADDRESS
match_max 100000
##If it is the first time you connect to the ssh server enable the following lines
#expect "Are you sure you want to continue connecting (yes/no)?"
#send "yes\r"
expect "*password: "
send -- "$PASSWORD\r"
expect "device@~$ "
send -- "$COMMAND\r"
expect "device@~$ "
send -- "exit\r"
interact
By the way, if you work with scripts called form system exec.vi, I recommend you to disable the "wait until completion" option and to print the output to file. In this way you will prevent that the system exec.vi blocks the program execution if any error occurs and you have the error log printed in your file, which you can read programmatically later. For instance:
/home/lvuser/scripts/script.exp |& tee /home/lvuser/scripts/log.txt
The same is applicable for SFTP, in fact I have another application in which I use the same method to communicate with a SFTP server from cRIO.
I have recently discovered a Git Hub repository with a LabVIEW library that implements SSH and SFTP protocols and compatible with NI Linux RT targets! I am sure that is very helpful for the community. All my acknowledgments to volks73, great job! (If you are present in the forum, you should post it in LabVIEW APIs section!)