Search code examples
bashscriptingparameter-passingpssh

PSSH command with different arguments to multiple hosts


I'm trying to write a bash script using PSSH which sends the same command but different arguments depending on the host. The host name and arguments will be pulled from a different file 'list.txt'.

An example of a 'list.txt' file would look like this:

10.0.0.1;'hello';'world'
10.0.0.2;'goodbye';'everyone'
10.0.0.3;'thank';'you!'

An example of what I currently have (but unfortunately isn't working) is shown below:

#!/bin/bash

# grab the list items and make them into a variable to be able to parse
actionList=$(</root/scripts/list.txt)

# parse out host name for pssh
host_name="$( cut -d ';' -sf 1 <<< "$actionList" )";

# parse out the first argument 
argument1="$( cut -d ';' -sf 2 <<< "$actionList" )";

# parse out the second argument
argument2="$( cut -d ';' -sf 3 <<< "$actionList" )";

# pssh command that creates a new file on each server with their respective argument1 and argument2 
pssh -i -AH $host_name -t 300 -O StrictHostKeyChecking=no "$(argument1) ' and ' $(argument2) >> arg1_and_arg2.txt" 

I'm pretty sure cutting the $actionList variable is not giving me what I want, but what I'm really stuck on is whether that pssh command will run correctly for every item in 'list.txt' after I've parsed out the correct strings from $actionList.

Is there a way to make same command, changing arguments from file work with PSSH? Is there a better program to do this with? If so how? Any help is appreciated.

Thanks!

P.S. If I formatted or did anything wrong with this post, I apologize. StackOverflow is usually my last resort so I don't use it often. Again, thanks for any help/advice!


Solution

  • I think you are better off simply using a loop that runs ssh for each line of the input file.

    while IFS=";" read host arg1 arg2; do
        echo "$arg1 and $arg2" | ssh "$host" "cat > arg1_and_arg2.txt" &
    done < list.txt