Search code examples
bashnginxmessagingsaas

Run a bash script on a remote server


I am developing an application which would require me to execute a bash script on a remote server when my users give me a command through a web interface.

What would be some ways to run a bash script on a remote server when I get a command on my main application server?

P.S. The bash script will need to do some tasks which only a root user can do, like, restart NGINX.


Solution

  • Take a look at this:

    Let us say you have 2 servers, A and B. You want to run a bash script on B, when you get a command on A.

    In order to make it work, we may want to setup auto-ssh between 2 server. Otherwise, it would require manual intervention, of entering password, each time.

    # Setting up auto ssh from A to B. Execute below commands from Server A.
    
    cat ~/.ssh/id_pub.rsa | ssh user_of_B@IP_of_B ">> ~/.ssh/authorized_keys"
    ssh user_ofB@IP_of_B "chmod 640 ~/.ssh/authorized_keys ; chmod 700 ~/.ssh"
    

    Above step assumes that your server A has proper directory permissions setup and server B has standard SSH configuration (which allows for auto ssh to be configured and used ["PubKeyAuthentication yes"]

    Since you have not mentioned, how you are going to process the command on A, i will assume that you have a logfile and we can read from logfile to invoke a script on A, which will invoke a script on B.

    Here is my uname -a and hostname, for reference. For now, this is our server A.

    bash$> uname -a
    Linux STATION.84station.com 3.10.0-327.36.1.el7.x86_64 #1 SMP Sun Sep 18 13:04:29 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
    bash$> hostname
    STATION.84station.com
    bash$>
    

    Now the second part, to to parse incoming log and take action.

    # Reading logfile on A, invoke a script on A to invoke a script on server B. 
    
    bash$> tail -f logfile |perl -nle 'if (m/2559/) {system("./a.sh")} '
    
    SunOS solaris 5.10 Generic_147148-26 i86pc i386 i86pc
    
    lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
            inet 127.0.0.1 netmask ff000000
    e1000g0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 index 2
            inet 192.168.2.135 netmask ffffff00 broadcast 192.168.2.255
    
    -rwxr-xr-x   1 gaurav   other         53 Oct  6 21:04 /export/home/b.sh
    ^C
    bash$>
    

    On Server A, the logfile is read, fed to a perl filter, which is looking for keyword "2559" and as soon as it matches it, it invokes the script on A, i.e "a.sh" which logs on to B and executes script "b.sh" on B, and its output is seen on A. You can put another script inside "b.sh" to execute something else or whatever you want.

    Here is the a.sh on A, which invokes b.sh on B.

    bash$> cat a.sh
    #!/bin/bash
    
    ssh gaurav@192.168.2.135 "
    
    uname -a
    echo
    /usr/sbin/ifconfig -a
    echo
    ls -lrtha /export/home/b.sh
    
    "
    bash$>
    

    You can put your commands in between quotes as shown above.

    So this is one way we can do the desired operation. Cheers!