| 1. |
Solve : Automatic Set IP? |
|
Answer» My BAT script To make the script automatically, i think the first thing i need to know DHCP status of the interface. and then if DHCP is yes ----> run :function2, else run function1. I don't know how to get the DHCP status of an interface, how if machine have 1 interface and more than 2 interface?Now the second thing is function2, it still request user input. Can we it automatic too? My solution is random a number between 1-254 and set to X. And then run ping command if it is request time out --> set X ---> done, Else loop random again. It will have a risk if there are a machine enable firewall. Quote from: tonlo on April 20, 2009, 11:33:28 PM Now the second thing is function2, it still request user input. Can we it automatic too? My solution is random a number between 1-254 and set to X. And then run ping command if it is request time out --> set X ---> done, Else loop random again. It will have a risk if there are a machine enable firewall. You can refer to below code to get an available IP. Let me know if you need further help. Code: [Select]@echo off set IPPart1=192 set IPPart2=168 set IPPart3=10 set IPPart4=# echo Need MINUTES to look for an available IP. for /l %%a in (2,1,254) do ( ping -n 1 -l 1 %IPPart1%.%IPPart2%.%IPPart1%.%%a >nul ) rem Don't care whether an IP is pingable. We just need the arp cache. arp -a>"%temp%\UsedIP.tmp" for /l %%a in (254,-1,2) do ( findstr "%IPPart1%\.%IPPart2%\.%IPPart3%\.%%a" "%temp%\UsedIP.tmp"||set IPPart4=%%a goto :FindIP ) :FindIP set AvaiIP=%IPPart1%.%IPPart2%.%IPPart3%.%IPPart4% if "%IPPart4%" neq "#" ( echo Find an available IP: %AvaiIP% ) else ( echo No IP is available currently. ) pause goto :eof Hi BATCHER, Maybe that is your typing mistake Quote for /l %%a in (2,1,254) do (ping -n 1 -l "missing size" %IPPart1%.%IPPart2%.%IPPart3%.%%a >nul I understand why to ping all the IP in subnet 192.168.10/24. But i take so long, i changed the buffer size to 10byte to test how long does it take. Over 2min just for get ARP table. I can't spend 2min to automatic set the IP address because i can set IP in window graphic mode in ~30s. Any better solutions?if you allways want same 'x' then use this : Code: [Select]:function2 set x=24 echo IP : 192.168.10.%x% Can't because i will share my script with other people in LAN. It will occur conflict IP. How about if it change subnet to 16, and random the 3rd, 4th Octet. More random time, more less conflict happen Code: [Select]@echo off setlocal enabledelayedexpansion set IP=192.168.10 for /L %%P in (10,1,30) do ( ping -n 1 -w 10 %IP%.%%P >nul if !errorlevel! equ 1 echo.%IP%.%%P is free... if !errorlevel! equ 0 echo.%IP%.%%P isn't free... ) pause you can use that so all people in lan can get first free IP it took me 11 sec to check 20 ip'sHi tonlo, I've fixed that typo. Thanks for your reminder. |
|