Вы можете использовать sshpass, но лучше использовать ansible . Пример использования sshpass:
# vim server.list
192.168.0.100
192.168.0.101
# apt-get install sshpass
$ vim script.sh
#!/bin/bash
while read -r line
do
echo "running $line"
SSHPASS=PASSWORD sshpass -e ssh-copy-id USERNAME@"$line" -o "StrictHostKeyChecking no"
done < "server.list"
Запустить скрипт:
$ sh script.sh
running 192.168.0.100
...
Number of key(s) added: 1
running 192.168.0.101
...
Number of key(s) added: 1
Расширение из ссылки , которую я разместил в комментариях ...
Эта функция будет оценивать ТОЛЬКО те переменные, которые вы запрашиваете.
read_config () { # read_config file.cfg var_name1 var_name2
#
# This function will read key=value pairs from a configfile.
#
# After invoking 'readconfig somefile.cfg my_var',
# you can 'echo "$my_var"' in your script.
#
# ONLY those keys you give as args to the function will be evaluated.
# This is a safeguard against unexpected items in the file.
#
# ref: https://stackoverflow.com/a/20815951
#
# The config-file could look like this:
#-------------------------------------------------------------------------------
# This is my config-file
# ----------------------
# Everything that is not a key=value pair will be ignored. Including this line.
# DO NOT use comments after a key-value pair!
# They will be assigend to your key otherwise.
#
# singlequotes = 'are supported'
# doublequotes = "are supported"
# but = they are optional
#
# this=works
#
# # key = value this will be ignored
#
#-------------------------------------------------------------------------------
shopt -s extglob # needed the "one of these"-match below
local configfile="${1?No configuration file given}"
local keylist="${@:2}" # positional parameters 2 and following
if [[ ! -f "$configfile" ]] ; then
>&2 echo "\"$configfile\" is not a file!"
exit 1
fi
if [[ ! -r "$configfile" ]] ; then
>&2 echo "\"$configfile\" is not readable!"
exit 1
fi
keylist="${keylist// /|}" # this will generate a regex 'one of these'
# lhs : "left hand side" : Everything left of the '='
# rhs : "right hand side": Everything right of the '='
#
# "lhs" will hold the name of the key you want to read.
# The value of "rhs" will be assigned to that key.
while IFS='= ' read -r lhs rhs; do
# IF lhs in keylist
# AND rhs not empty
if [[ "$lhs" =~ ^($keylist)$ ]] && [[ -n $rhs ]]; then
rhs="${rhs%\"*}" # Del opening string quotes
rhs="${rhs#\"*}" # Del closing string quotes
rhs="${rhs%\'*}" # Del opening string quotes
rhs="${rhs#\'*}" # Del closing string quotes
eval $lhs=\"$rhs\" # The magic happens here
fi
# tr used as a safeguard against dos line endings
done <<< $( tr -d '\r' < $configfile )
shopt -u extglob # Switching it back off after use
} # ---------- end of function read_config ----------
У вас опечатка в коде.
Переменная в файле конфигурации называется PORT_INDEX
, но вы пытаетесь отобразить PORT_IXDEX
, которая не определена.