Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm streaming audio from my Windows 7 laptop to a sound card attached to a router. I have a little batch script to start streaming.

REM Kill any instances of vlc 
taskkill /im vlc.exe
"c:\Program Files\VideoLAN\VLC\vlc.exe" <parameters to start http streaming>
REM Wait for vlc
TIMEOUT /T 10
REM start playback on router
plink -ssh me@192.168.1.1 -pw password killall -9 madplay
plink -ssh me@192.168.1.1 -pw password wget -q -O - http://192.1.159:8080/audio | madplay -Q --no-tty-control - &

As you see the http stream is hard coded. It would be nice to get the address dynamically to reuse the script on other machines. Any ideas?

share|improve this question
    
The LAN address? Be careful with that. –  grawity Jan 7 '11 at 12:45
    
Also, suggestion: use hostnames instead of addresses. Windows sends its hostname in DHCP requests, so if your router does both DHCP and DNS, link them. Otherwise, there's NetBIOS (smbclient+nss_wins) and Apple Bonjour (avahi+nss_mdns). (I could also suggest LLMNR, but I'm not sure if there are any Linux resolvers for it.) This would remove the need for specifying the address in the router. –  grawity Jan 7 '11 at 12:53
    
@grawity: Could you elaborate? I'm doing this basically without any knowledge about networking. Can I query for the(?) lan ip address assigned by the router? Pinging //<MY-laptop-hostname>/ does not work. –  Ville Koskinen Jan 7 '11 at 16:22
    
@Ville: And I'm doing this without any knowledge about why you need to "pass the laptop's LAN IP to the router" and how exactly you are doing it. :/ –  grawity Jan 7 '11 at 16:26
    
@grawity: OK, I have edited the question. –  Ville Koskinen Jan 7 '11 at 16:41
show 5 more comments

7 Answers

up vote 2 down vote accepted

Here is a simple example that will get the ipv4 address of the current machine:

:: find IP address in scriptable format
:: !!Windows 7 specific at the moment!!
:: Note this only works with one adapter connected
@echo off
:: get ipv4
ipconfig | findstr IPv4 > ipadd.txt

:: For statement to find the numbers
for /F "tokens=14" %%i in (ipadd.txt) do ( 
@echo %%i 
)
del ipadd.txt /Q

This just echos out the IP but you can integrate it in.

share|improve this answer
    
interesting, but when I try ipconfig | findstr IPv4 it finds nothing. Is it a type that you think findstr recognizes? Here's the documentation for findstr technet.microsoft.com/en-us/library/bb490907.aspx any idea where it says about IPv4 being a recognized parameter? All findstr IPv4 does is looks for the string .. so, does your windows version use the term IPv4 in ipconfig output? xp doesn't. –  barlop Jan 7 '11 at 23:26
    
I wrote the script on Windows 7, I don't have a XP box to try it out on. findstr grabs only the line with the string IPv4 on it that is passed to it by the pipe(the entire output of ipconfig). You should be able to adapt that to snag whatever line the ipv4 address line is on XP. If I get around to either firing up a VM or swapping SSD's I will try it out on XP. –  Dan M. Jan 7 '11 at 23:52
    
Thanks Dan M, this doesn't require additional programs and works on my localized Windows 7 version. –  Ville Koskinen Jan 9 '11 at 19:11
add comment

Windows XP one-liner (no IPv6 installed), note "findstr 192." - you may need to remove of adjust it (I use it to select necessary interface):

for /F "tokens=2 delims=:" %%i in ('"ipconfig | findstr IP | findstr 192."') do SET LOCAL_IP=%%i

echo %LOCAL_IP%
share|improve this answer
add comment

Here is a command to output the default gateway, then a command to output the laptop's IP, a local IP. Just so you see those 2 commands

Then a command to dump the local IP one, which you want, to a file called afile.

Then a command to dump afile into an environment variable called a

You can download grep from gnuwin32

C:\>ipconfig | grep -E -i "def" | grep -E -o "[0-9][0-9.]+"  
192.168.1.254

C:\>ipconfig | grep -E -i "IP Address" | grep -E -o "[0-9][0-9.]+"  
192.168.1.67

C:\>ipconfig | grep -E -i "IP Address" | grep -E -o "[0-9][0-9.]+" > afile

C:\>for /f %f in ('type afile') do set a=%f

C:\>set a=192.168.1.67   <-- that got executed automatically

C:\>echo %a%
192.168.1.67

C:\>

So your bat file could be dothis.bat and it would have these 2 lines and of course you can amend the name of the file(afile) and the environment variable (a). note in a bat file you use %%f(or whatever letter) instead of %f

ipconfig | grep -E -i "IP Address" | grep -E -o "[0-9][0-9.]+" > afile  
for /f %%f in ('type afile') do set a=%%f  

a neater alternative second line to the 2 line bat file would be

for /f %%f in (afile) do set a=%%f
share|improve this answer
    
The output of ipconfig in Windows 7 is apparently different. You can adapt where it says "IP Address" to "IPv4" or some other string that is unique to the line you want to grab from the ipconfig output. –  barlop Jan 8 '11 at 11:42
add comment

answered my own question...

for /f "tokens=3" %%i in ('ping %computername% -4 -n 1 ^| findstr Reply') do (
    set localipwc=%%i
)

for /f "tokens=1 delims=:" %%j in ("%localipwc%") do (
    set localip=%%j
)

echo "%localip%"

Here's an even better one... note that the -4 in the ping command forces IPv4 on Win7 and is ignored on XP... (the :~11 in the variable name expands beginning at the 11 character in the var)

@echo off
cls
for /f "tokens=1 delims=:" %%j in ('ping %computername% -4 -n 1 ^| findstr Reply') do (
    set localip=%%j
)
echo Your local IP is:"%localip:~11%"

Sample output:

Your local IP is:"192.168.220.133"

share|improve this answer
    
This is the best one as it supports multiple IP addresses –  Anton K Oct 1 '12 at 18:59
add comment

Windows 7 one-liner:

for /F "tokens=14" %i in ('"ipconfig | findstr IPv4"') do SET LOCAL_IP=%i
share|improve this answer
add comment

Based on the previous answers, and noting that the second will not work if you have a local IP not starting from 192, we get:

for /F "tokens=2 delims=:" %i in ('"ipconfig | findstr IPv4"') do SET LOCAL_IP=%i

and if we want to put it to a batch file:

@for /F "tokens=2 delims=:" %%i in ('"ipconfig | findstr IPv4"') do set LOCAL_IP=%%i
@echo Detected: Local IP = [%LOCAL_IP%]

(Example result: Detected: Local IP = [ 10.67.1.205])

but if we need the variable NOT to contain a space before, then we need (win7, if there is a problem play with 13...):

@for /F "tokens=13 delims=\  " %%i in ('"ipconfig | findstr IPv4"') do set LOCAL_IP=%%i
@echo Detected: Local IP = [%LOCAL_IP%]

(Example result: Detected: Local IP = [10.67.1.205])

share|improve this answer
add comment
for /f "tokens=3" %%i in ('ping %computername% -4 -n 1 ^| findstr Reply') do (
    echo %%i
)

Works on both Windows 7 and Windows XP.

share|improve this answer
2  
Please start a new question for your does anyone else know how to get rid of the trailing :?inquiry rather than combining it with the answer. –  jonsca Aug 10 '12 at 22:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.