Linux使用shell编程自动切换IP

来源:互联网 发布:sip服务器端口 编辑:程序博客网 时间:2024/05/17 03:24

Ubuntu系统,由于上下班使用的IP不一样,每次手工修改很麻烦。

今天参考网络资料,做成了批处理。

全文如下:

#!/bin/bash
#上下班自动切换网络地址
#上班IP,Mac,网关
IP[0]=192.168.0.2
MAC[0]=00:01:02:03:04:05
GW[0]=192.168.0.1
#下班IP,Mac,网关
IP[1]=192.168.1.3
MAC[1]=00:02:03:04:05:06
GW[1]=192.168.1.1
#设定下班时间
off=1630
now=$(date +%H%M)
let now=${now/0/$null}
let int=$now-$off
#echo $now-$off=$int
#这里可能需要进一步取整
let id=$now/$off
#echo $id
ifconfig eth0 down
ifconfig eth0 hw ether "${MAC[$id]}"
ifconfig eth0 up
#Ubuntu上无network服务?
#service network stop
ifconfig eth0 "${IP[$id]}" netmask 255.255.255.0
route add -net default gw "${GW[$id]}"
#service network start
sleep 1
echo "Network Connected."

复制后保存为【ip.sh】,运行terminal进入保存目录,执行【./ip.sh】即可。

当然你也可以将其添加到启动程序中,这样每次开机就自动修改了。


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击