我制作了剧本:
for server in ${servTab[*]}; do
echo $server >> /var/www/html/df.txt
/usr/bin/expect <<EOF
spawn ssh $server df | grep /appl/ | awk '{print \\\$4}' | sed s/%// >> /var/www/html/df.txt
expect "password"
send "$pass\r"
expect "$ "
EOF
但运行后我有错误:
spawn ssh server-1-lin df | grep /appl/ | awk '{print $4}' | sed s/%// >> /var/www/html/df.txt
ksh: /var/www/html/df.txt: cannot create [No such file or directory]
仅当它看起来如下时才有效:
for server in ${servTab[*]}; do
echo $server >> df.txt
/usr/bin/expect <<EOF
spawn ssh $server df | grep /appl/ | awk '{print \\\$4}' | sed s/%// >> df.txt
expect "password"
send "$pass\r"
expect "$ "
EOF
但始终在用户的主目录中创建文件“df.txt”,无论目录脚本是什么。谢谢你的帮助。
答案 0 :(得分:0)
这里的简单答案是停止使用expect
;它会让你的生活更加复杂,而且在目前的情况下,没有令人信服的优势。
#!/usr/bin/env bash
# make sure this is an array
servTab=( serverOne serverTwo serverThree )
# Advice: Stop passing passwords around and use SSH agent authentication.
[[ $pass ]] || read -s -r -p "Password: " pass
# open your output file *once*; truncate it when that happens.
exec 3>/var/www/html/df.txt
for server in "${servTab[@]}"; do
echo "$server" >&3
SSHPASS="$pass" sshpass -e "$server" df \
| awk '/appl/ { gsub(/%/, ""); print $4 }' \
>&3
done