mysql - 写了一会儿针对几个ip运行的shell脚本

标签 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 | 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 的建议删除 while 循环并使用 for (这正是我要做的)

请注意,如果您使用数组变量而不指定索引,它似乎会为您提供第一个元素:

$ ip=(one two three)
$ echo ${ip[1]}
two
$ echo $ip
one

$ echo ${#ip[*]}
3
$ let a=0
$ while [[ $a -lt ${#ip[*]} ]]; do
> echo $ip
> let a+=1
> done
one
one
one

关于mysql - 写了一会儿针对几个ip运行的shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26179161/

相关文章:

php - IP地址欺骗/更改以在本地机器上进行测试

bash - Bash 中的 "[0: command not found"

linux - 如何在 linux 中通过唯一进程名称获取 pid?

linux - shell,删除名称末尾的空格

python - Django - 如何在发布表单时添加用户的 IP 地址

php - 如何通过合并两个表返回多行

mysql - 获取当前用户 ID 并存储它

mysql - 使用存储过程更新mysql中的表

php - 试图在laravel中获取非对象错误的属性

Java 服务器和客户端在本地工作,但不通过 IP