Home > Software engineering >  How to get some information from remote machine by using shell script?
How to get some information from remote machine by using shell script?

Time:01-13

First of all, sorry for dummy question.

I would like to get distribution information from remote target by using following sample code under shell script. My local machine is Ubuntu16.04 and remote target is Ubuntu20.04(192.168.100.15). However, when I run shell script, the $distribution value is ubuntu16.04.

Why the value is not Ubuntu20.04? and How should I modify my code correctly?

ssh [email protected] "distribution=$(. /etc/os-release;echo ) && echo $distribution"

CodePudding user response:

Check the contents of /etc/os-release to find out which variables are available, then echo one of those. For example:

$ ssh [email protected] '. /etc/os-release; echo $PRETTY_NAME'
Ubuntu 20.04.3 LTS

If you want to populate the distribution variable on your local machine, you need to use the $(...) construct locally:

$ distribution=$(ssh [email protected] '. /etc/os-release; echo $PRETTY_NAME')
$ echo $distribution
Ubuntu 20.04.3 LTS

By the way, giving ssh access to the root user is frowned upon nowadays. And using root in this case is entirely unneccesary anyway, because /etc/os-release is world-readable to any user.

CodePudding user response:

Use lsb_release:

ssh [email protected] 'lsb_release -ds'

LSB means Linux Standard Base. The command should be available on every Linux system.

  •  Tags:  
  • Related