shell script to ssh into another server and execute a script in that 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.
shell script to ssh into another server and execute a script in that server
HI,
i have 6 servers in my testing env. I want to write a script to stop tomcat server in all boxes by logging into one box instead of logging into each and every box.
please help me with shell scripting, ssh and execute a script in all other boxes.
The custom port part is easy, but for the authentication you really need to be using SSH keys instead of passwords. SSH keys authentication is not only the established best practice, it is also the easiest way to trigger a remote script.
See the command="command" description in "man sshd" in the section "AUTHORIZED_KEYS FILE FORMAT"
1) write script
2) upload to remote servers
3) generate and install a key pair for each server
4) modify public key to trigger script
5) make a shortcut for each server in ~/.ssh/config
I don’t know how to stop tomcat, but to execute a script on a remote server, I am aware of two options.
Option 1: Put the script in a file, copy the file to the remote system, then execute it remotely.
Code:
vi script.sh
for i in server{1..6}
do
scp script.sh ${i}:
ssh $i sh script.sh
done
Option 2: If the script consists of few commands, you can execute it this way:
Code:
for i in server{1..6}
do
ssh $i sh -c "command1; command2; command3"
done
Option 2 (BTW I changed the "quotes" to ASCII!) has the disadvantage that you have to consider local evaluation then remote evaluation.
Option 3: pass script via stdin
Code:
for i in server{1..6}
do
<scipt.sh ssh $i /bin/bash -s
done
There is no local evaluation. It even takes script arguments after the -s
The /bin/bash should match the #!/bin/bash shebang in the script; for example use /bin/ksh for a #!/bin/ksh script.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.