Mysql 为几个运行shell脚本的ip编写一段时间
我的脚本部分工作,或者直到我想能够运行while循环,只要IP变量中有IP,我已经在google上的shell中搜索了while循环,但我不太清楚如何编写它,所以我可以添加几个IP并运行它 我来自丹麦,所以变量看起来很奇怪Mysql 为几个运行shell脚本的ip编写一段时间,mysql,shell,ip,sh,snmp,Mysql,Shell,Ip,Sh,Snmp,我的脚本部分工作,或者直到我想能够运行while循环,只要IP变量中有IP,我已经在google上的shell中搜索了while循环,但我不太清楚如何编写它,所以我可以添加几个IP并运行它 我来自丹麦,所以变量看起来很奇怪 #!/bin/bash # snmp printer status #Function takes two arguments: ip oid #function snmpget() #{ # snmpdata=`snmpget -v2c -c public $1 $2
#!/bin/bash
# snmp printer status
#Function takes two arguments: ip oid
#function snmpget()
#{
# snmpdata=`snmpget -v2c -c public $1 $2 | cut -d" " -f3-`
# echo $snmpdata
#}
kunde='fkt'
ip[0]= '172.29.8.250'
ip[1]= '172.29.8.200'
let a=0
while [[ $a -lt ${#ip[*]} ]] ; do
let a+=1
serienummer=$(snmpget -v2c -c public $ip iso.3.6.1.2.1.43.5.1.1.17.1 | cut -d" " -f4-)
model=$(snmpget -v2c -c public $ip iso.3.6.1.2.1.43.5.1.1.16.1 | cut -d" " -f4-)
ialt=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.43.10.1.1.12.1.1 | cut -d" " -f4-)
a3f=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.42.2.1.1.1.8.1.1 | cut -d" " -f4-)
a3sh=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.42.2.1.1.1.7.1.1 | cut -d" " -f4-)
a4sh=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.42.2.1.1.1.7.1.3 | cut -d" " -f4-)
a4f=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.42.2.1.1.1.8.1.3 | cut -d" " -f4-)
hostnavn=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.40.10.1.1.5.1 | cut -d" " -f4-)
scan=$(snmpget -v2c -c public $ip iso.3.6.1.4.1.1347.46.10.1.1.5.3 | cut -d" " -f4-)
mysql --host=localhost --user=username --password=password status_printer -e "INSERT INTO status (serienummer,model,ialt,a3f,a3sh,a4sh,a4f,hostnavn,scan)
VALUES('${serienummer}','${model}','${ialt}','${a3f}','${a3sh}','${a4sh}','${a4f}','${hostnavn}','${scan}');"
done
你有
ip[0]= '172.29.8.250'
ip[1]= '172.29.8.200'
let a=0
while [[ $a -lt ${#ip[*]} ]] ; do
let a+=1
serienummer=$(snmpget -v2c -c public $ip iso.3.6.1.2.1.43.5.1.1.17.1 | cut -d" " -f4-)
# ....................................^^
您正在循环索引变量,但没有使用它。您可以将$ip
更改为${ip[a]}
,或者使用fvu的建议在循环时删除,并使用for
(这正是我要做的)
请注意,如果使用数组变量而不指定索引,它似乎会为您提供第一个元素:
$ ip=(one two three)
$ echo ${ip[1]}
two
$ echo $ip
one
及
Glenn很好地解释了脚本失败的原因,我是这样写的,因为for循环更适合这种情况。我还展示了如何避免手动管理索引
# "classic" initialization
ips[0]= '172.29.8.250'
ips[1]= '172.29.8.200'
# or alternatively - no need to manage the index
ips=('172.29.8.250' '172.29.8.200')
# or even
ips=()
ips+=('172.29.8.250')
ips+=('172.29.8.200')
for ip in ${ips[@]} ; do
serienummer=$(snmpget -v2c -c public $ip iso.3.6.1.2.1.43.5.1.1.17.1 | cut -d" " -f4-)
# and so on
done
我认为,${ips[@]}中的ip的;do
能准确地完成您需要的任务,而且不会太复杂?你同意吗?注意,我调用数组ips
和单个地址ip
,但这只是一个示例!!它起作用了:D现在我有一个问题,当打印机没有应答时,脚本当然会给我一个超时,但是我怎么能忽略“丢失的”打印机应答,并继续到下一个IP?基姆
# "classic" initialization
ips[0]= '172.29.8.250'
ips[1]= '172.29.8.200'
# or alternatively - no need to manage the index
ips=('172.29.8.250' '172.29.8.200')
# or even
ips=()
ips+=('172.29.8.250')
ips+=('172.29.8.200')
for ip in ${ips[@]} ; do
serienummer=$(snmpget -v2c -c public $ip iso.3.6.1.2.1.43.5.1.1.17.1 | cut -d" " -f4-)
# and so on
done