Results 1 to 3 of 3

Thread: Bash scrip which ping several servers and execute command

  1. #1
    Join Date
    Jul 2014
    Beans
    8

    Bash scrip which ping several servers and execute command

    Hello,

    So far I have this script which pings 7 servers and execute an command if the server is alive. What I need is to put parameter which will change in the command according to server. For example:
    ping SERVER 1 -> response with OK -> execute command /snmptrap -v 2c -c public server_1 param_1 ....
    ping SERVER 2 -> response with OK -> execute command /snmptrap -v 2c -c public server_2 param_2 ....
    ping SERVER 3 -> response with OK -> execute command /snmptrap -v 2c -c public server_3 param_3 ....
    My problem is how to put in array those parameters ( 1; 2; 3; ) and in the command call them according to server which is alive. If server 1 is alive execute command with parameter 1 ... server 2 with parameter 2 and so on.

    Here is the script that I ping the servers.
    Code:
    #!/bin/bash
    servers=( "ip_serv_1" "ip_serv_2" "ip_serv_3" )
    
    for i in "${servers[@]}"; do
        ping -c 1 $i > /dev/null && /snmptrap -v 2c -c public ...
    done
    EDIT:

    Can I do something like this?
    Code:
    #!/bin/bash
    servers=( "ip_serv_1" "ip_serv_2" "ip_serv_3" )
    cmd=("1" "2" "3" "4")
    
    for i in "${servers[@]}"; do
        ping -c 1 $i > /dev/null && /snmptrap -v 2c -c public ... $cmd...
    done
    But I guess if some host is down will mess the commands like this? SOmehow each $param must be assigned to corresponded host..
    Last edited by vinsbg; July 30th, 2014 at 08:08 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,791

    Re: Bash scrip which ping several servers and execute command

    Google for Parallel SSH.

  3. #3
    Join Date
    Jul 2014
    Beans
    8

    Re: Bash scrip which ping several servers and execute command

    Quote Originally Posted by HermanAB View Post
    Google for Parallel SSH.
    Thank you for replay!
    I don't understand why I need this. I don't want to execute command from script on remote host. I will execute it on local.

    Edit: I'm not shell-bash-script-guru and just starting to learn. That is why I dont understand some things.
    Last edited by vinsbg; July 30th, 2014 at 08:17 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •