
我為課程製作了這個腳本。它透過 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 的名稱和群組下創建文件,這意味著我對該文件沒有權限。