windows下以及Linux下经常切换ip的批处理脚本

经常进行自动ip和固定ip更改的用户,可以以下批处理,在windows以及Linux下轻松实现自动ip和固定ip的更改。

一、设置自动ip

echo 设置开始自动IP,请稍等....
echo.
echo 正在自动获取IP地址....
netsh interface ip set address name = "本地连接" source = dhcp
echo 正在自动获取DNS ,请稍等......
netsh interface ip set dns "本地连接" source = dhcp
echo 设置完成!


将以上代码复制到记事本编辑保存为”设置自动autoip.bat,放置桌面,双击执行即可自动完成设置;

二、设置固定ip

@echo off
echo 开始设置本机网络地址!

echo 正在设置本机IP ,请稍等......
netsh interface ip set address "本地连接" source = static addr = 192.168.1.3 mask=255.255.255.0
echo 正在设置本机网关,请稍等......
netsh interface ip set address "本地连接" gateway = 192.168.1.1 gwmetric =1
echo 正在设置本机DNS ,请稍等......
netsh interface ip set dns "本地连接" source = static addr = 202.96.69.38
echo 正在设置备用DNS ,请稍等......
netsh interface ip add dns "本地连接" addr=202.96.64.68
echo 设置完成!
ipconfig /all >c:\ip.txt


将以上代码复制到记事本编辑,保存为设置固定staticip.bat,置于桌面,用法同上

下面是Linux下完整设置IP的脚本

1、创建文件chip.scr
#touch chip.scr

2、修改权限为700
#chmod 700 chip.scr

3、编辑文件内容如下

#!/bin/bash
echo 'DEVICE=eth0' > /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'ONBOOT=yes' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'TYPE=Ethernet' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'BOOTPROTO=static' >> /etc/sysconfig/network-scripts/ifcfg-eth0
if [ $1 = "home" ] ; then
echo 'IPADDR=192.168.1.2' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'NETMASK=255.255.255.0' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'GATEWAY=192.168.1.1' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'nameserver 202.96.69.38' > /etc/resolv.conf
else
echo 'IPADDR=10.0.0.2' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'NETMASK=255.0.0.0' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'GATEWAY=10.0.0.1' >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'nameserver 10.0.0.100' > /etc/resolv.conf
fi;
service network restart


4、执行脚本
#./chip.scr [home | office]

相关推荐