LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to run a shell script from remote server to local server. (https://www.linuxquestions.org/questions/programming-9/how-to-run-a-shell-script-from-remote-server-to-local-server-4175472331/)

Satyaveer Arya 08-06-2013 09:20 AM

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!

bypper 08-06-2013 10:49 AM

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

Curl http://remoteurl/script.sh | bash

Firerat 08-06-2013 10:52 AM

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

Satyaveer Arya 08-06-2013 11:04 AM

We don't have to download the script from remote server. Is there any other way?

Satyaveer Arya 08-06-2013 11:12 AM

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.

konsolebox 08-06-2013 12:58 PM

Quote:

Originally Posted by Satyaveer Arya (Post 5004112)
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)

Firerat 08-06-2013 02:16 PM

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 )

Satyaveer Arya 08-06-2013 02:42 PM

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.

Satyaveer Arya 08-06-2013 02:45 PM

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.

Firerat 08-06-2013 02:50 PM

Quote:

Originally Posted by Satyaveer Arya (Post 5004235)
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.

Satyaveer Arya 08-06-2013 07:28 PM

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. :( :(

konsolebox 08-06-2013 09:16 PM

Quote:

Originally Posted by Satyaveer Arya (Post 5004231)
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)


Satyaveer Arya 08-07-2013 11:04 AM

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


konsolebox 08-07-2013 11:29 AM

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.

Satyaveer Arya 08-07-2013 11:39 AM

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



All times are GMT -5. The time now is 08:49 PM.