追求新生活 名字2008

活着的意义

不同的网络之间切换命令行文件设置ip信息,以及命令行启动停止iis

要经常在不同的网络之间切换,比如局域网、系统内部网和外网,有手提的朋友需要经常切换更改ip地址等信息,想了想,学习下如何快速更改ip地址信息也好。

把下面的程序保存为bat文件

@Echo off                                                //这句的加入是为了不让屏幕显示详细过程

netsh interface ip delete dns "本地连接" addr=all
netsh interface ip add dns "本地连接" addr=100.0.2.8
netsh interface ip add dns "本地连接" addr=100.0.2.9
netsh interface ip add address "本地连接" 100.8.57.222 255.255.254.0
netsh interface ip add address "本地连接" gateway=100.8.56.1 gwmetric=2
ipconfig /flushdns

 

发现可以,但ip地址是在原来的基础上填加的,达不到修改的要求.于是更改后变为

netsh interface ip delete dns "本地连接" addr=all
netsh interface ip add dns "本地连接" addr=100.0.2.8
netsh interface ip add dns "本地连接" addr=100.0.2.9
netsh interface ip set address "本地连接" Static 100.8.57.222 255.255.255.0 10.8.56.1 1
netsh interface ip set address "本地连接" gateway=100.8.56.1 gwmetric=2
ipconfig /flushdns

 以上把地址设置为:100.8.57.222

 

其中netsh用法,在命令行 cmd下
输入netsh,回车,出现:netsh>
再输入interface,回车,出现netsh interface>
再输入ip,回车,出现netsh interface ip>

连着写就有上面的程序行了

 

解析:

  set address "本地连接" static 192.168.0.107 255.255.255.0 192.168.0.1 1 
  "本地连接"为name连接名,有些朋友的电脑上显示"本地连接3"等均不受影响,192.168.0.107为addr待设定IP地址,255.255.255.0为mask子网掩码,192.168.0.1 1为gateway网关,1为gwmetric默认网关的跃点数

 

有关帮助?

 

netsh interface ip>/?

下列指令有效:

命令从 netsh 上下文继承:
..             - 移到上一层上下文级。
aaaa           - 更改到 `netsh aaaa' 上下文。
abort          - 丢弃在脱机模式下所做的更改。
add            - 在项目列表上添加一个配置项目。
alias          - 添加一个别名
bridge         - 更改到 `netsh bridge' 上下文。
bye            - 退出程序。
commit         - 提交在脱机模式中所做的更改。
delete         - 在项目列表上删除一个配置项目。
dhcp           - 更改到 `netsh dhcp' 上下文。
diag           - 更改到 `netsh diag' 上下文。
exit           - 退出程序。
firewall       - 更改到 `netsh firewall' 上下文。
interface      - 更改到 `netsh interface' 上下文。
ipsec          - 更改到 `netsh ipsec' 上下文。
offline        - 将当前模式设置成脱机。
online         - 将当前模式设置成联机。
popd           - 从堆栈上打开一个上下文。
pushd          - 将当前上下文放入堆栈。
quit           - 退出程序。
ras            - 更改到 `netsh ras' 上下文。
routing        - 更改到 `netsh routing' 上下文。
rpc            - 更改到 `netsh rpc' 上下文。
set            - 更新配置设置。
show           - 显示信息。
unalias        - 删除一个别名。
wins           - 更改到 `netsh wins' 上下文。
winsock        - 更改到 `netsh winsock' 上下文。

命令从 netsh interface 上下文继承:
add            - 向表中添加一个配置项目。
delete         - 从表中删除一个配置项目。
ip             - 更改到 `netsh interface ip' 上下文。
ipv6           - 更改到 `netsh interface ipv6' 上下文。
portproxy      - 更改到 `netsh interface portproxy' 上下文。
reset          - 复位信息。
set            - 设置配置信息。
show           - 显示信息。

此上下文中的命令:
?              - 显示命令列表。
add            - 向表中添加一个配置项目。
delete         - 从表中删除一个配置项目。
dump           - 显示一个配置脚本。
help           - 显示命令列表。
reset          - 复位 TCP/IP 及相关的组件到干净的状态。
set            - 设置配置信息。
show           - 显示信息。

 

实际在winxp例子:把地址设置为192.168.101.89

@Echo off
netsh interface ip delete dns "本地连接" addr=all
netsh interface ip add dns "本地连接" addr=202.96.128.166
netsh interface ip add dns "本地连接" addr=202.96.128.86
netsh interface ip set address "本地连接" Static 192.168.101.89 255.255.255.0 192.168.101.254 1
netsh interface ip set address "本地连接" gateway=168.101.254 gwmetric=2
ipconfig /flushdns

 

经过测试,如果win7系统(测试通过),有较强的安全性 ,则需要以下代码保存bat执行,并且用管理员身份执行,全部允许该程序修改操作,不要阻止. 可在不同网络环境中切换.

@echo off
rasphone -h 宽带连接               //这里假如有宽带连接,或者ppoe服务的账号连接,先断开rasphone -h 连接名称,再设置ip地址信息
netsh interface ip set address name="本地连接" source=static addr=100.60.251.149 mask=255.255.255.0 gateway=100.60.251.1 1
netsh interface ip set dns name="本地连接" source=static addr=202.96.128.86 register=primary
netsh interface ip add dns name="本地连接" addr=202.96.128.166 index=2
ipconfig /flushdns

如果你想切换回pppoe上网,则需要取消网关,可以这样做

@echo off
netsh interface ip set address name="本地连接" source=static addr=10.60.251.4 mask=255.255.255.0
netsh interface ip set dns name="本地连接" source=static addr=202.96.128.86 register=primary
netsh interface ip add dns name="本地连接" addr=202.96.128.166 index=2
ipconfig /flushdns
rasdial 宽带连接 lingshang@163.gd lingshang    //  rasdial格式 rasdial 连接名称 账号 密码

假如你上网的路径暂时不可用,则可以用自己的手机共享热点(流量)给pc,只需要设置pc的网卡为dhcp就可以了。以下代码保存为bat(win7测试通过),用管理员身份执行bat

@echo off
netsh interface ip set address name="无线网络连接" source=dhcp
netsh interface ip set dns name="无线网络连接" static addr = none
config /flushdns

再例如:如果你要求服务器iis按照你的指定时间启动和停止,请参考如下指令

@echo off
net stop iisadmin  #直接停止指定服务
echo "stop is ok ,echo空格加字符串,表示输出字符串"
pause
net start iisadmin  #直接停止指定服务
net start w3svc    #直接停止指定服务  这里要求开启两个服务才算正式开启,待验证。
pause

 其实。在win2012以上版本,有时候用命令行是比较方便的。例如当你是远程控制计算机的时候,自需要运行命令行,输入

例如命令行:注销计算机  logoff

                     关闭计算机  shutdown -s

                     清理dns 缓存  ipconfig/flushdns

posted on 2008-08-02 11:31  pyman  阅读(2355)  评论(0编辑  收藏  举报

导航