LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-06-2013, 09:20 AM   #1
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Lightbulb how to run a shell script from remote server to local server.


Hi,

I am using Solaris.
SunOS <hostname> 5.10 Generic_144488-17 sun4v sparc SUNW,Sun-Blade-T6340

We are preparing to keep all the scripts on one particular remote server.
And we have to use those scripts from there to run on local servers.
How will I achieve this?

I searched this below mentioned link from google:
http://unix.stackexchange.com/questi...-local-machine

In this forum they were talking about sshing the server and copying the script from remote server to local server. But is there any other way to achieve this? Please share some ideas!
 
Old 08-06-2013, 10:49 AM   #2
bypper
LQ Newbie
 
Registered: Jun 2013
Posts: 8

Rep: Reputation: Disabled
Perhaps you can download the script core with Furl and Pipe to bash. Something like this...

Curl http://remoteurl/script.sh | bash
 
Old 08-06-2013, 10:52 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,329

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
I suppose the simplest solution would be to have the scripts on a network filesystem , mount that on local server(s)

I take that back, bypper's is simpler

Last edited by Firerat; 08-06-2013 at 10:53 AM.
 
2 members found this post helpful.
Old 08-06-2013, 11:04 AM   #4
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
We don't have to download the script from remote server. Is there any other way?
 
Old 08-06-2013, 11:12 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
I have this code mentioned below:

Code:
#!/bin/sh

#########################################################################################
#                                                                                       #
#                               DAS startup script                                      #
#                                                                                       #
#########################################################################################

current-das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log

echo Hostname of this server is `hostname`
echo
echo Today date is `date`
echo

echo "===== Now going to start amqbroker process. =====" >> /logs/log.out
        cd $camq
        . ./set-path.sh					#Setting the environment for ant command.
        ant -f run.xml startup				#Running the amqbroker process.

        tail $amqlog | grep start-up\ finished > /dev/null			#Finding the start-up finished string in broker.log file.
        if [ $? -eq 0 ]; then
                echo "amqbroker process started successful." >> /logs/log.out
        else
                echo "amqbroker process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi


echo "===== Now going to start message-switch process. =====" >> /logs/log.out
        cd $cms
        . ./set-path.sh					#Setting the environment for ant command.
        ant -f ms.xml startup				#Running the message-switch process.

        tail $mslog | grep start-up\ finished > /dev/null			#Finding the start-up finished string in ms.log file.
        if [ $? -eq 0 ]; then
                echo "message-switch process started successful." >> /logs/log.out
        else
                echo "message-switch process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi


echo "===== Now going to start das process. =====" >> /logs/log.out
        cd $cdas
        . ./set-path.sh					#Setting the environment for ant command.
        ant -f das.xml startup				#Running the das process.

        tail $daslog | grep start-up\ finished > /dev/null			#Finding the start-up finished string in das.log file.
        if [ $? -eq 0 ]; then
                echo "das process started successful." >> /logs/log.out
        else
                echo "das process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
Now, this script is kept on remote server and I have to run it on local server. But I have to run this script without copying it to local server. How can I achieve this?
This is the code above and what modification can be done in this code so that I can run this script from remote server to local server?

I have an idea that using ssh we can do but how to do that, what modification will be done in this script for that, that I'm not getting.
 
Old 08-06-2013, 12:58 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Satyaveer Arya View Post
Now, this script is kept on remote server and I have to run it on local server. But I have to run this script without copying it to local server. How can I achieve this?
How about running the script on a mounted network filesystem of the remote server? e.g.
Code:
mount.nfs etc /mnt/remote
bash /mnt/remote/shared-dir/something.sh
You could also use ssh to fetch the script like this and run bash.
Code:
echo "cat /path/to/file.sh" | ssh somewhere
But you have to automate it perhaps by the use of public key authentications and not by passwords. The local server should only be able to access the home directory of an account with limited privileges and capabilities.
Code:
bash <(echo "cat /path/to/file.sh" | ssh ...)
Since you're executing a pipe-like file, it's a good idea to wrap all your methods on a function before finally executing the starter function. This would prevent problems just in case a connection problem occurs during read of the script.
Code:
#!/bin/bash

