利用vbs自动修改ip的代码

编程入门 行业动态 更新时间:2024-10-28 13:17:01
单位机房的系统需要重新安装,一共近300台设备,使用ghost网络克隆后,客户机重新设置ip是个麻烦的事情。我们使用的教学管理软件要求客户机必须有固定ip,单位5个机房如下(dns:61.134.1.4,掩码为:255.255.255.0):

机房

起始ip

ip终止ip

网关

机器名

工作组

1号192.168.1.1100254no_100~no_160s01
2号192.168.1.101200254no_200~no_260s02
3号192.168.3.180254no_300~no_360s03
4号192.168.3.81160254no_400~no_460s04
5号192.168.3.161240254no_500~no_560s05
以下为vbs源码:1.xp系统(测试通过,用户为administrator,文件为e:\fxp.vbs,启动组建立快捷方式fxp.lnk以便开机后自动运行一次)复制代码 代码如下:'/////主程序 dimmsginf,machname'定义变量:对话框,机器名 msginf=msgbox("该程序只能执行1次,请在xp系统硬件安装完毕后执行!"&chr(13)&"是否继续?",65,"修改机器网络配置")'信息提示 ifmsginf=1then'如果按确定,则 machname=inputon()'用函数inputon()分析 ifmachname<>"quit"then'如果返回值不等于"quit",则 wmitoip(machname)'运行函数wmitoip()设置机器信息 mreboot()'重启机器 endif endif '///重启机器 submreboot() dimfso,f1,f2 setfso=createobject("scripting.filesystemobject") '删除启动组 iffso.fileexists("c:\documentsandsettings\administrator\「开始」菜单\程序\启动\fxp.lnk")then setf1=fso.getfile("c:\documentsandsettings\administrator\「开始」菜单\程序\启动\fxp.lnk") f1.delete endif '删除vbs文件 iffso.fileexists("e:\fxp.vbs")then setf2=fso.getfile("e:\fxp.vbs") f2.delete endif setwshshell=wscript.createobject("wscript.shell") 'wshshell.run("shutdown.exe-r-t5")'重启 endsub '///生成计算机名 functioninputon()'函数inputon() dimt'变量 whiletrue'循环直到退出函数 t=inputbox("按一下规则输入:"&chr(13)&chr(13)&"第1位代表机房号"&chr(13)&"第2、3位代表机器号"&chr(13)&"教师机用00代表"&chr(13)&"如:123代表1号机房23号机"&chr(13)&"请确保输入正确!!","请输入3位机器标识!","")'输入机算机名,默认值为空 ift=""then'如果t等于空(按了取消键),则 inputon="quit"'返回值为"quit" exitfunction'退出程序 endif iflen(t)=3then'计算机号的长度为3位 ifcint(t)>=100andcint(t)<580then'验证 inputon=t'返回需要的计算机名 exitfunction endif endif wend endfunction '///修改机器ip、掩码、网关、工作组、机器名 subwmitoip(t) strcomputer="." strmask="255.255.255.0" dimlt,rt'变量 dimipv,gateway,lan'ip,网关,工作组 lt=cint(left(t,1))'机号左1位数字值 rt=cint(right(t,2))'机号右两位数字值 iflt=1orlt=2then'判断网关 gateway="192.168.1.254" else gateway="192.168.3.254" endif iflt=1then'1号机房 lan="s01" ipv="192.168.1." ifrt=0then'教师机 ipv=ipv+"100" else'学生机 ipv=ipv+cstr(rt) endif endif iflt=2then'2号机房 lan="s02" ipv="192.168.1." ifrt=0then'教师机 ipv=ipv+"200" else'学生机 rt=rt+100 ipv=ipv+cstr(rt) endif endif iflt=3then'3号机房 lan="s03" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"80" else'学生机 ipv=ipv+cstr(rt) endif endif iflt=4then'4号机房 lan="s04" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"160" else'学生机 rt=rt+80 ipv=ipv+cstr(rt) endif endif iflt=5then'5号机房 lan="s05" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"240" else'学生机 rt=rt+160 ipv=ipv+cstr(rt) endif endif setobjwmiservice=getobject("winmgmts:\"&strcomputer&"\root\cimv2") setcolnetadapters=objwmiservice.execquery("select*fromwin32_networkadapterconfigurationwhereipenabled=true") stripaddress=array(ipv) strsubnetmask=array(strmask) strgateway=array(gateway)'修改网关 'strgatewaymetric=array(1)'跃点数 strdns=array("61.134.1.4") foreachobjnetadapterincolnetadapters errenable=objnetadapter.enablestatic(stripaddress,strsubnetmask)'ip,掩码 errgateways=objnetadapter.setgateways(strgateway)'网关 errdns=objnetadapter.setdnsserversearchorder(strdns)'dns next setobjwmiservice=getobject("winmgmts:"_ &"{impersonationlevel=impersonate}!\"&strcomputer&"\root\cimv2") setcolcomputers=objwmiservice.execquery_ ("select*fromwin32_computersystem") foreachobjcomputerincolcomputers err=objcomputer.rename("no_"&t)'机器名 returnvalue=objcomputer.joindomainorworkgroup("s0"&left(t,1))'工作组 next endsub2.98系统 98系统可以生成ip.reg注册表文件,导入后就可以了,源码如下(主体思路,这次没有98系统,所以未完成,可参考xp系统的改进): 复制代码 代码如下:'/////主程序 dimmsginf,machname'定义变量:对话框,机器名 msginf=msgbox("生成注册表文件,是否继续?",65,"getreg")'信息提示 ifmsginf=1then'如果按确定,则 machname=inputon()'用函数inputon()分析 ifmachname<>"quit"then'如果返回值不等于"quit",则 setreg(machname)'运行函数setreg()生成注册表ip.reg endif endif '///生成计算机名 functioninputon()'函数inputon() dimt'变量 whiletrue'循环直到退出函数 t=inputbox("按一下规则输入:"&chr(13)&chr(13)&"第1位代表机房号"&chr(13)&"第2、3位代表机器号"&chr(13)&"教师机用00代表"&chr(13)&"如:123代表1号机房23号机"&chr(13)&"请确保输入正确!!","请输入3位机器标识!","")'输入机算机名,默认值为空 ift=""then'如果t等于空(按了取消键),则 inputon="quit"'返回值为"quit" exitfunction'退出程序 endif iflen(t)=3then'计算机号的长度为3位 ifcint(t)>=100andcint(t)<580then'验证 inputon=t'返回需要的计算机名 exitfunction endif endif wend endfunction '///生成注册文件 subsetreg(t)'生成注册表,t为机器号 dimfso,f1,f2,lt,rt'变量 dimipv,gateway,lan'ip,网关,工作组 lt=cint(left(t,1))'机号左1位数字值 rt=cint(right(t,2))'机号右两位数字值 iflt=1orlt=2then'判断网关 gateway="192.168.1.254" else gateway="192.168.3.254" endif iflt=1then'1号机房 lan="s01" ipv="192.168.1." ifrt=0then'教师机 ipv=ipv+"100" else'学生机 ipv=ipv+cstr(rt) endif endif iflt=2then'2号机房 lan="s02" ipv="192.168.1." ifrt=0then'教师机 ipv=ipv+"200" else'学生机 rt=rt+100 ipv=ipv+cstr(rt) endif endif iflt=3then'3号机房 lan="s03" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"80" else'学生机 ipv=ipv+cstr(rt) endif endif iflt=4then'4号机房 lan="s04" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"160" else'学生机 rt=rt+80 ipv=ipv+cstr(rt) endif endif iflt=5then'5号机房 lan="s05" ipv="192.168.3." ifrt=0then'教师机 ipv=ipv+"240" else'学生机 rt=rt+160 ipv=ipv+cstr(rt) endif endif setfso=createobject("scripting.filesystemobject") iffso.fileexists("e:\ip.reg")then setf2=fso.getfile("e:\ip.reg") f2.delete endif'如果存在ip.reg,先删了 setf1=fso.createtextfile("e:\ip.reg",true)'建立文件ip.cfg 'f1.writeline("regedit4")'以下为生成注册表 f1.writeline("windowsregistryeditorversion5.00") f1.writeblanklines(1) f1.writeline("[hkey_local_machine\system\currentcontrolset\control\computername\computername]") f1.writeline(chr(34)&"computername"&chr(34)&"="&chr(34)&t&chr(34))'计算机名 f1.writeline("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]") f1.writeline(chr(34)&"ipaddress"&chr(34)&"="&chr(34)&ipv&chr(34))'ip f1.writeline("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]") f1.writeline(chr(34)&"defaultgateway"&chr(34)&"="&chr(34)&gateway&chr(34))'网关 f1.writeline("[hkey_local_machine\system\currentcontrolset\services\class\nettrans\0000]") f1.writeline(chr(34)&"ipmask"&chr(34)&"="&chr(34)&"255.255.255.0"&chr(34))'子网掩码 f1.writeline("[hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]") f1.writeline(chr(34)&"comment"&chr(34)&"="&chr(34)&t&chr(34))'计算机说明 f1.writeline("[hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]") f1.writeline(chr(34)&"computername"&chr(34)&"="&chr(34)&t&chr(34))'计算机名 f1.writeline("[hkey_local_machine\system\currentcontrolset\services\vxd\vnetsup]") f1.writeline(chr(34)&"workgroup"&chr(34)&"="&chr(34)&lan&chr(34))'工作组 endsub
  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

利用vbs自动修改ip的代码

本文发布于:2023-06-10 23:03:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   vbs   ip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!