请高手帮忙,我写的实在是太复杂了,其中有些地方不知道该怎么整的简洁一些,请指教,谢谢!!
已知可以改进的地方:在查询本机信息的时候,可以整合成一个子功能进行调用,但是因为不知道如何 call bat内嵌的代码,所以只好copy了一次。
1、程序目的:我公司给中移动做运营维护,整天有装不完的系统,但是大致分为几类,需要按照一定的规律对计算机进行设置IP,命名等动作。
2、计算机参数设置规律:
计算机IP:
服务厅内网的192开头,根据用途不同,最后一位不同,其他方面IP设置相同。
服务厅业务用机使用10开头的IP,网关不同
工作组规则:根据服务厅拼音全称来设置,例如JINGTIAN
计算机命名规律: 服务厅名字缩写+IP末一位+业务用途 例如 JT-149YW
计算机描述规律:景田服务厅 业务2
3、批处理架构
主程序:修改本机参数.bat
达到查询、修改以下内容的功能
IP、掩码、网关、DNS、计算机名、工作组、计算机描述、
调用子程序:loadini.bat
达到读取.ini配置文件预设参数的功能
附加程序:生成模板.bat
利用本机配置,生成通用配置文件。方便在其他机器直接调用,不需要手动修改模板文件。
以下是程序内容
★★★★★★★★★★★★★★★★★★★★★★★★★
修改本机参数.bat
echo off
cls
title IP、掩码、网关、计算机名、工作组、计算机描述 简易修改工具 CREATED BY PJH. 080130
if not exist 配置文件 goto err
echo off
cls
echo.
echo 信息收集中,请稍候...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%b
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config workstation ^|find "工作站域"') do set GZZ=%%a
for /f "delims=" %%a in ('net config server ^|find "服务器注释"') do set miaoshu=%%a
cls
echo.
echo 目前的机器情况如下:
echo.
echo 机名: %computername%
echo 工作组:%gzz:~24,60%
echo 描述:%miaoshu:~25,60%
echo IP:%nip%
echo 掩码:%nmask%
echo 网关:%ngw%
echo DNS:%dns%
echo.
:slect
echo 请选择本机用途。
echo.
echo 1、蝌台机
echo 2、后台机
echo 3、网吧机
echo 4、业务展示机
echo 5、蝌台触摸屏
echo.
set /p mycho= 请输入您的选择:
cls
if %mycho%#==1# GOTO :cho%mycho%
if %mycho%#==2# GOTO :cho%mycho%
if %mycho%#==3# GOTO :cho%mycho%
if %mycho%#==4# GOTO :cho%mycho%
if %mycho%#==5# GOTO :cho%mycho%
if %mycho%#==# GOTO slect
goto slect
:cho1
set myuse1=Q
set myuse2=蝌台
call loadini 10cfg.ini
goto setting
:cho2
set myuse1=H
set myuse2=后台
call loadini 10cfg.ini
goto setting
:cho3
set myuse1=WB
set myuse2=网吧
call loadini 192cfg.ini
goto setting
:cho4
set myuse1=YW
echo 请选择本机用途:
echo.
echo 1、飞信
echo 2、手机仿真
echo 3、手机报
echo 4、无限音乐俱乐部
echo.
set /p ywyt= 请输入您的选择:
cls
if %ywyt%#==1# set myuse2=飞信&&goto cho4goon
if %ywyt%#==2# set myuse2=手机仿真&&goto cho4goon
if %ywyt%#==3# set myuse2=手机报&&goto cho4goon
if %ywyt%#==4# set myuse2=无限音乐俱乐部&&goto cho4goon
if %ywyt%#==# GOTO cho4
goto cho4
:cho4goon
call loadini 192cfg.ini
goto setting
:cho5
set myuse1=YW
set myuse2=触摸屏
call loadini 192cfg.ini
goto setting
:setting
echo.
echo 本机IP默认前半截为 %fmyip% 请输入本机IP后半截数字
echo.
set /p ips=
if /I %ips% LEQ 0 (cls&echo 输入错误!&goto setting)
if /I %ips% GEQ 255 (cls&echo 输入错误!&goto setting)
for /f "tokens=2 delims==" %%a in ('findstr "服务厅名" 配置文件\服务厅cfg.ini') do set tname=%%a
for /f "tokens=2 delims==" %%a in ('findstr "服务厅简写" 配置文件\服务厅cfg.ini') do set tjx=%%a
for /f "tokens=2 delims==" %%a in ('findstr "默认工作组" 配置文件\服务厅cfg.ini') do set wkgp=%%a
cls
echo.
echo 请问本机是 %myuse2% 几号? 如果无需编号,则回车即可.
set /p mynum=
cls
echo.
echo 正在进行设置,请稍等……
wmic computersystem where Name="%COMPUTERNAME%" call JoinDomainOrWorkgroup Name="%wkgp%" >nul 2>nul
wmic computersystem where name="%COMPUTERNAME%" call rename name="%tjx%-%ips%%myuse1%" >nul 2>nul
net config server /srvcomment:"%tname% %myuse2%%mynum%" >nul 2>nul
netsh interface ip set address "%wkname%" static %fmyip%%ips% %fmymask% %fmygw% 1 >nul 2>nul
netsh interface ip delete dns "%wkname%" all >nul 2>nul
netsh interface ip add dns "%wkname%" %dns% 1 >nul 2>nul
echo off
cls
echo.
echo 信息收集中,请稍候...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%b
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config workstation ^|find "工作站域"') do set GZZ=%%a
for /f "delims=" %%a in ('net config server ^|find "服务器注释"') do set miaoshu=%%a
cls
echo.
echo 目前的机器情况如下:
echo.
echo 机名: %computername% (重启后才会变化成正确的)
echo 工作组:%gzz:~24,60%
echo 描述:%miaoshu:~25,60%
echo IP:%nip%
echo 掩码:%nmask%
echo 网关:%ngw%
echo DNS:%dns%
echo.
echo 修改完毕!按任意键退出程序。
PAUSE >nul 2>nul
exit
:err
echo.
echo.
echo 没有发现配置文件,请先手动设置本机参数再运行 生成本机模板.bat 生成配置信息!
echo.
PAUSE >nul 2>nul
loadini.bat
★★★★★★★★★★★★★★★★★★★★★★★★★
for /f "tokens=2 delims==" %%a in ('findstr "IP前缀" 配置文件\%1') do set fmyip=%%a
for /f "tokens=2 delims==" %%a in ('findstr "默认掩码" 配置文件\%1') do set fmymask=%%a
for /f "tokens=2 delims==" %%a in ('findstr "默认网关" 配置文件\%1') do set fmygw=%%a
for /f "tokens=2 delims==" %%a in ('findstr "DNS" 配置文件\%1') do set dns=%%a
★★★★★★★★★★★★★★★★★★★★★★★★★
生成模板.bat
echo off
MD 配置文件
cls
echo.
echo 正在生成配置文件...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%a
for /f "delims=" %%a in ('net config workstation ^|find "工作站域"') do set GZZ=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config server ^|find "服务器注释"') do set miaoshu=%%a
for /f "delims=-" %%a in ("%computername%") do echo 服务厅简写=%%a>配置文件\服务厅CFG.INI
echo 默认工作组=%gzz:~25,60%>>配置文件\服务厅CFG.INI
for /f "delims= " %%a in ("%miaoshu:~25,60%") do echo 服务厅名=%%a>>配置文件\服务厅CFG.INI
echo 默认网关=%ngw%>配置文件\ipCFG.INI
echo 默认掩码=%nmask%>>配置文件\ipCFG.INI
echo DNS=%dns%>>配置文件\ipCFG.INI
for /f "tokens=1-3 delims=." %%a in ("%nip%") do set fstip=%%a&echo IP前缀=%%a.%%b.%%c.>>配置文件\ipCFG.INI
if /i %fstip%==10 (del 配置文件\10cfg.ini /q /f&rename 配置文件\ipCFG.INI 10cfg.ini) else (del 配置文件\192cfg.ini /q /f&rename 配置文件\ipCFG.INI 192cfg.ini)
cls
echo.
echo 操作完毕,请查看文件夹 "配置文件" 下面的文件
echo.
PAUSE
★★★★★★★★★★★★★★★★★★★★★★★★★
服务厅CFG.INI
服务厅简写=CS
默认工作组=CESHIFUWUTING
服务厅名=测试服务厅
★★★★★★★★★★★★★★★★★★★★★★★★★
192cfg.ini
默认网关= 192.200.200.1
默认掩码= 255.255.255.0
DNS=
IP前缀= 192.200.200.
★★★★★★★★★★★★★★★★★★★★★★★★★
10cfg.ini
默认网关= 10.245.17.126
默认掩码= 255.255.255.0
DNS= 10.245.1.2
IP前缀= 10.245.17.
───────────────── 版务记录 ─────────────────
执行:HAT
操作:代码加code标签
───────────────── 版务记录 ─────────────────
Last edited by HAT on 2008-12-5 at 20:13 ]
Please help, I wrote it too complicated, and I don't know how to make some parts simpler. Please give me some advice, thank you!!
Known areas for improvement: When querying local information, it can be integrated into a sub-function for calling, but because I don't know how to call the code embedded in the bat, I had to copy it once.
1. Program purpose: Our company does operation and maintenance for China Mobile, and there are always endless systems to install, but they are roughly divided into several types, and it is necessary to set IP, naming, etc. of computers according to certain rules.
2. Computer parameter setting rules:
Computer IP:
The service hall internal network starts with 192, the last digit is different according to different purposes, and other aspects of IP settings are the same.
The service hall business machine uses an IP starting with 10, and the gateway is different
Workgroup rules: Set according to the full pinyin of the service hall, for example, JINGTIAN
Computer naming rule: Service hall abbreviation + last digit of IP + business use, for example, JT-149YW
Computer description rule: Jingtian service hall Business 2
3. Batch processing architecture
Main program: Modify local parameters.bat
Achieve the function of querying and modifying the following content
IP, mask, gateway, DNS, computer name, workgroup, computer description,
Call subroutine: loadini.bat
Achieve the function of reading the preset parameters of the.ini configuration file
Additional program: Generate template.bat
Use the local configuration to generate a general configuration file. It is convenient to call directly on other machines without manually modifying the template file.
The following is the program content
★★★★★★★★★★★★★★★★★★★★★★★★★
Modify local parameters.bat
@echo off
cls
title IP, Mask, Gateway, Computer Name, Workgroup, Computer Description Simple Modification Tool CREATED BY PJH. 080130
if not exist Configuration File goto err
@echo off
cls
echo.
echo Collecting information, please wait...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%b
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config workstation ^|find "Workstation Domain"') do set GZZ=%%a
for /f "delims=" %%a in ('net config server ^|find "Server Comment"') do set miaoshu=%%a
cls
echo.
echo Current machine situation is as follows:
echo.
echo Machine name: %computername%
echo Workgroup: %gzz:~24,60%
echo Description: %miaoshu:~25,60%
echo IP: %nip%
echo Mask: %nmask%
echo Gateway: %ngw%
echo DNS: %dns%
echo.
:slect
echo Please select the purpose of this machine.
echo.
echo 1、Terminal machine
echo 2、Back-end machine
echo 3、Internet cafe machine
echo 4、Business display machine
echo 5、Terminal touch screen
echo.
set /p mycho= Please enter your choice:
cls
if %mycho%#==1# GOTO :cho%mycho%
if %mycho%#==2# GOTO :cho%mycho%
if %mycho%#==3# GOTO :cho%mycho%
if %mycho%#==4# GOTO :cho%mycho%
if %mycho%#==5# GOTO :cho%mycho%
if %mycho%#==# GOTO slect
goto slect
:cho1
set myuse1=Q
set myuse2=Terminal
call loadini 10cfg.ini
goto setting
:cho2
set myuse1=H
set myuse2=Back-end
call loadini 10cfg.ini
goto setting
:cho3
set myuse1=WB
set myuse2=Internet cafe
call loadini 192cfg.ini
goto setting
:cho4
set myuse1=YW
echo Please select the purpose of this machine:
echo.
echo 1、Fetion
echo 2、Mobile simulation
echo 3、Mobile newspaper
echo 4、Infinite Music Club
echo.
set /p ywyt= Please enter your choice:
cls
if %ywyt%#==1# set myuse2=Fetion&&goto cho4goon
if %ywyt%#==2# set myuse2=Mobile simulation&&goto cho4goon
if %ywyt%#==3# set myuse2=Mobile newspaper&&goto cho4goon
if %ywyt%#==4# set myuse2=Infinite Music Club&&goto cho4goon
if %ywyt%#==# GOTO cho4
goto cho4
:cho4goon
call loadini 192cfg.ini
goto setting
:cho5
set myuse1=YW
set myuse2=Touch screen
call loadini 192cfg.ini
goto setting
:setting
echo.
echo The default first half of this machine's IP is %fmyip% Please enter the last digit of this machine's IP number
echo.
set /p ips=
if /I %ips% LEQ 0 (cls&echo Input error!&goto setting)
if /I %ips% GEQ 255 (cls&echo Input error!&goto setting)
for /f "tokens=2 delims==" %%a in ('findstr "Service Hall Name" Configuration File\Service Hall cfg.ini') do set tname=%%a
for /f "tokens=2 delims==" %%a in ('findstr "Service Hall Abbreviation" Configuration File\Service Hall cfg.ini') do set tjx=%%a
for /f "tokens=2 delims==" %%a in ('findstr "Default Workgroup" Configuration File\Service Hall cfg.ini') do set wkgp=%%a
cls
echo.
echo Is this machine %myuse2% No.? If no number is needed, just press Enter.
set /p mynum=
cls
echo.
echo Setting in progress, please wait...
wmic computersystem where Name="%COMPUTERNAME%" call JoinDomainOrWorkgroup Name="%wkgp%" >nul 2>nul
wmic computersystem where name="%COMPUTERNAME%" call rename name="%tjx%-%ips%%myuse1%" >nul 2>nul
net config server /srvcomment:"%tname% %myuse2%%mynum%" >nul 2>nul
netsh interface ip set address "%wkname%" static %fmyip%%ips% %fmymask% %fmygw% 1 >nul 2>nul
netsh interface ip delete dns "%wkname%" all >nul 2>nul
netsh interface ip add dns "%wkname%" %dns% 1 >nul 2>nul
@echo off
cls
echo.
echo Collecting information, please wait...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%b
for /f "tokens=1-10 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%b
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config workstation ^|find "Workstation Domain"') do set GZZ=%%a
for /f "delims=" %%a in ('net config server ^|find "Server Comment"') do set miaoshu=%%a
cls
echo.
echo Current machine situation is as follows:
echo.
echo Machine name: %computername% (It will change to the correct one after restart)
echo Workgroup: %gzz:~24,60%
echo Description: %miaoshu:~25,60%
echo IP: %nip%
echo Mask: %nmask%
echo Gateway: %ngw%
echo DNS: %dns%
echo.
echo Modification completed! Press any key to exit the program.
PAUSE >nul 2>nul
exit
:err
echo.
echo.
echo No configuration file found, please manually set the local parameters first and then run Generate local template.bat to generate configuration information!
echo.
PAUSE >nul 2>nul
loadini.bat
★★★★★★★★★★★★★★★★★★★★★★★★★
for /f "tokens=2 delims==" %%a in ('findstr "IP Prefix" Configuration File\%1') do set fmyip=%%a
for /f "tokens=2 delims==" %%a in ('findstr "Default Mask" Configuration File\%1') do set fmymask=%%a
for /f "tokens=2 delims==" %%a in ('findstr "Default Gateway" Configuration File\%1') do set fmygw=%%a
for /f "tokens=2 delims==" %%a in ('findstr "DNS" Configuration File\%1') do set dns=%%a
★★★★★★★★★★★★★★★★★★★★★★★★★
Generate template.bat
@echo off
MD Configuration File
cls
echo.
echo Generating configuration file...
for /f "tokens=3 delims=: " %%a in ('ipconfig ^|findstr "Ethernet"') do set wkname=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Address"') do set nip=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Mask"') do set nmask=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|findstr "Gateway"') do set ngw=%%a
for /f "delims=" %%a in ('net config workstation ^|find "Workstation Domain"') do set GZZ=%%a
for /f "tokens=2 delims=:" %%a in ('ipconfig /all ^|findstr "DNS"') do set dns=%%a
for /f "delims=" %%a in ('net config server ^|find "Server Comment"') do set miaoshu=%%a
for /f "delims=-" %%a in ("%computername%") do echo Service Hall Abbreviation=%%a>Configuration File\Service Hall CFG.INI
echo Default Workgroup=%gzz:~25,60%>>Configuration File\Service Hall CFG.INI
for /f "delims= " %%a in ("%miaoshu:~25,60%") do echo Service Hall Name=%%a>>Configuration File\Service Hall CFG.INI
echo Default Gateway=%ngw%>Configuration File\ipCFG.INI
echo Default Mask=%nmask%>>Configuration File\ipCFG.INI
echo DNS=%dns%>>Configuration File\ipCFG.INI
for /f "tokens=1-3 delims=." %%a in ("%nip%") do set fstip=%%a&echo IP Prefix=%%a.%%b.%%c.>>Configuration File\ipCFG.INI
if /i %fstip%==10 (del Configuration File\10cfg.ini /q /f&rename Configuration File\ipCFG.INI 10cfg.ini) else (del Configuration File\192cfg.ini /q /f&rename Configuration File\ipCFG.INI 192cfg.ini)
cls
echo.
echo Operation completed, please check the files in the folder "Configuration File"
echo.
PAUSE
★★★★★★★★★★★★★★★★★★★★★★★★★
Service Hall CFG.INI
Service Hall Abbreviation=CS
Default Workgroup=CESHIFUWUTING
Service Hall Name=Test Service Hall
★★★★★★★★★★★★★★★★★★★★★★★★★
192cfg.ini
Default Gateway= 192.200.200.1
Default Mask= 255.255.255.0
DNS=
IP Prefix= 192.200.200.
★★★★★★★★★★★★★★★★★★★★★★★★★
10cfg.ini
Default Gateway= 10.245.17.126
Default Mask= 255.255.255.0
DNS= 10.245.1.2
IP Prefix= 10.245.17.
───────────────── Moderation Record ─────────────────
Performed: HAT
Operation: Added code tags to the code
───────────────── Moderation Record ─────────────────
Last edited by HAT on 2008-12-5 at 20:13 ]