【发布时间】:2017-07-23 04:15:31
【问题描述】:
我试图让用户输入他们想要与之交互的 IP 地址的数量,然后输入每个 IP 地址并将其分配给一个变量。该脚本最终将编写并执行第二个脚本。第二个脚本的原因是我正在 ssh-ing 到一个 AP,以便将 x 个 AP 聚集在一起,一旦 SSH 发生,bash/python 变量就不再通过(AP 有它自己的语言),所以在运行 ssh 脚本之前,它们必须被翻译成纯文本。下面的代码可以运行,但只允许 2 个 ip 地址(我不知道如何使用 $cvar 创建多个变量),并且不允许我决定输入多少个 ip 地址:
#!/bin/bash
echo -e "How many AP's do you want to Cluster?:"
#this is the variable to define how many ips to ask for
read cvar
echo -e "Enter last 2 digits of AP #1:"
read ip1
echo -e "Enter last 2 digits of AP #2:"
read ip2
#I want this to continue for x number of ip addresses(defined by $cvar)
echo -e "Enter a name for your Cluster:"
read cname
#code below is executed within the AP terminal(commands are unique to that shell)
echo "#!/bin/bash
ssh -T admin@192.168.0.$ip1 <<'eof'
configure
cluster
add $cname
add-member 192.168.0.$ip1 user ***** password ********
save
add-member 192.168.0.$ip2 user ***** password ********
save
exit
operate $cname
save
exit
" > ./2ndScript.sh
chmod a+x ./2ndScript.sh
/bin/bash ./2ndScript.sh
【问题讨论】:
-
你需要一个array。
-
您有两个答案建议您使用数组,这应该足以让您自己改进脚本,所以我不会添加另一个。相反,我将向您推荐this answer 底部的 bash 函数,以帮助您验证 IP 地址。 :-)