LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-25-2018, 02:23 AM   #1
Anand R
LQ Newbie
 
Registered: May 2018
Posts: 3

Rep: Reputation: Disabled
Push multiple commands through ssh to remote server


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.
 
Old 05-25-2018, 05:11 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
probably protecting $ with \ will help you.
 
Old 05-27-2018, 04:35 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Make a separate remote_script like
Code:
#!/bin/bash
if [ "$(rpm -q kernel | tail -1)" == "kernel-$(uname -r)" ]
then
  echo "System is already running with latest kernel version"
else
  echo "reboot"
fi
that you can test on the remote server.
(Maybe add a further indirection level of su -c to the remote_script. Better work with keys and switch to root in ssh like the following.)
The following is a script that pushes/runs the remote_script to/on each remote host:
Code:
for h in remote_host1 remote_host2
do
  ssh root@"$h" /bin/bash -s < remote_script
done
Having a separate remote script saves another level of indirection i.e. extra escapes.

Last edited by MadeInGermany; 06-04-2018 at 05:48 AM. Reason: changed wrong " to ascii
 
1 members found this post helpful.
Old 05-27-2018, 04:50 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Also, it looks like Vista8 might be mutilating the quotes, too. You need plain single ' and double " quotes not fancy ones like ‘ ’ and “ ” because the fancy ones are invalid.
 
Old 05-27-2018, 07:47 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Ah yes, and one command within the test was not in $( ) - I changed my post#3.
 
Old 06-03-2018, 10:28 PM   #6
Anand R
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
probably protecting $ with \ will help you.
Can you help me using $with \ in appropriate place? I have tried, its not working.

Am tried with your answer and am getting below error.

ssh -t anandr@xxx.xxx.x.xx $\("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'"\)
Password:
bash: System: command not found
Connection to xxx.xxx.x.xx closed.

Last edited by Anand R; 06-03-2018 at 10:32 PM.
 
Old 06-04-2018, 01:09 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Just pay lots of attention to the quotes, work your way in from the outside to the inside and make sure they are balanced. Quotes of the same kind need to be escaped when on the inside:

Code:
ssh -t anandr@xxx.xxx.x.xx "su -lc 'if [ \"\$(rpm -q kernel | tail -n 1)\" == \"kernel-\$(uname -r)\" ]; then echo \"System is already running with latest kernel version\"; else echo reboot; fi'"
or this would be more simple

Code:
ssh -t anandr@xxx.xxx.x.xx "if [ \"\$(rpm -q kernel | tail -n 1)\" == \"kernel-\$(uname -r)\" ]; then echo \"System is already running with latest kernel version\"; else echo sudo reboot; fi"

Last edited by Turbocapitalist; 06-04-2018 at 01:49 AM. Reason: escaped $ also
 
Old 06-04-2018, 01:46 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
you need to protect $ so you need to prepend a \: kernel-\$(uname -r)
But as it was already suggested you need to check quotation.
see for example here: https://unix.stackexchange.com/quest...uotes-in-shell
or here: http://wiki.bash-hackers.org/syntax/quoting
 
2 members found this post helpful.
Old 06-04-2018, 02:30 AM   #9
Anand R
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by pan64 View Post
you need to protect $ so you need to prepend a \: kernel-\$(uname -r)
But as it was already suggested you need to check quotation.
see for example here: https://unix.stackexchange.com/quest...uotes-in-shell
or here: http://wiki.bash-hackers.org/syntax/quoting
Thank you pan64 and Thank you all.

It's working perfectly what I expected.
 
Old 06-04-2018, 06:36 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
A bit more sophisticated is
Code:
# cat remote_script
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin
rev=`uname -r`
rev=${rev%%[-.][!0-9]*}

#for kernel in `rpm -qa | egrep '^kernel-((default|PAE)-)?[0-9]' | sort`
# rpm --last is more reliable than an alpabetical sort
for kernel in `rpm -qa --last | egrep '^kernel-((default|PAE)-)?[0-9]' | awk '{print $1}' | tac`
do
  krev=${kernel##*[!0-9]-}
  krev=${krev%%[-.][!0-9]*}
  if [[ $krev == $rev* ]]
  then
    printf "*$krev\n"
  else
    printf " $krev\n"
  fi
done
if [[ $krev != $rev* ]]
then
  echo "reboot needed"
fi

# ssh remotehost /bin/bash -s < remote_script
 2.6.32-696.13.2
*2.6.32-696.16.1
 2.6.32-696.23.1
reboot needed

Last edited by MadeInGermany; 06-11-2018 at 11:17 AM. Reason: replace sort with rpm --last
 
Old 06-04-2018, 07:17 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
additionally you only need to remote execute the rpm command, the other parts of the script can run on the local host (like grep):
Code:
ssh remotehost "rpm -qa" | egrep  | sort | while read kernel
do
 ....
done
or something similar
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Awesome way to run-execute multiple remote commands-shell scripts using ssh in Linux / UNIX. LXer Syndicated Linux News 0 06-21-2017 03:51 PM
How to execute multiple commands on remote server AshwiniIngale Programming 14 01-26-2013 12:54 PM
ssh remote commands dcfnef Linux - Security 1 11-15-2007 11:39 AM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM
remote ssh commands on multiple hosts evilchild Linux - Software 6 08-12-2004 10:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration