Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1348059
  • 博文数量: 81
  • 博客积分: 10060
  • 博客等级: 上将
  • 技术积分: 2366
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-17 14:44
文章分类
文章存档

2009年(6)

2008年(75)

我的朋友

分类: WINDOWS

2008-07-31 13:07:26

先介绍一下Netsh命令的使用方法:

在这里跟大家介绍几个简单的指令
1.Show IP
1.1Cmd Mode
直接在cmd下面输入
netsh interface ip show address
亦可简写为
netsh int ip sh ad
看看,指令是不是和Cisco的nos指令很像!非常怀疑是抄袭Cisco的。 1.2Netsh Mode
您也可以进入netsh的命令模式下
netsh //进入到 netsh mode
netsh>int //进入到 interface 子选项。
interface>ip //進入到 ip 子选项。
interface ip>show
show address - 显示 IP 位址。
show config - 显示 IP 位址及其他资料。
show dns - 显示 DNS 服务器位址。
show icmp - 显示 ICMP 统计
show interface - 显示 IP 介面统计
show ipaddress - 显示 IP 位址
show ipnet - 显示 IP net-to-media 对应
show ipstats - 显示 IP 统计
show joins - 显示加入的多点传送群组
show offload - 显示 offload 内容
show tcpconn - 显示 TCP 连线
show tcpstats - 显示 TCP 统计
show udpconn - 显示 UDP 连线
show udpstats - 显示 UDP 统计
show wins - 显示 WINS 服务器位址。
2.Set IP
下列是所有可用的指令。
这个內容中的指令:
set address - 在指定的介面设定 IP 位址或预设网关。
set dns - 设定 DNS 服务器模式及位址。
set wins - 设定 WINS 服务器模式及位址。2.1.设定IP位址
2.1.1.DHCP设定
若您希望由DHCP取得IP位址可输入
interface ip>set ad "区域连接" DHCP
或简写成
interface ip>set ad "区域连接" D2.1.2.静态IP设定
2.1.2.1.设定IP位址与子网关
netsh -进入到 netsh mode
netsh>int -进入到 interface 子选项。
interface>ip -进入到 ip 子选项。
interface ip>set address name = "区域连接" source = static addr = 10.2.2.100 mask = 255.255.255.0
可简写成
interface ip>set ad "区域连接" s 10.2.2.100 255.255.255.02.1.2.2.设定IP路由
interface ip>set address name = "区域连接" gateway = 10.2.2.254 gwmetric = 1
可简写成
interface ip>set ad "区域连接" ga=10.2.2.254 gw = 12.1.2.3同时设定IP位址和路由
interface ip>set address name = "区域连接" source = static addr = 10.2.2.100 mask = 255.255.255.0 gateway = 10.2.2.254 gwmetric = 1
可简写成
interface ip>set ad "区域连接" s 10.2.2.100 255.255.255.0 10.2.2.254 1设定完後,记得用sh ad去看一下设定的對不對。2.3设定DNS来源
若是由DHCP取得,请输入
interface ip>set dns "区域连接" source=dhcp
若是使用静态设定,请输入
interface ip>set dns name = "区域连接" source = static addr = 10.2.5.2
新增第二組DNS,请输入
interface ip>add dns name = "区域连接" addr = 10.2.5.32.4设定WINS来源
若是由DHCP取得,请输入
interface ip>set wins "区域连接" source=dhcp
若是使用静态设定,请输入
interface ip>set wins name = "区域连接" source = static addr = 10.2.5.10
新增第二組WINS,请输入
interface ip>add wins name = "区域连接" addr = 10.2.5.17
3.将网路状态设定导出/导入
3.1导出
netsh -c interface dump >c:/netset.txt
当然,interface可以简写成int,dump更可简化成d,所以就变成了
netsh -c int d >c:/netset.txt
3.2导入
netsh -f c:/netset.txt 既可在Delphi中调用过程:
//设置成自动获取IP:
NetShTxt := 'netsh interface ip set address ' + '"' + NetWork + '"' + ' source=dhcp';
winexec(pchar(NetShTxt), sw_hide);
//以下代码设置IP地址和网关:
NetShTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' source=static addr=' + IPAddress.IPString + ' mask=' + submask.IPString;
winexec(pchar(NetShTxt), sw_hide);
//网关
NetshTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' gateway=' + DefaultGateway.IPString + ' gwmetric=0';
winexec(pchar(NetShTxt), sw_hide);
其中使用了IPEdit,这个组件,经过修改,已经基本能用于IP地址的输入和获取.本来从网上下载的以为能用,可实际用的过程中会发现不少问题.
设置DNS:
NetshTxt := 'netsh interface ip set dns name=' + '"' + NetWork + '"' + ' source=static addr=' + FirstDns.IPString + ' register=PRIMARY';
winexec(pchar(NetShTxt), SW_Hide);
//netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=2
NetshTxt := 'netsh interface ip add dns name=' + '"' + NetWork + '"' + ' addr=' + SecondDNS.IPString + ' index=2';
注意以上第二个DNS,添加用的命令:
//netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=2
第三个DNS:
//netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=3后期调用winExec执行,感觉很不错,注意生成后使用GetAdaptersInfo函数进行检查一下IP地址是否设置正确.
在网络上流传的好多类似的工具都是这个原理.
设为dhcp   
   winexec(
'netsh    interface    ip    set    address    本地连接1    dhcp',
SW_HIDE);   
    
   设为手工ip   
   WinExec(
'netsh    interface    ip    set    address    本地连接1    static    192.168.1.189    255.255.255.0    192.168.1.1    1',SW_HIDE);
阅读(4751) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-07-31 13:10:03

很久没搞技术文章了,先转一篇垫上。 刚好需要用到修改网关和IP的方法。 用netsh时取巧的方法,正道还是自己写程序,不过程序修改注册表IP后不能马上生效,还需要iphelp api里面一个函数才行。 懒得写了,所以就用命令好了。