我为课程制作了这个脚本。它通过 ssh 从脚本的参数在文件中指定的多个远程服务器上执行命令:
#!/bin/bash
# The server file. Can be changed with the -f argument
SERVER_FILE='/vagrant/servers'
# The function to check if the chosen SERVER_FILE exists
filecheck() {
if [[ ! -e $SERVER_FILE ]]; then
echo "The file $SERVER_FILE does not exist." >&2
exit 1
fi
}
# The usage statement
usage() {
echo "usage $0 -vsn -f FILE 'COMMAND'"
echo " -v Verbose mode"
echo " -s Run command as sudo on remote server"
echo " -n Dry run, commands not actually executed"
echo " -f FILE Selects a different file other than /vagrant/servers"
exit 1
}
# The verbose mode text things
say() {
if [[ $VERBOSE = 'true' ]]; then
echo "$@"
fi
}
# The ssh command
sshing() {
ssh -o ConnectTimeout=2 $SERVER $@
}
# User executing the command should not be root
if [[ $UID -eq 0 ]]; then
echo "You should not execute this script with sudo or as root" >&2
echo "Use the -s argument if you want sudo powers" >&2
exit 1
fi
# DRYMODE is sshing by Default
DRYMODE='sshing'
#check to see if file SERVER_FILE exists
filecheck
# The options for the script
while getopts vsnf: OPTION; do
case $OPTION in
v)
echo "Verbose mode on"
VERBOSE='true'
;;
s)
say "Sudo mode"
SUDO='sudo'
;;
n)
say "Dry run mode"
DRYMODE='echo'
DRYRUN='DRY RUN: '
echo "DRY RUN MODE ON: "
echo
;;
f)
say "Different file mode"
SERVER_FILE=${OPTARG}
#check to see if file SERVER_FILE exists
filecheck
;;
*)
usage
;;
esac
done
echo
# shifts so that the options are removed from the list of arguments
shift $((OPTIND-1))
#Set a variable for the rest of the arguments, as a command
COMMAND="${@}"
# Checks if the user provided any arguments apart from the optinos
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
# Executes the commands
for SERVER in $(cat ${SERVER_FILE}); do
say "Executing ${COMMAND} on ${SERVER}:"
$DRYMODE $DRYRUN $SUDO ${COMMAND} 2> /dev/null
CMDEX=$?
# if the exit status is 255, something is wrong with the server or is unreachable
if [[ $CMDEX -eq 255 ]]; then
echo "The server you're trying to reach does not exist or is unreachable. Aborting." >&2
exit 1
fi
# if the exit status is non 0 and non 255, something is wrong with the command
if [[ $CMDEX -ne 0 ]]; then
echo "Invalid command ${COMMAND} or wrong syntax. Aborting." >&2
exit 1
# if the exit status is non 0 and non 255, something is wrong with the command
fi
say "Command ${COMMAND} executed successfuly."
done
exit 0
它对于简单的命令(例如ls
,ps
甚至adduser test
)完美地工作,但如果我给它任何包含双引号的命令,它就会中断,除非我单引号整个命令。
现在我不知道这是否是我的代码中的错误或其他问题,但我无法通过此管道命令。
所以这个命令不起作用:
[vagrant@admin01 vagrant]$ ./run-everywhere.sh -sv 'echo 1 | passwd --stdin test4'
如果我用 \| 逃离管道它只是字面上将其写为\|。这个其他命令也不起作用:
[vagrant@admin01 vagrant]$ ./run-everywhere.sh -sv 'echo "1" | sha256sum > file1'
编辑:
我发现管道无法工作的问题:如果命令需要 sudo 权限,我还必须在管道之后编写 sudo 。这个是这样工作的:
[vagrant@admin01 vagrant]$ ./run-everywhere.sh -sv 'echo 1 | sudo passwd --stdin test4'
但我仍然无法重定向。
答案1
尝试这个:
sshing () {
ssh -o ConnectTimeout=2 "$SERVER" "$@"
# ................................^..^ crucial quotes
}
# ...
cmd="$*"
# ...
while read -r SERVER; do
say "Executing ${COMMAND} on ${SERVER}:"
$DRYMODE $DRYRUN $SUDO sh -c "${COMMAND}" 2> /dev/null
# .....................11111.2..........2
# 1. run with a shell to enable redirections and pipe
# 2. crucial quotes
# ...
done < "$SERVER_FILE"
使用 sudo 在 shell 中运行命令将允许整个管道以提升的权限执行。
另外,您应该改掉使用全大写变量名的习惯。有一天,您会不小心覆盖 PATH,然后想知道为什么您的脚本被破坏了。
答案2
发现问题了。如果我使用 sudo 权限运行脚本来自行处理某些文件,它将在 root 的名称和组下创建文件,这意味着我对该文件没有权限。