中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 16:53
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Closed: About the problem of automatically changing IP address (XP)
Printable Version  8,238 / 22
Floor1 ronin Posted 2005-11-10 13:42
中级用户 Posts 111 Credits 421
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 ]
Floor2 willsort Posted 2005-11-10 20:49
元老会员 Posts 1,512 Credits 4,432
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.
Floor3 ronin Posted 2005-11-11 11:00
中级用户 Posts 111 Credits 421
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.
Floor4 ronin Posted 2005-11-11 20:43
中级用户 Posts 111 Credits 421
Floor5 JonePeng Posted 2005-11-11 22:30
金牌会员 Posts 1,883 Credits 4,562 From 广东广州
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.
Floor6 ronin Posted 2005-11-11 22:40
中级用户 Posts 111 Credits 421
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?
Floor7 ronin Posted 2005-11-11 23:00
中级用户 Posts 111 Credits 421
@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?
Floor8 无奈何 Posted 2005-11-12 00:27
荣誉版主 Posts 356 Credits 1,338
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

Floor9 ronin Posted 2005-11-12 14:13
中级用户 Posts 111 Credits 421
I made a batch script, but netsh doesn't work
Floor10 willsort Posted 2005-11-12 19:14
元老会员 Posts 1,512 Credits 4,432
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
Floor11 ronin Posted 2005-11-12 21:22
中级用户 Posts 111 Credits 421
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
Floor12 ronin Posted 2005-11-14 10:38
中级用户 Posts 111 Credits 421
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?
Floor13 unique009 Posted 2005-11-14 11:17
初级用户 Posts 8 Credits 28
I also quite want to know this question
Floor14 willsort Posted 2005-11-14 17:51
元老会员 Posts 1,512 Credits 4,432

—————————————— 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 ]
Floor15 chenhui530 Posted 2005-11-15 14:08
高级用户 Posts 273 Credits 772
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
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023