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

Author Topic: updating hosts file with a batch  (Read 6585 times)

0 Members and 1 Guest are viewing this topic.

Patplays852

    Topic Starter


    Rookie
    updating hosts file with a batch
    « on: April 25, 2009, 01:33:05 PM »
    Hello, I've tried to find what i was looking for on the forums and the internet and have not been able to find an answer yet.

    Here is the situation:

    I set up an ftp server on my friends computer (yippie) the bad news is his ISP gives him a dynamic EXTERNAL IP address... bad news for ftp'ing lol.  We found a program that sences when his external ip changes, and it uploads a text file to my ftp server with just the new IP in it.

    I would like to make a batch file that runs every hour or so (i can do that) but i want it to DELETE the old entry in the hosts file and then add the new ip to the hosts file.  (I have the text file setup so that it has:
    ***.***.***.*** [tab] dan

    this way when i want to ftp into his server all i need to do is type:
    ftp://dan:(port)

    The key part that is stopping me from acomplishing this is that i need the old entry to be deleted, otherwise i am going to have multiple addresses for the same host (dan)

    Thank you very much for your help =]

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: updating hosts file with a batch
      « Reply #1 on: April 25, 2009, 06:37:30 PM »
      if you can use vbscript
      Code: [Select]
      Set objFS = CreateObject("Scripting.FileSystemObject")
      strFile = "c:\test\host.txt"
      Set objFile = objFS.OpenTextFile(strFile,1)
      Do Until objFile.AtEndOfStream
      strLine = objFile.ReadLine
      splitted = Split(strLine)
      splitted(0) = "new ip address"
      WScript.Echo Join(splitted)
      Loop

      save as myscript.vbs and on command line
      Code: [Select]
      C:\test>cscript /nologo myscript.vbs
      new ip address [tab] dan


      redirect to new file as desired. (or do your modification inside the script, i leave it to you)

      or if you can use tools like Perl (v5.8++)
      Code: [Select]
      C:\test>perl -lane "$F[0]=newip; print join(' ',@F);" host.txt
      newip [tab] dan


      Patplays852

        Topic Starter


        Rookie
        Re: updating hosts file with a batch
        « Reply #2 on: April 25, 2009, 07:06:33 PM »
        Excuse my ignorance of vbscripts, but how does that find the line with the old ip address on it, or do i just need to make sure that it is on the last line?

        Code: [Select]
        Set objFS = CreateObject("Scripting.FileSystemObject")
        strFile = "D:\FROM DAN\IP\ipaddress.txt"  //the text file with ip address in it.
        Set objFile = objFS.OpenTextFile(strFile,1)
        Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        splitted = Split(strLine)
        splitted(0) = "new ip address"
        WScript.Echo Join(splitted)
        Loop

        Code: [Select]
        C:\FROM DAN\IP\>cscript /nologo myscript.vbs
        new ip address [tab] dan

        and im wondering in there where it points to the hosts file to update it?

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: updating hosts file with a batch
          « Reply #3 on: April 25, 2009, 07:16:21 PM »
          i only assume you have 1 line in that host file. to check for old ip address, you can use Instr()
          Code: [Select]
          ...
          if Instr(strLine,"old ip address") > 0 then
            wscript.echo "found "
            ' do the splitting here......
          end if


          to learn more about vbscript, download this and read.

          Patplays852

            Topic Starter


            Rookie
            Re: updating hosts file with a batch
            « Reply #4 on: April 25, 2009, 07:35:29 PM »
            nope, hosts file has over 11k lines in it (thanks to spybot S&D)

            I will download that now and see what i can do =] thank you very much for your help.
            -EDIT  (looks like im not ganna get it from windows site, wants me to install the genuine validation =P and i can't have that) lol  Im still able to get windows updates, not sure if i install that plugin what will happen tho, and not ganna risk it.

            If anyone has a batch file that can do this, my ears are still open

            I can make the LAST line of the hosts file the ip that needs to be deleted and replaced. so if anyone can help with that info that would be greatly appreciated =]

            gh0std0g74



              Apprentice

              Thanked: 37
              Re: updating hosts file with a batch
              « Reply #5 on: April 25, 2009, 07:50:13 PM »
              nope, hosts file has over 11k lines in it (thanks to spybot S&
              then show some samples and where the IP you need to change is. show also your sample output.

              Quote
              I will download that now and see what i can do =] thank you very much for your help.
              -EDIT  (looks like im not ganna get it from windows site, wants me to install the genuine validation =P and i can't have that) lol  Im still able to get windows updates, not sure if i install that plugin what will happen tho, and not ganna risk it.
              don't underestimate the power of googling.... check here


              Patplays852

                Topic Starter


                Rookie
                Re: updating hosts file with a batch
                « Reply #6 on: April 25, 2009, 07:55:53 PM »
                this is at the end of the hosts file atm:
                Code: [Select]
                127.0.0.1 www.searchdestroydownload.com
                127.0.0.1 searchdestroydownload.com
                127.0.0.1 win-loads.net
                127.0.0.1 www.win-loads.net
                127.0.0.1 www.win-pc-defender.com
                127.0.0.1 win-pc-defender.com
                # End of entries inserted by Spybot - Search & Destroy
                127.0.0.1 activate.adobe.com
                ***.***.***.*** pat
                ***.***.***.***   dan

                this is the ip.txt
                Code: [Select]
                ?*?.?*?.?*?.?*?   dan

                i need to replace the last line of the hosts file [***.***.***.***   dan] with [?*?.?*?.?*?.?*?   dan]
                lol, was using three "?" but it came up as a smiley ???
                note his ip is dynamic, and it wont have 3 numbers in each octet every time.

                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: updating hosts file with a batch
                  « Reply #7 on: April 25, 2009, 08:29:16 PM »
                  try this then
                  Code: [Select]


                  Set regEx = New RegExp       
                  regEx.Pattern = "\s+"   'pattern to substitute all spaces/tabs with 1 space
                  Set objFS = CreateObject("Scripting.FileSystemObject")
                  strFile = "c:\test\host.txt"
                  strIPFile = "c:\test\ip.txt"
                  Set objFile = objFS.OpenTextFile(strIPFile,1)
                  Do Until objFile.AtEndOfStream
                  strLine = objFile.ReadLine
                  strLine = regEx.Replace(strLine, " ")
                  splitted = Split(strLine," ")
                  strIPtoChange = splitted(0)
                  strUserName = splitted(1)
                  Loop

                  Set objFile = objFS.OpenTextFile(strFile,1)
                  Do Until objFile.AtEndOfStream
                  strLine = objFile.ReadLine
                  If InStr(strLine,strUserName) > 0 Then
                  splitted = Split(strLine)
                  splitted(0) = strIPtoChange
                  strLine = Join(splitted," ")
                  End If
                  WScript.Echo strLine

                  Loop

                  Patplays852

                    Topic Starter


                    Rookie
                    Re: updating hosts file with a batch
                    « Reply #8 on: April 25, 2009, 08:52:25 PM »
                    ok, i have sync.bat, and sync2.vbs in the same folder as DansIP.txt

                    Sync.bat:
                    Code: [Select]
                    @echo off
                    cls

                    cd D:\FROMDAN\IP
                    start D:\FROMDAN\IP\>cscript /nologo sync2.vbs
                    new ip address  dan

                    pause
                    I only get the error that new is not an internal/external command.

                    sync2.vbs:
                    Code: [Select]
                    Set regEx = New RegExp       
                    regEx.Pattern = "\s+"   'pattern to substitute all spaces/tabs with 1 space
                    Set objFS = CreateObject("Scripting.FileSystemObject")
                    strFile = "c:\WINDOWS\system32\drivers\etc\hosts.txt"
                    strIPFile = "D:\FROMDAN\IP\DansIP.txt"
                    Set objFile = objFS.OpenTextFile(strIPFile,1)
                    Do Until objFile.AtEndOfStream
                    strLine = objFile.ReadLine
                    strLine = regEx.Replace(strLine, " ")
                    splitted = Split(strLine," ")
                    strIPtoChange = splitted(0)
                    strUserName = splitted(1)
                    Loop

                    Set objFile = objFS.OpenTextFile(strFile,1)
                    Do Until objFile.AtEndOfStream
                    strLine = objFile.ReadLine
                    If InStr(strLine,strUserName) > 0 Then
                    splitted = Split(strLine)
                    splitted(0) = strIPtoChange
                    strLine = Join(splitted," ")
                    End If
                    WScript.Echo strLine

                    Loop

                    hosts file remains unchanged.

                    gh0std0g74



                      Apprentice

                      Thanked: 37
                      Re: updating hosts file with a batch
                      « Reply #9 on: April 25, 2009, 09:39:30 PM »
                      you do not need to use start to call cscript. hostfile remain unchanged because all i do in the vbs is to echo out the changed lines. I leave it to you to figure out how to rename file. Its basic DOS

                      Code: [Select]
                      @echo off
                      cscript /nologo myscript.vbs > newfile
                      REM rename the newfile back to oldfile.

                      Patplays852

                        Topic Starter


                        Rookie
                        Re: updating hosts file with a batch
                        « Reply #10 on: April 26, 2009, 11:11:33 AM »
                        got another batch file to work:

                        Code: [Select]
                        @echo off
                        cls
                        pushd "%systemroot%\system32\drivers\etc"
                        type hosts|find /i /v "#dan's dynamic address" > hosts.new
                        move "%cd%\hosts.new" "%cd%\hosts"
                        for /f "usebackq" %%a in ("D:\FROM_DAN\IP\DansIP.txt") do (
                        >>hosts echo %%a dan #dan's dynamic address.
                        )
                        popd