Hi,
I have the script below. When i assign SSH_COMMAND
to "ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}"
and then execute it as ${SSH_COMMAND}
I get the following error:
ssh: Could not resolve hostname sudo: Name or service not known
ssh: Could not resolve hostname sudo: Name or service not known
But when I run the command directly from the script, it works as expected. The script below is running as expected. Can you guys help?
#!/bin/bash
#set -x
function usage {
echo "USAGE: ${0} [-nsv] [-f FILE] COMMAND"
echo "-f FILE Use to override the default file"
echo "-n Use for dry Run"
echo "-s Use to run the command with sudo"
echo "-v Use for verbose"
}
SERVERFILE="/vagrant/servers"
if [[ ! -e "${SERVERFILE}" ]]; then
echo "File ${SERVERFILE} does not exist"
exit 0
fi
if [[ ${#} -lt 1 ]]; then
usage
fi
if [[ ${UID} -eq 0 ]]
then
echo "Do not execute this as root, use -s instead"
fi
while getopts 'nsvf:' opt
do
case "${opt}" in
n)
DRY_RUN="true" ;;
s)
AS_SUDO="sudo" ;;
f)
SERVERFILE=${OPTARG} ;;
v)
echo "verbose mode" ;;
esac
done
shift "$(( OPTIND-1 ))"
COMMANDS=${*}
SSH_COMMAND="ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}"
if [[ ! -z ${DRY_RUN} ]]
then
echo "DRY RUN: ${SSH_COMMAND}"
else
for SERVER in $(cat ${SERVERFILE})
do
#${SSH_COMMAND}
ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}
done
fi