function other_functions {
   ...
}

function main {
   ... start everything here
   ... you could call other_functions as well
}

main  ## safe call
(EOF)
---- Edit ----

Sorry the proper ssh command should actually be:
Code:
ssh remote-server -- cat /path/to/file.sh
So to execute the file locally, do this:
Code:
bash <(exec ssh remote-server -- cat /path/to/file.sh)
Though I'm still not sure how other messages could affect it. Let's hope ssh sends it to stderr instead. So you could supress it with 2>/dev/null. Or you could send it to a logfile instead.
Code:
bash <(exec ssh remote-server -- cat /path/to/file.sh 2>/dev/null)

Last edited by konsolebox; 08-06-2013 at 01:08 PM.
 
Old 08-06-2013, 02:16 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,329

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
just wanted to clarify "downloading"
Whatever you do the local server must "download" the script

bypper's example uses curl,
by default curl "outputs" to stdout

which is being piped to bash

the script is not saved to the filesystem.

Anyway...

Code:
current-das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log
where are these directories ?
are they on the local machines, or on the remote server?
if on the remote, then network filesystem is what you want/need.
But you will want to get those logs "unique" to each local machine, since you are greping a tail ( what if two "clients" were running the script at same time )

assuming they are "local"
Is the "no download" requirement for version control?
If so you could use rsync to keep the local copies in sync with 'master' script

but even then you would still need to exercise version control on ~/current-das...

( which could also be achieved with rsync )
 
Old 08-06-2013, 02:42 PM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Hi konsolebox,

Quote:
How about running the script on a mounted network filesystem of the remote server?
We don't have to use the NFS on remote server.

And all the other methods suggested by you will definitely work.

How about putting ssh -T das@<IP_address> <<EOI and end of the script EOI.
Please make me correct if I'm wrong.
 
Old 08-06-2013, 02:45 PM   #9
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Hi Firerat,

Quote:
current-das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log
All these directories are on local server.

No download requirement for version control.
 
Old 08-06-2013, 02:50 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,329

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
Quote:
Originally Posted by Satyaveer Arya View Post
Hi Firerat,



All these directories are on local server.

No download requirement for version control.
Then I don't understand why that particular script has to be on a central server

You are going to need something on each "local" machine to get the script anyway..
So why not just have the script local?

I don't mind
It is just I know I must be missing something.
 
Old 08-06-2013, 07:28 PM   #11
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Quote:
Then I don't understand why that particular script has to be on a central server
Even I don't understand why the heck our seniors need to do all this. Making all the things complicated.

Quote:
You are going to need something on each "local" machine to get the script anyway..
So why not just have the script local?
As per the decision of our seniors.
 
Old 08-06-2013, 09:16 PM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Satyaveer Arya View Post
How about putting ssh -T das@<IP_address> <<EOI and end of the script EOI.
Please make me correct if I'm wrong.
That's not a bad idea either but you'll only send one line so I think doing any of these one line methods would be enough:
Code:
ssh server -- cat /path/to/file
echo "cat /path/to/file" | ssh server
ssh server <<< "cat /peth/to/file"

# not sure about these
ssh server -- "cat /path/to/file"
echo "cat /path/to/file" | ssh server -- bash

# a little attempt to make things a little more efficient
ssh server -- exec cat /path/to/file   # if ssh does send the arguments as command string to be executed by bash
echo "exec cat /path/to/file" | ssh server  # in cases where remote ssh always expect interaction (interactive shell), this might help to make it not interactive
Of course it would be a good idea to wrap it as a script. It would also be helpful if you're executing it on cron or somewhere not able to parse bash syntaxes.
Code:
#!/bin/bash
exec bash <(exec ssh server -- cat /path/to/file)
Or simply:
Code:
#!/bin/bash
. <(exec ssh server -- cat /path/to/file)
You could also add checks to prevent other shells from interpreting it:
Code:
#!/bin/bash
[[ BASH_VERSINFO -ge 3 ]] && . <(exec ssh server -- cat /path/to/file)

Last edited by konsolebox; 08-06-2013 at 09:18 PM.
 
Old 08-07-2013, 11:04 AM   #13
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Hi,

My seniors suggested me to make some modification in the script and to make it more modular using function. So, I come with this solution and please suggest me if it is correct.

I used ssh also in this script to ssh the remote server and user have to give username and IP address as the parameters while running the script.
And as you can see I used if-else condition in every function. I think that condition will run on local server and will give me error. So, how will I check the condition after starting the service and saving it in log file on local server.
Or I have made it correct? Please verify.

Also I want to know that how will I call the functions mentioned in the script.

Code:
#!/bin/sh

#########################################################################################
#                                                                                       #
#                               DAS startup script                                      #
#                                                                                       #
#########################################################################################


current_das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log

echo Hostname of this server is `hostname`
echo
echo Today date is `date`
echo

read -p "Do you really want to continue? (y/n) " RESP
if [ "$RESP" = "y" ]; then

amqbroker()
{
                echo "===== Now going to start amqbroker process. =====" >> /logs/log.out
        ssh $2@$3 'cd $camq && . ./set-path.sh && ant -f run.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'          #Entering into current amqbroker directory and setting environment to run ant command and then running the amqbroker process.
                if [ $? -eq 0 ]; then
                echo "amqbroker process started successful." >> /logs/log.out
        else
                echo "amqbroker process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

message_switch()
{
                echo "===== Now going to start message-switch process. =====" >> /logs/log.out
        ssh $2@$3 'cd $cms && . ./set-path.sh && ant -f ms.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'            #Entering into current message-switch directory and setting environment to run ant command and then running the message-switch process.
        if [ $? -eq 0 ]; then
                echo "message-switch process started successful." >> /logs/log.out
        else
                echo "message-switch process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

das()
{
                echo "===== Now going to start das process. =====" >> /logs/log.out
        ssh $2@$3 'cd $cdas && . ./set-path.sh && ant -f das.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'          #Entering into current das directory and setting environment to run ant command and then running the das process.
        if [ $? -eq 0 ]; then
                echo "das process started successful." >> /logs/log.out
        else
                echo "das process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

else
	exit 0
fi

Last edited by Satyaveer Arya; 08-07-2013 at 11:15 AM.
 
Old 08-07-2013, 11:29 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
For starters I would give a form like this:
Code:
#!/bin/sh

#########################################################################################
# #
# DAS startup script #
# #
#########################################################################################


current_das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log

amqbroker()
{
    echo "===== Now going to start amqbroker process. =====" >> /logs/log.out
    ssh "$2@$3" 'cd $camq && . ./set-path.sh && ant -f run.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'  # Entering into current amqbroker directory and setting environment to run ant command and then running the amqbroker process.
    if [ "$?" -eq 0 ]; then
        echo "amqbroker process started successful." >> /logs/log.out
    else
        echo "amqbroker process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
    fi
}

message_switch()
{
    echo "===== Now going to start message-switch process. =====" >> /logs/log.out
    ssh "$2@$3" 'cd $cms && . ./set-path.sh && ant -f ms.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'  # Entering into current message-switch directory and setting environment to run ant command and then running the message-switch process.
    if [ "$?" -eq 0 ]; then
        echo "message-switch process started successful." >> /logs/log.out
    else
        echo "message-switch process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
    fi
}

das()
{
    echo "===== Now going to start das process. =====" >> /logs/log.out
    ssh "$2@$3" 'cd $cdas && . ./set-path.sh && ant -f das.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'  # Entering into current das directory and setting environment to run ant command and then running the das process.
    if [ "$?" -eq 0 ]; then
        echo "das process started successful." >> /logs/log.out
    else
        echo "das process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
    fi
}

main()
{
    echo "Hostname of this server is `hostname`"
    echo
    echo "Today date is `date`"
    echo

    read -p "Do you really want to continue? (y/n) " RESP

    if [ "$RESP" = "y" ] || [ "$RESP" = "Y" ]; then
        :
    else
        exit 0
    fi
}

main
I added some quotings to make variables safe, and some other tweaks. I want to ask though. Do you intend to run the script on bash or just on a normal shell e.g. dash? Because if you have Bash some things could also be simplified a bit.

And about 1>&2 >> /logs/log.out, does that intend to send a message to stderr but have a copy of it to /logs/log.out? I would suggest creating functions that would do that as well like log_message() and log_error():
Code:
log_message() {
    echo "$1"
    echo "$1" >>/logs/log.out
}
log_error() {
    echo "$1" >&2
    echo "$1" >>/logs/log.out
}
And calling echo twice is actually faster than using tee or process subtitutions.

Btw, you haven't called any function yet in the part after the user presses y.
 
1 members found this post helpful.
Old 08-07-2013, 11:39 AM   #15
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Dehradun, Uttarakhand, India
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,412

Original Poster
Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Quote:
Do you intend to run the script on bash or just on a normal shell e.g. dash?
The main purpose is to run on Unix and Linux platform on bash, sh shell.

Quote:
And about 1>&2 >> /logs/log.out, does that intend to send a message to stderr but have a copy of it to /logs/log.out?
Yes the intention is to log all the echoed commands and errors in a single log file.

And what is the main in the last line in the above modified script by you?

Code:
if [ "$RESP" = "y" ] || [ "$RESP" = "Y" ]; then
        :
    else
        exit 0
    fi
And what does the : mean in above code?

And where do I have to include these log_message and log_error function? In the same script or in other script?

And I found on the google and got some more modification by me so far.
Please take a look!

Code:
#!/bin/sh

#########################################################################################
#                                                                                       #
#                               DAS startup script                                      #
#                                                                                       #
#########################################################################################


current_das=~/current-das
cconf=~/current-das/configuration
cdas=~/current-das/das
camq=~/current-das/amqbroker
cms=~/current-das/message-switch
mslog=~/current-das/message-switch/log/ms.log
amqlog=~/current-das/amqbroker/log/broker.log
daslog=~/current-das/das/log/das.log

echo Hostname of this server is `hostname`
echo
echo Today date is `date`
echo

read -p "Do you really want to continue? (y/n) " RESP
if [ "$RESP" = "y" ]; then

amqbroker()
{
        echo "===== Now going to start amqbroker process. =====" >> /logs/log.out
        ssh $2@$3 'cd $camq && . ./set-path.sh && ant -f run.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'          #Entering into current amqbroker directory and setting environment to run ant command and then running the amqbroker process.
                if [ $? -eq 0 ]; then
                echo "amqbroker process started successful." >> /logs/log.out
        else
                echo "amqbroker process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

message_switch()
{
        echo "===== Now going to start message-switch process. =====" >> /logs/log.out
        ssh $2@$3 'cd $cms && . ./set-path.sh && ant -f ms.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'            #Entering into current message-switch directory and setting environment to run ant command and then running the message-switch process.
        if [ $? -eq 0 ]; then
                echo "message-switch process started successful." >> /logs/log.out
        else
                echo "message-switch process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

das()
{
        echo "===== Now going to start das process. =====" >> /logs/log.out
        ssh $2@$3 'cd $cdas && . ./set-path.sh && ant -f das.xml startup | grep BUILD\ SUCCESSFUL > /dev/null'          #Entering into current das directory and setting environment to run ant command and then running the das process.
        if [ $? -eq 0 ]; then
                echo "das process started successful." >> /logs/log.out
        else
                echo "das process startup failed. Exiting!" 1>&2 >> /logs/log.out
        exit 1
        fi
}

echo "Starting amqbroker process : $(amqbroker)"
echo "Starting message-switch process : $(message_switch)"
echo "Starting das process : $(das)"

else
	exit 0
fi
 
  


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
How to run shell script on all remote client from server machine simulataneously LittleMaster Linux - Newbie 1 10-09-2012 04:42 AM
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
run a shell script on remote client from a server to find all its running process robin singh Linux - Newbie 7 10-03-2008 11:32 AM
Cannot run shell script on the remote server haresh_dba Linux - General 2 04-14-2007 09:26 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration