ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using LabView to SSH to a remote Linux unit

I need to remotely connect to a Linux machine using SSH

 

IP: 192.168.5.xx,

Port: 22

User: *****

Password: *****

 

Then then I want to send some commands "lsusb[...]" and "sudo lspci[...]", and read back the responses.

 

Are there any prewritten vi's for this, or can anyone share what they have done to overcome this?

LABOR OMNIA VINCIT
0 Kudos
Message 1 of 8
(1,019 Views)

There are a couple of SSH packages in VIPM. I haven't tried them myself though.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 2 of 8
(978 Views)

I've used the Putty command-line tools for this in the past.

https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

 

The device we were talking to needed some RSA keys for security, and the customer was able to provide .ppk files designed for Putty.

 

The command line for SSH w/ security keys was (example):

@echo OFF
echo n | "C:\Program Files\PuTTy\plink.exe" -i "C:\SSH\keys.ppk" root@192.168.1.2 echo "Hello World!"

 

Or without all the security key stuff:

@echo OFF
"C:\Program Files\PuTTy\plink.exe" root@192.168.1.2 echo "Hello World!"

 

 

There's also a command line tool for SCP file transfer, example:

@echo OFF
echo n | "C:\Program Files\PuTTy\pscp.exe" -scp -i "C:\SSH\keys.ppk" "C:\SSH\filetocopy.txt" root@192.168.1.2:/root/scripts/filetocopy.txt

 

It looks like they added native SSH into LabVIEW in version 2021. I've never played with it -- we are still on 2019 and I believe Putty was NI's recommended method back then.

0 Kudos
Message 3 of 8
(976 Views)

LabVIEW 2021 includes support for SFTP and SSH. You can find the palettes under Data Communication >> Protocols.

 

0 Kudos
Message 4 of 8
(942 Views)

There is only SFTP palette no SSH, but I am able to use this to connect. There is nothing in the palette for send receive so I can issue commands and read the response. In serial this is read write.

 

 

LABOR OMNIA VINCIT
0 Kudos
Message 5 of 8
(803 Views)

LabVIEW opens and closes the connection for each command, you would have to dig in deeper or use a different API if you want to reuse a connection. 

 

The VIs are here: <labview>\vi.lib\net\SSH\NI_SSH.lvlib

LabVIEW SSHLabVIEW SSH

 

Works for me on 64 bit LabVIEW.

 

Tim Cannon uses LabSSH2 for his RT SSH API commands, might be worth a look for you: https://gitlab.com/cannontim/rt-ssh-api

 

 

0 Kudos
Message 6 of 8
(794 Views)

Right, so I am using LIBSSH2 for LabVIEW Toolkit, and can successfully login, send commands and read the response. Where I am stuck now is using "sudo", as it will expect the password again, not sure how I do this. I am using a string array into a loop to send commands, but don't know how to get the password in and read the required respons.

LABOR OMNIA VINCIT
0 Kudos
Message 7 of 8
(747 Views)

When using the SSH.NET library, I used the technique in this stackoverflow post:

 

https://stackoverflow.com/questions/40918265/how-to-su-with-ssh-net

 

Specifically, you echo the password and then pipe that to the sudo command with a -S argument and then the command to execute.

 

PhillipBrooks_0-1765425659930.png

 

If you log your session, the password will be included in clear text. Not generally a good idea.

 

Alternately, if you have multiple commands to run and evaluate as super user, you can use interactive mode with sudo -i

 

You will need to perform a read after writing the command and look for a message requesting the  password (I used regex with this pattern \[sudo\] password for (\w+)\: ).

 

If found, you send the password with a new line appended. If you have previously elevated the session to super user, you will not get a prompt for the password (regex will return nothing)

 

PhillipBrooks_1-1765426299840.png

 

Once you are done, you can send a ^D or a logout command to revert back the the standard user shell with standard permissions.

 

 

0 Kudos
Message 8 of 8
(690 Views)