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-06-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Closed: About the problem of automatically changing IP address (XP) View 7,877 Replies 22
Original Poster Posted 2005-11-10 13:42 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
Reference materials:
http://www.cn-dos.net/forum/viewthread.php?tid=16990&fpage=12
http://www.cn-dos.net/forum/viewthread.php?tid=17272&fpage=8

The question is: Use batch processing to automatically modify the IP address, subnet mask, and gateway of XP. Since it is for company use and cannot all use the same IP address, it is hoped to give an IP range such as 192.168.1.*, where * is randomly assigned, and the range is limited such as 100 - 200. How to write this batch processing file for everyone?

Refer to the information found online, everyone can also refer to
http://www.btvgd.com/net/bbs/topic.php?filename=f_8&forumid=10

[ Last edited by willsort on 2005-11-30 at 19:53 ]
Floor 2 Posted 2005-11-10 20:49 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re ronin:

There are no problems with modifying network configurations such as IP addresses and generating random IPs. The problem is how to ensure that random IPs do not repeat. There are only 101 values from 100 to 200. If there are more than 20 running hosts, there is a high possibility of repetition. Also, since this batch script may run on multiple different machines, the method of generating an occupied IP list is generally not considered. It is better to use a unique value specific to each machine as a seed and generate a unique corresponding value with a specific hash function. What do you all think about this? Of course, if Brother ronin has other compromise methods, feel free to propose them.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2005-11-11 11:00 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
You can expand the range to 100-250 to generate random IP addresses. Generally, there are not many company computers, so duplication doesn't need to be considered. If duplication is encountered, there will be a prompt, and then you can just run it again. This problem occurs because the company's computer systems all use GHOST, so the IPs are the same. The plan is to run this batch script after GHOST is completed to avoid IP duplication.
Floor 4 Posted 2005-11-11 20:43 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
Floor 5 Posted 2005-11-11 22:30 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
It is better to set the router to allow DHCP, set the range of the IP address (the fourth digit) to 100-200, and then set the IP and DNS of each machine to obtain automatically. In this way, each machine can automatically obtain an IP when it starts and enters the system, without manual configuration.
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 6 Posted 2005-11-11 22:40 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
Because the customer's network environment cannot be controlled by themselves, I am doing customer service work.

set x=%random:~-1%
set x1=%random:~-1%
;echo Random number: %x%
::Write the obtained random number to the Wattcp.txt file
echo IP=192.168.1.1%x%%x1%> Wattcp.txt
echo NETMASK=255.255.255.0>> Wattcp.txt
echo GATEWAY=192.168.1.1>> Wattcp.txt

Basically, the IP has been obtained and controlled in the range of 100-199. I don't know what method there is to specify the Random number in the range of 100-254?
Floor 7 Posted 2005-11-11 23:00 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
@echo off
set x=%random:~-1%
set x1=%random:~-1%
set eth="本地连接"
set ip=192.168.1.1%x%%x1%
set gateway=192.168.1.1
set netmasks=255.255.255.0

The final batch script I wrote seems to be correct, but it didn't change the IP of the "Local Area Connection". Where is the mistake?
Floor 8 Posted 2005-11-12 00:27 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
You are always setting environment variables, how can you change the IP? You need to enter the netsh setup mode.

Let's transfer a paragraph of article to see if it inspires you.


In the work process, there will be situations where network debugging is carried out in different subnets, and it is often necessary to switch the machine among several different IP addresses. Changing the IP address in the Win2000 operating system is more convenient than in Win98 (because there is no need to restart the computer after changing the IP address), but it still needs to be operated in the network properties settings. Is there a simpler method, such as double-clicking a shortcut with the mouse to realize the switching of the IP address?

The answer is yes. In Win2000, the netsh command can be used to realize this function. First, enter the command line mode (you can enter the command line mode by typing "cmd" in "Start → Run"), and type netsh at the prompt to enter the netsh interface. Then type int ip to enter the interface IP configuration mode, and type dump to list the interface IP configuration information:

C:\Documents and Settings\Administrator>netsh
netsh>
netsh>int ip
interface ip>dump
# ----------------------------------
# Interface IP configuration
# ----------------------------------
pushd interface ip
# IP configuration of "Local Area Connection" (Note: The following display depends on the specific machine situation)
set address name = "Local Area Connection" source = static addr = 192.168.0.5 mask = 255.255.255.0
set address name = "Local Area Connection" gateway = 192.168.0.2 gwmetric = 1
set dns name = "Local Area Connection" source = static addr = 61.237.17.181
add dns name = "Local Area Connection" addr = 211.97.168.129
add dns name = "Local Area Connection" addr = 211.98.4.1
set wins name = "Local Area Connection" source = static addr = none
# IP configuration of "Local Area Connection 2"
set address name = "Local Area Connection 2" source = dhcp
set dns name = "Local Area Connection 2" source = dhcp
set wins name = "Local Area Connection 2" source = dhcp
popd
# End of interface IP configuration

