[SOLVED] Push multiple commands through ssh to remote server
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.