[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] IP,DNS,网关VBS代码,可获取,可修改

由于上下班要带电脑,所以难免要切换网关,IP,DNS等,花了2个小时的成果,写成了函数供调用
本人QQ:312782705 希望大家共同学习,分享
  1. On Error Resume Next
  2. Set moWindow=WScript.CreateObject("InternetExplorer.Application", "IE_")
  3. moWindow.Navigate2 "about:blank"
  4. With moWindow.Document.ParentWindow
  5. .Document.Write "<input type='button' value='一键修改网关192.168.0.1' name='Button'/></br>"
  6. .Document.Write "<input type='button' value='一键修改网关192.168.1.1' name='Button2'/></br>"
  7. .Document.Write "<input type='text' size='30' name='ip'/><input type='text' size='30' name='xip'/></br>"
  8. .Document.Write "<input type='text' size='30' name='mac'/><input type='text' size='30' name='xmac'/></br>"
  9. .Document.Write "<input type='text' size='30' name='dhcp'/><input type='text' size='30' name='xdhcp'/></br>"
  10. .Document.Write "<input type='text' size='30' name='IPGateway'/><input type='text' size='30' name='xIPGateway'/></br>"
  11. .Document.Write "<input type='text' size='30' name='IPSubnet'/><input type='text' size='30' name='xIPSubnet'/></br>"
  12. .Document.Write "<input type='text' size='30' name='DNS1'/><input type='text' size='30' name='xDNS1'/></br>"
  13. .Document.Write "<input type='text' size='30' name='DNS2'/><input type='text' size='30' name='xDNS2'/></br>"
  14. .Document.Write "<input type='button' value='自定义修改IP网关DNS等' name='IPset'/></br><body background='//1.jpg'>
  15. <body bgcolor='#0000'></br></br></br>"
  16. .Document.Write "<input type='button' value='  一键获取本机设置    ' name='getip'/></br>"
  17. .MoveTo .Screen.AvailWidth/2-140,.Screen.AvailHeight/2-225
  18. .ResizeTo 280,650
  19. .Document.Title="沈寂 高级IP修改器 QQ:312782705"
  20. End With
  21. moWindow.Document.Close
  22. With moWindow
  23. .FullScreen=0
  24. .MenuBar=0
  25. .AddressBar=0
  26. .ToolBar=0
  27. .StatusBar=0
  28. .Resizable=0
  29. .Visible=1
  30. Set Form=.Document.All
  31. Set .Document.All.Button.onClick=GetRef("Button_onClick")
  32. Set .Document.All.Button2.onClick=GetRef("Button2_onClick")
  33. Set .Document.All.IPset.onClick=GetRef("IPset_onClick")
  34. Set .Document.All.getip.onClick=GetRef("getip_onClick")
  35. End With
  36. mbFinished=False
  37. Do Until mbFinished
  38. WScript.Sleep 50
  39. Loop
  40. '____________________________________________________________________________退出代码
  41. Sub IE_onQuit
  42. mbFinished=True
  43. End Sub
  44. '____________________________________________________________________________自定义1
  45. Sub Button_onClick
  46. xiugaiip "192.168.0.74","192.168.0.1","255.255.255.0","202.96.199.132","202.96.209.5"
  47. End Sub
  48. '____________________________________________________________________________自定义2
  49. Sub Button2_onClick
  50. xiugaiip "192.168.1.74","192.168.1.1","255.255.255.0","202.96.199.132","202.96.209.5"
  51. End Sub
  52. ''____________________________________________________________________________自定义修改IP
  53. Sub IPset_onClick
  54. if (moWindow.Document.All.IPset.value="填写好了,确定修改") then
  55. msgbox "123"
  56. xiugaiip moWindow.Document.All.xip.value,moWindow.Document.All.xIPGateway.value,moWindow.Document.All.xIPSubnet.value,moWindow.Document.All.xDNS1.value,moWindow.Document.All.xDNS1.value
  57. else
  58. moWindow.Document.ParentWindow.ResizeTo 580,450
  59. getip_onClick
  60. msgbox "左边为本机原始设置,请在右边填写你修改后的设置",,"提示"
  61. moWindow.Document.All.IPset.value="填写好了,确定修改"
  62. end if
  63. End Sub
  64. '____________________________________________________________________________获取本机设置
  65. Sub getip_onClick
  66. Dim objWMIService,colItems,objItem,objAddress
  67. Set bjWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
  68. Set colItems =bjWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  69. For Each objItem in colItems
  70. For Each objAddress in objItem.IPAddress
  71.   If objAddress <> "" then
  72.   IP = objAddress  'IP地址
  73.   mac =objItem.MACAddress 'MAC地址
  74.   DHCPEnabled=objItem.DHCPEnabled  '是否启用DHCP
  75.   DefaultIPGateway=objItem.DefaultIPGateway(0) '网关
  76.   IPSubnet=objItem.IPSubnet(0) '子网掩码
  77.   DNSServerSearchOrder1=objItem.DNSServerSearchOrder(0) 'DNS设置1
  78.   DNSServerSearchOrder2=objItem.DNSServerSearchOrder(1) 'DNS设置2
  79.   Exit For
  80. End If  
  81. Next
  82. Exit For
  83. Next
  84. moWindow.Document.All.ip.value="IP地址是:" & ip
  85. moWindow.Document.All.xip.value=ip
  86. moWindow.Document.All.mac.value="MAC地址是::" & mac
  87. moWindow.Document.All.xmac.value="不能修改"
  88. moWindow.Document.All.dhcp.value="是否启用DHCP:" & DHCPEnabled
  89. moWindow.Document.All.xdhcp.value="不能修改"
  90. moWindow.Document.All.IPGateway.value="网关是:" & DefaultIPGateway
  91. moWindow.Document.All.xIPGateway.value=DefaultIPGateway
  92. moWindow.Document.All.IPSubnet.value="子网掩码是:" & IPSubnet
  93. moWindow.Document.All.xIPSubnet.value=IPSubnet
  94. moWindow.Document.All.DNS1.value="DNS1:" & DNSServerSearchOrder1
  95. moWindow.Document.All.xDNS1.value=DNSServerSearchOrder1
  96. moWindow.Document.All.DNS2.value="DNS2:" & DNSServerSearchOrder2
  97. moWindow.Document.All.xDNS2.value=DNSServerSearchOrder2
  98. End Sub
  99. '____________________________________________________________________________自定义修改函数
  100. Function xiugaiip(ip,IPGateway,IPSubnet,DNS1,DNS2)
  101. STR_GATEWAY = IPGateway
  102. STR_NEWDNS1 = DNS1
  103. STR_NEWDNS2 = DNS2
  104. STR_IP = ip
  105. STR_zwym=IPSubnet
  106. Set fs=CreateObject("Scripting.FileSystemObject")
  107. strWinMgmt="winmgmts:{impersonationLevel=impersonate}"
  108. Set objNICs = GetObject( strWinMgmt ).InstancesOf("Win32_NetworkAdapterConfiguration")
  109. For Each objNIC In objNICs
  110. If objNIC.IPEnabled Then
  111. objNIC.SetDNSServerSearchOrder Array(STR_NEWDNS1,STR_NEWDNS2)
  112. objNIC.SetGateways Array(STR_GATEWAY)
  113. End If
  114. Next
  115. strComputer = "."
  116. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  117. Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")
  118. strIPAddress = Array(STR_IP)
  119. strSubnetMask = Array(IPSubnet)
  120. For Each objNetAdapter in colNetAdapters
  121. errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
  122. Next
  123. msgbox "已经设置为" & vbcrlf & "网关:" & IPGateway & vbcrlf & "ip地址:" & ip & vbcrlf & "子网掩码:" & IPSubnet & vbcrlf & "DNS1:" & DNS1 & vbcrlf & "DNS2:" &DNS2,,"提示"
  124. end Function
复制代码

[ 本帖最后由 ipqfhvk 于 2010-12-12 18:03 编辑 ]
1

评分人数

不好意思,之前从来没有发过帖子不知道格式,已经修正

TOP

感谢分享,学习下

TOP

只有两个地方,哪系统里有设置两个IP的功能喔。实在不行就设置成一个。

TOP

这些对象和属性,作者都是从哪里学的,为什么我找不到呢

TOP

回复 5# 秋风·飞扬


InternetExplorer.Application 对象随便google一下,铺天盖地到处都是。
html dom 部分, 像document 那些玩意,可在 http://www.w3school.com.cn/ 查看,都是中文,如果你E英不错,直接上msdn查看。
wmi各种类的属性和方法可上msdn查询。比如文中的Win32_NetworkAdapterConfiguration之类,google一下,第一条结果一般必定是msdn。

TOP

回复 6# powerbat


    哦,继续学习,明天估计要裸考四级了

TOP

返回列表