1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Another batch scripting question...

Discussion in 'Software Development' started by Codiah, Oct 16, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Codiah

    Codiah Thread Starter

    Joined:
    Sep 27, 2008
    Messages:
    116
    Just have a quick question, is it possible to get your external IP using a batch file.... i know you can grab the output of "ipconfig" but if your behind a router you get the routers IP.. i want to grab the IP from this ISP outside of the router..
     
  2. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi Codiah,

    What type of a connection do you have? (dialup, dsl, FiOS)

    For example, I am on dialup, and use wvdial. I can interrogate /var/log/messages with a script to get the remote ISP's IP address. From it, I construct a ping command to interrogate the ISP's IP address every 30 seconds.

    Unfortunately, I do not have a router to be able to simulate your situation, and I run in a Linux environment.

    For Windows, ipconfig is probably your best bet.

    -- Tom
     
  3. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Quick and dirty, but this will do it:

    Copy the following into Notepad and save it as gethttp.vbs

    Code:
    set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run "cmd"
    WScript.Sleep 100
    WshShell.AppActivate "{%}SystemRoot{%}\system32\cmd.exe"
    WScript.Sleep 100
    WshShell.SendKeys "telnet -f {%}temp{%}\{}}1{{}.txt checkip.dyndns.org 80~"
    WScript.Sleep 100
    WshShell.SendKeys "GET / HTTP/1.0~"
    WshShell.SendKeys "Host: checkip.dyndns.org~"
    WshShell.SendKeys "~"
    WshShell.SendKeys "~"
    WScript.Sleep 1000
    WshShell.SendKeys "~"
    WshShell.SendKeys "exit~"
    Copy the following into Notepad and save it with a .bat or .cmd extension:
    Code:
    @echo off
    cscript //nologo gethttp.vbs
    Findstr /I "Current" "%temp%\}1{.txt" >"%temp%\}2{.txt"
    For /F "usebackq tokens=2-5 delims=.:" %%I In ("%temp%\}2{.txt") Do Set _ip=%%I.%%J.%%K.%%L
    Set _ip=%_ip:~1,-14%
    Echo Your IP address is %_ip%
    Del "%temp%\}?{.txt"
    When you run the batch, it uses telnet to get the web page at checkip.dyndns.org, parses it, and stores your Public IP in the variable _ip.

    Your AntiVirus program may give you a warning about the script, you'll need to authorize it so it will run.

    Depending on your system you may be able to decrease the sleep times; they are in milliseconds.

    There may be a way to hide the command prompt window the script opens, but I'm not sure sendkeys will work if it's hidden.

    If your router supports telnet you should be able to just telnet into the router and read the address. You can also use Network Management software to get the IP via SNMP if the router supports SNMP and is configured for it.

    HTH

    Jerry
     
  4. Codiah

    Codiah Thread Starter

    Joined:
    Sep 27, 2008
    Messages:
    116
    Havn't figured out why, but your script wasn't storing the variable correctly... but it gave me a base to build from, so thanks it was greatly appreciated..
     
  5. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    My first guess is that an IP with a different number of digits must be spaced a bit different. Mine consists of these digits: XX.XXX.XXX.XX

    Copied and pasted the files from the forum, and it still works, so there was no typo in the post. Assuming nothing got added on your end, that's the only thing I can think of.

    Glad it was useful for you.

    Jerry
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/759663

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice