Reboot remote Linux server over ssh only if kernel was updated

Hi,

I have gone through the link https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/

My requirement is that I want to push ssh command to remote server, So that will check current and installed kernel version on the remote server and have to reboot.

I have tried “su --session-command” combination also.

ssh  anand@remote_host "su -lc 'if [ "`rpm -q kernel | tail -1`" == "kernel-$(uname -r)" ]; then  echo  "System is already running with latest kernel version"; else echo "reboot"; fi'"

Its taking variables where I pushing the command but I should take variables from remote server.

Kindly help.

Thanks & Regards,
Anand R

First you create a script called checkversion.sh on your workstation:

#!/bin/bash
rv="$(rpm -q kernel)"
cv="kernel-$(uname -r)"
[ "$rv" == "$cv" ] && echo "The latest version of the Linux kernel running on this box." || echo "Reboot the box"

Copy this script to remote server:

scp  checkversion.sh  anand@remote_host /home/anand/
ssh anand@remote_host chmod +x /home/anand/checkversion

Now run that script:

ssh anand@remote_host bash /home/anand/checkversion

To run it as sudo so that you can actually reboot the box add user anand to sudo on remote server and run it as follows:

ssh -t anand@remote_host sudo /home/anand/checkversion

To actually reboot the box modify script as follows:

#!/bin/bash
rv="$(rpm -q kernel)"
cv="kernel-$(uname -r)"
[ "$rv" == "$cv" ] && echo "The latest version of the Linux kernel running on this box." || /sbin/reboot

A better solution is to use tool like Ansible that allows to reboot the box when the Linux kernel updated on fly.

Thank you for your reply.

Am not authorized to add my user into sudoers. But I know the root password of the server.

Is any other way to push the command without creating script.

Thanks & Regards,
Anand R