There is a trick to quickly switch IP addresses 2

Now we can roughly understand the way to change the IP address with the netsh command, that is, to realize it through "set address name = connection name (the connection name should be enclosed in quotes) source = static addr = IP address mask = subnet mask".

Try it directly with netsh in the command line mode. For example, to change the IP address of this computer to 192.168.0.7 and the subnet mask to 255.255.255.0, the following operations can be carried out:

C:\Documents and Settings\Administrator>netsh
netsh>int ip
interface ip>set address name ="Local Area Connection"source = static addr = 192.168.0.7 mask = 255.255.255.0
Confirm the change.
interface ip>exit
Verify with the ipconfig command again:
C:\Documents and Settings\Administrator>ipconfig
Windows 2000 IP Configuration
Ethernet adapter 本地连接 2:
Media State . . . . . . . . . . . : Cable Disconnected
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.0.7
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.2

Floor 9 Posted 2005-11-12 14:13 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
I made a batch script, but netsh doesn't work
Floor 10 Posted 2005-11-12 19:14 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re ronin:

In XP, you can use netsh in batch processing to configure almost all network information. The sample is as follows:

netsh interface ip set address "Local Area Connection" static 10.0.0.5 255.255.255.0 10.0.0.4 1
netsh interface ip set dns "Local Area Connection" static 10.0.0.4

  And to generate a random IP, you can use set /a to perform operations on the random variable %random%.

set rndip=%random%
set /a rndip=rndip%156+100
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2005-11-12 21:22 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
Thanks to willsort, Wu Nai He, and JonePeng for help. It's already fixed. The following batch script works perfectly:
set x=%random:~-1%
set x1=%random:~-1%
netsh interface ip set address "本地连接" static 192.168.1.1%x%%x1% 255.255.255.0 192.168.1.1 1
Floor 12 Posted 2005-11-14 10:38 ·  中国 广东 梅州 电信
中级用户
★★
Credits 421
Posts 111
Joined 2003-08-29 00:00
22-year member
UID 9153
Gender Male
Status Offline
Continuing from the previous question, the command is `netsh interface ip set address "本地连接" static 192.168.1.1%x%%x1% 255.255.255.0 192.168.1.1 1`. During use, it is found that because of changing the network card or other problems, many computers do not use "Local Area Connection", many use "Local Area Connection 3" and so on, this batch processing will be wrong. Can the batch processing detect which one the local connection is, or just replace it with LOCAL?
Floor 13 Posted 2005-11-14 11:17 ·  中国 广东 深圳 宝安区 电信
初级用户
Credits 28
Posts 8
Joined 2005-11-05 13:58
20-year member
UID 44636
Status Offline
I also quite want to know this question
Floor 14 Posted 2005-11-14 17:51 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

—————————————— Edited by willsort ——————————————
Merge topic: "17732 - About XP detecting local connection problem"
Merge reason: The two topics have direct contextual connections

Delete posts: Three posts by voiL and ronin
Delete reason: The content of the posts is about the relationship between the two Q&A topics
The two can request points compensation in the following posts
—————————————— Edited by willsort ——————————————


Re ronin:

Regarding obtaining the connection name, you can also use netsh. The way I have found so far is:

netsh interface dump

Using for + set + if, you can obtain and judge the connection names existing in it. The following is an example of code in a batch file, which will store the result in the environment variable addressname. It should not be fully comprehensive. It is recommended to test it in detail before actual application.

for /f "tokens=1,2,3" %%a in ('netsh interface dump') do if "%%b"=="address" echo %%a %%b%%c

[ Last edited by willsort on 2005-11-14 at 20:32 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 15 Posted 2005-11-15 14:08 ·  中国 重庆 巴南区 电信
高级用户
★★
Credits 772
Posts 273
Joined 2004-10-23 00:00
21-year member
UID 32861
Gender Male
Status Offline
Get connection name:
for /f "skip=3 tokens=1,2,3 delims=: " %%a in ('ipconfig') do for %%A in (%%c) do set a=%%A && goto view

:view
echo %a%
pause
http://www.msfans.net/bbs/
Forum Jump: