我不确定我做错了什么,只是没有理由它不起作用的原因:
这个我确实得到了预期的SSH结果
if [ "Hennopsview" != "2 - LAN" ] && [ "Hennopsview" != "1 - WAN" ] \
&& [ "Hennopsview" != "Cross Connect" ]; then
if [[ "Hennopsview" =~ /pptp/g ]]; then
echo "No Match 2"
else
ssh admin@192.168.0.1 ':put [/interface eoip get [/interface eoip find name="Hennopsview"] remote-address]'
fi
else
echo "No Match"
fi
但是下面的一个没有给我第2场比赛回音
if [ "<pptp-hennopsview>" != "2 - LAN" ] && [ "<pptp-hennopsview>" != "1 - WAN" ] \
&& [ "<pptp-hennopsview>" != "Cross Connect" ]; then
if [[ "<pptp-hennopsview>" =~ /pptp/g ]]; then
echo "No Match 2"
else
ssh admin@192.168.0.1 ':put [/interface eoip get [/interface eoip find name="<pptp-hennopsview>"] remote-address]'
fi
else
echo "No Match"
fi
这个正则表达式有什么问题?
然而,以下修复了第二个请求
if [ "<pptp-hennopsview>" != "2 - LAN" ] && [ "<pptp-hennopsview>" != "1 - WAN" ] \
&& [ "<pptp-hennopsview>" != "Cross Connect" ]; then
if [[ "<pptp-hennopsview>" =~ [pptp] ]]; then
echo "No Match 2"
else
ssh admin@154.127.117.218 ':put [/interface eoip get [/interface eoip find name="<pptp-hennopsview>"] remote-address]'
fi
else
echo "No Match"
fi
然后打破原来的
if [ "Hennopsview" != "2 - LAN" ] && [ "Hennopsview" != "1 - WAN" ] \
&& [ "Hennopsview" != "SDA - Cross Connect" ]; then
if [[ "Hennopsview" =~ [pptp] ]]; then
echo "No Match 2"
else
ssh admin@154.127.117.218 ':put [/interface eoip get [/interface eoip find name="Hennopsview"] remote-address]'
fi
else
echo "No Match"
fi
我明白使用“[pptp]”是一个字符匹配而不是单词。为什么用“/”替换“[”并不能解决问题?
答案 0 :(得分:1)
/pptp/g
将匹配字符串".../pptp/g..."
。操作符=~
在其右侧使用正则表达式,没有任何分隔符:没有引号,没有斜杠。如果你想找到pptp
,那是唯一应该存在的东西:
[[ "<pptp-hennopsview>" =~ pptp ]]
是你可能想做的事。
编辑:我不知道为什么我写了#34;没有引号&#34;。
答案 1 :(得分:0)
以下解决了我的问题:
if [ "<pptp-Hamanskraal>" != "2 - LAN" ] && [ "<pptp-Hamanskraal>" != "1 - WAN" ] && [ "<pptp-hennopsview>" != "Cross Connect" ]; then if [[ "<pptp-hennopsview>" =~ "pptp-" ]]; then ssh admin@192.168.0.1 ':put [/interface pptp-server get [/interface pptp-server find name="<pptp-hennopsview>"] client-address ]'; else ssh admin@192.168.0.1 ':put [/interface eoip get [/interface eoip find name="<pptp-hennopsview>"] remote-address]'; fi else echo "No Match"; fi
以下解决了我的所有问题
[[ "<pptp-hennopsview>" =~ "pptp-" ]]
谢谢。