China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 01:16
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch script to modify computer name\workgroup\description\IP\mask\gateway\DNS View 2,746 Replies 6
Original Poster Posted 2008-02-01 15:50 ·  中国 广东 深圳 福田区 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pusofalse +3 2008-06-02 14:16
Floor 2 Posted 2008-02-01 15:53 ·  中国 广东 深圳 福田区 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
If any friend has just started to get in touch with batch processing and wants to ask about some of its grammars, you are welcome to ask questions, and I will do my best to answer. Also, please leave an email, because I may not be free to surf the Internet often. Thank you!
Floor 3 Posted 2008-06-02 14:13 ·  中国 陕西 西安 电信
新手上路
Credits 12
Posts 6
Joined 2008-05-30 11:08
18-year member
UID 120069
Gender Male
Status Offline
A bit...
Too...
Powerful
I used the IP - changing script circulating on the network
Sweat...
Floor 4 Posted 2008-06-02 14:17 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
It feels a bit complicated script~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 5 Posted 2008-12-05 17:17 ·  中国 广东 深圳 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
I didn't come online for a long time because the forum seemed to be unavailable some time ago.

Today, I received an email from a net friend asking about several commands in this batch script. After replying to the email and coming back to take a look, I was even awarded points. That made me very happy, heh heh.
Floor 6 Posted 2008-12-05 21:16 ·  中国 福建 泉州 电信
初级用户
★★
Credits 112
Posts 81
Joined 2008-04-11 23:47
18-year member
UID 115654
Gender Male
Status Offline
Learning
CN-DOS批处理室非官方QQ群①:61377162 ②:70174619 ③:75070617
请勿尝试同时加多个群
Floor 7 Posted 2008-12-11 10:36 ·  中国 上海 电信
初级用户
★★
Credits 160
Posts 81
Joined 2006-07-27 17:52
20-year member
UID 59377
Gender Male
Status Offline
Feels great, push it up for everyone to share
Forum Jump: