一个ip执行一次程序

来源:互联网 发布:c语言课程讲解 编辑:程序博客网 时间:2024/05/24 01:38

假设你有一堆可以是用的ip(对外ip),可改变本机ip进行网络连接执行一次程序。 

比较幸运的是,我位于一个局域网内,而局域网的每个ip都是独立的ip,这样就可以侦测局域网可用ip来执行程序了。

由于其他主机可能屏蔽ping,这里使用arp检测某一ip是否未被其他主机使用。

下面给出bash脚本

#!/bin/bashkeys="keywords.txt"jar="google.jar" #我要运行一个java程序net_pfx="xx.xx.xx" #局域网ip前缀network=1subip=1if [ ! -f $key ]; thenecho "$keys not exsist"exit 1fiif [ ! -f $jar ]; thenecho "$jar not exsist"exit 1fiif [ `id -u` -ne 0 ]; thenecho "must run by root"exit 2fiwhile read keydowhile  arping -c1 -w2 -Ieth1 "$net_pfx$network.$subip" || grep "address $net_pfx$network.$subip" /etc/network/interfacesdolet subip++if [ $subip -ge 254 ] ; then if [ $ network -gt 3 ]; then echo "all ips traversed"return 0fisubip=1let network++fidone  sed -i s/address.*/"address $net_pfx$network.$subip"/ /etc/network/interfacessed -i s/gateway.*/"gateway $net_pfx$network.254"/ /etc/network/interfaces/etc/init.d/networking restartjava -jar $jar "$key" done < $keys

原创粉丝点击