Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Automatic Set IP  (Read 2802 times)

0 Members and 1 Guest are viewing this topic.

tonlo

    Topic Starter


    Rookie

    Automatic Set IP
    « on: April 20, 2009, 07:47:47 AM »
    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  ;D. 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?

    devcom



      Apprentice

      Thanked: 37
      Re: Automatic Set IP
      « Reply #1 on: April 20, 2009, 08:43:38 AM »
      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  ;)
      Download: Choice.exe

      tonlo

        Topic Starter


        Rookie

        Re: Automatic Set IP
        « Reply #2 on: April 20, 2009, 09:03:33 AM »
        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"

        tonlo

          Topic Starter


          Rookie

          Re: Automatic Set IP
          « Reply #3 on: April 20, 2009, 11:33:28 PM »
          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.

          Batcher



            Rookie

            Thanked: 5
            Re: Automatic Set IP
            « Reply #4 on: April 21, 2009, 12:21:01 AM »
            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
            « Last Edit: April 22, 2009, 12:38:21 AM by Batcher »

            tonlo

              Topic Starter


              Rookie

              Re: Automatic Set IP
              « Reply #5 on: April 21, 2009, 05:29:34 AM »
              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?

              devcom



                Apprentice

                Thanked: 37
                Re: Automatic Set IP
                « Reply #6 on: April 21, 2009, 06:45:58 AM »
                if you allways want same 'x' then use this :
                Code: [Select]
                :function2
                    set x=24
                    echo IP : 192.168.10.%x%
                Download: Choice.exe

                tonlo

                  Topic Starter


                  Rookie

                  Re: Automatic Set IP
                  « Reply #7 on: April 21, 2009, 07:20:59 AM »
                  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  8)

                  devcom



                    Apprentice

                    Thanked: 37
                    Re: Automatic Set IP
                    « Reply #8 on: April 21, 2009, 11:59:15 AM »
                    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's
                    Download: Choice.exe

                    Batcher



                      Rookie

                      Thanked: 5
                      Re: Automatic Set IP
                      « Reply #9 on: April 22, 2009, 12:39:53 AM »
                      Hi tonlo,

                      I've fixed that typo. Thanks for your reminder.