问题描述
我正在编写 Bash/expect 脚本以在多个网络设备上运行 show 命令。
要识别设备,有 2 个可接受的选项:
我的代码:
#!/bin/bash
echo -e "\033[101mInsert the hostnames then hit Ctrl+D\033[0m"
cat > /temp.txt
echo
echo -e "\033[105mInsert the commands then hit Ctrl+D\033[0m"
cat > /commands.txt
echo
read -r -p $'\033[100mChoose what kind of check you would like to perform (\033[92mpre\033[39m or \033[35mpost\033[39m):\033[0m\n' prepost
prepost=${prepost:-pre}
username="uuuuuu"
pass="passssss"
############# Generate Expect Script ###########################################
echo "
#!/usr/bin/expect
set timeout 60
set hostname [lindex \$argv 0]
set username [lindex \$argv 1]
set password [lindex \$argv 2]
set send_slow {10 .001}
send_user \"\n#####\n# \$hostname\n#####\n\"
spawn ssh -oStrictHostKeyChecking=no \$username@\$hostname
expect {
timeout { send_user \"\nFailed to get password prompt\n\"; exit 1 }
eof { send_user \"\nSSH failure for \$hostname\n\"; exit 1 }
\"*#\" {}
\"*assword:\" {
send \"\$password\r\"
}
}
expect {
default { send_user \"\nCould not get into enabled mode. Password problem?\n\"; exit 1 }
\"*#\" {}
\"*>\" {
send \"ena\r\"
expect \"*assword\"
send \"\$password\r\"
}
}
expect \"*#\"
send \"term len 0\r\"
" >> /generated_expect
while read -r command
do
echo "
send \"$command\r\"
expect \"*#\"
" >> /generated_expect
done < /commands.txt
echo "expect \"*#\"
send \"!\r\"
send \"exit\r\"
close
" >> /generated_expect
chmod 700 /generated_expect
############################################################################################
echo
echo "#################################################################################"
echo
while read -r line
do
match=$(grep -i -m 1 "$line" /inventory.csv | cut -f1 -d";")
pattern="[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
if [[ $line =~ $pattern ]]; then
host="$line"
echo Logging into "$host"
nohup /generated_expect "$host" "$username" "$pass" >> /"$line"_"$prepost".txt
echo
echo -e "\033[42m\033[30m Output successfully collected\033[39m\033[0m \n"
echo "--------------------------------------------------------------------------------"
else
if [[ $match =~ $line ]]; then
host=$(grep -i -m 1 "$line" /inventory.csv | cut -f2 -d";")
hostname=$(grep -i -m 1 "$line" /inventory.csv | cut -f1 -d";")
echo Logging into "$hostname"
nohup /generated_expect "$host" "$username" "$pass" >> /"$line"_"$prepost".txt
echo
echo -e "\033[42m\033[30m Output successfully collected\033[39m\033[0m \n"
echo "--------------------------------------------------------------------------------"
else
echo 0 match found for "$line"
fi
fi
done < /temp.txt
echo
rm /temp.txt
rm /commands.txt
rm /generated_expect
我已经将这个脚本从 Ubuntu 迁移到我的 Mac 上——它运行得非常完美。
这是我得到的错误:
#####
# 1.1.1.1
#####
spawn ssh -oStrictHostKeyChecking=no uuuuuu@1.1.1.1
ssh: Could not resolve hostname 1.1.1.1^M: nodename nor servname provided,or not kNown
SSH failure for 1.1.1.1
我怀疑它与某处的行结束有关,但我找不到它......我已经检查了所有东西,两次。
我知道脚本本身如果不是实现我正在尝试执行的任务的最优雅的方式,但它以前运行良好。任何建议将不胜感激。
--- 更新---
我已经按照建议使用了 ShellCheck;能够稍微清理代码,但错误仍然相同。
我什至尝试过建议的 sed -i "" 's/\r$//' script.sh
,但也没有运气。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)