Shell Script to Find Kernel Version from Multiple Servers

In this tutorial, we learn to write a shell script that will login to multiple servers via ssh and get kernel version. List of all the servers are placed in 'server_list.txt' file.

The script will login to each server in the list and run uname command. The output will be written to variable and after into a file.

This script helps system administrators who manage large Linux servers, to take reports of what Linux versions running. Here I have used 'uname -r' but you can edit and use your own command as per your requirements.

Script to get kernel version

The following script help to get kernel information from multiple Linux servers.

#we user variable serverlist to keep there path to file with server names
#we write in variable all server list
servers=`cat $serverlist`
#we use variable result to keep there path to file with result

#this print header to file with resilt using \t\t to add 2 tab symbols
echo -e "Servername \t\t kernel version"> $result
#this get each line of serverlist one by one and write to server variable
for server in $servers
#this login to server by ssh and get uname -r
kernel=`ssh root@${server} "uname -r"`
#this write server name and kernel version separated by 2 tab to result file
echo -e "$server \t\t $kernel" >> $result
#end of for loop.

server_list.txt file
# cat server_list.txt

Shell Script Output

centos_node1@bobbin:~/Documents/Work/Bobbin$ cat result.txt
Servername kernel version
dev 3.3.8-gentoo
web1 3.2.12-gentoo
svn 3.2.12-gentoo

Read Also:

2 Comments... add one

Leave a Comment