1.

Solve : Automatic Set IP?

Answer»

My BAT script

Code: [Select]@ECHO off
:start
cls
ECHO.
ECHO Press : 1. Get IP from DHCP
ECHO 2. Change IP : 192.168.10.x / 32
ECHO 3. Show Ip config
ECHO X. Exit

set choice=
echo Choice :
set /p choice=

if not '%choice%'=='' set choice=%choice:~0,1%
:: Lay bat dau tu ky tu 0 cua chuoi~, do dai cua chuoi la 1.
:: choice=abcdef
::%choice:~3,2%=de

if '%choice%'=='1' goto function1
if '%choice%'=='2' goto function2
if '%choice%'=='3' goto function3
if '%choice%'=='x' goto end

ECHO "%choice%" is not valid please try again
ECHO.
goto start


:function1
echo.
Netsh interface ip set address name="lan" dhcp
goto end

:function2
set x=
echo.
echo IP : 192.168.10.X
echo X =
Set /p x=
Netsh interface ip set address name="lan" static 192.168.10.%x% 255.255.255.0
goto end

:function3
ipconfig | find /I "lan"
PAUSE
goto start

::Local Area Connection

:end
Script work fine, but i want it all automatic. I don't need to input anything . 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?
Code: [Select]set name=lan

for /f "skip=2 tokens=1,2* delims= " %%a in ('netsh interface ip show addresses %name%') do (
set dhcpOn=%%c
goto :EndFor
)
:EndFor
if /I "%dhcpOn%" equ "yes" goto function2
if /I "%dhcpOn%" equ "no" goto function1
pause
this works for me, i dont know if it will for you Thanks, Devcom again.
i am not test your code yet, but i think it will work.
I know the command
netsh int ip show add
But i didn't know we can only display 1 interface by input the interface name like that
netsh int ip show add "interface name"Quote

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 %IPPart1%.%IPPart2%.%IPPart1%.%%a >nul
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.


Discussion

No Comment Found