LinuxQuestions.org
Visit Jeremy's Blog.
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 02-12-2019, 12: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 12:49 AM.
 
Old 02-12-2019, 01:11 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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, 01:46 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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, 01:54 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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, 10:17 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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, 12:07 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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



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

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

All times are GMT -5. The time now is 08:21 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
Open Source Consulting | Domain Registration