您的位置:首页 > 其它

利用CMD 批处理 实现自动更改ip

2010-06-22 09:21 162 查看
最近由于总需要在教固定ip和动态获取ip这两种网络环境下切换,所以干脆写了个批处理的脚本,自动实现切换功能。不废话了,贴代码:

@echo off
:head
echo "Usage:Input 0 to change ip setting to auto mode; 1 changes ip setting to wwc
mode."
set /p option= Please input your choice:
if  %option% EQU 0 (
netsh interface ip set address "本地连接"  dhcp
netsh interface ip set dns "本地连接" dhcp
echo "已设置成ip自动获取!"
pause
exit
) else if  %option% EQU 1 (
@echo off
netsh interface ip set address "本地连接"  static 210.30.101.20 255.255.255.0
210.30.101.254 1
netsh interface ip set dns "本地连接" static 202.96.69.38
echo "已设置为固定ip!"
pause
exit
) else (
echo "输入错误!"
goto head
)


bat的教材很好查到,我只简单解释几句。:head是标签,供最后的goto使用。set /p 一行是让用户输入选项(0或1),并赋值给option变量。如果option为0则将ip切换为自动获取,为1则切换为预定义的设置。其中%中间是对变量option的引用。

另外我还遇到一个小问题未解决:由于netsh interface ip set命令成功后会在终端显示“确定”,我希望把这个显示去掉,没能实现,还请各位高手指点了!(注:这个现实属于命令的输出,与回显不同,所以不受 echo off 的影响)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: