LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-12-2019, 01:47 AM   #1
nithi28
LQ Newbie
 
Registered: Feb 2019
Posts: 1

Rep: Reputation: Disabled
Post 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.

* SSH with password and custom port

Last edited by nithi28; 02-12-2019 at 01:49 AM.
 
Old 02-12-2019, 02:11 AM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 2,603

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
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
 
Old 02-12-2019, 02:46 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 10,677
Blog Entries: 7

Rep: Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671
Quote:
Originally Posted by nithi28 View Post
* SSH with password and custom port
i'm sure
Code:
man ssh
can answer that part?
 
Old 02-12-2019, 02:54 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,663
Blog Entries: 3

Rep: Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644Reputation: 1644
Quote:
Originally Posted by nithi28 View Post
* SSH with password and custom port
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
 
Old 02-12-2019, 11:17 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,001

Rep: Reputation: 437Reputation: 437Reputation: 437Reputation: 437Reputation: 437
Quote:
Originally Posted by berndbausch View Post
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.
 
Old 02-13-2019, 01:07 AM   #6
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 2,603

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
Quote:
Originally Posted by MadeInGermany View Post
Option 2 (BTW I changed the "quotes" to ASCII!) has the disadvantage that you have to consider local evaluation then remote evaluation.
True. Though this may be a plus, depending on the task at hand.

I promise I will work on my quotes :P
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script with Menu and sub menu and in sub menu execute another shell script SHWE Linux - Newbie 9 11-03-2018 07:19 PM
using ssh to execute dozens of shell commands in shell script hilou Programming 7 04-20-2016 05:25 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
Execute a command , reboot and then execute another command ganeshp@moris.org Linux - Newbie 3 12-03-2008 01:51 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM

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

All times are GMT -5. The time now is 09:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration