6

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 [email protected] -pw password killall -9 madplay
plink -ssh [email protected] -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?

10
  • The LAN address? Be careful with that. Jan 7, 2011 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. Jan 7, 2011 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. Jan 7, 2011 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. :/ Jan 7, 2011 at 16:26
  • @grawity: OK, I have edited the question. Jan 7, 2011 at 16:41

8 Answers 8

4

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.


Or you can try this and not use temporary files:

for /f "tokens=14" %%a in ('ipconfig ^| findstr IPv4') do set _IPaddr=%%a
echo IP is: %_IPaddr%
3
  • 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, 2011 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.
    – user61724
    Jan 7, 2011 at 23:52
  • Thanks Dan M, this doesn't require additional programs and works on my localized Windows 7 version. Jan 9, 2011 at 19:11
4

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%
2

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
1
  • 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, 2011 at 11:42
2

Windows 7 one-liner:

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

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"

1
  • This is the best one as it supports multiple IP addresses
    – Anton K
    Oct 1, 2012 at 18:59
0

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])

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

Works on both Windows 7 and Windows XP.

1
  • 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, 2012 at 22:47
-1
@echo off
net use J: /d /y
rem Aqui se colocan manualmente ip
set matriz=172.25.103.*
set uno=172.25.20.*
set sur=172.25.18.*
set alamor=172.25.4.*
set autobanco=172.25.19.*
set cariamanga=172.25.7.*
set catacocha=172.25.3.*
set catamayo=172.25.2.*
set pangui=172.25.44.*
set macara=172.25.5.*
set hipervalle=172.25.35.*
set gualaquiza=172.25.21.*
set quito=172.25.10.*
set yanzatza=172.25.9.*
set zamora=172.25.8.*
set recreo=172.25.38.*
set machala=172.25.103.196
rem Estas dos líneas extraen el ip real del equipo a una variable
for /f "tokens=1,2* delims=:" %%i in (' ipconfig ^| find "IPv4" ') do set ip=%%j
set ip=%ip:~1%
echo %ip%
pause
rem Compara la dirección IP si esta ok va a Formas Agencias sino sale a ERROR1
if /I "%matriz%=="%ip%" goto matriz1 else
if /I "%uno%"=="%ip%" goto uno1 else
if /I "%sur%"=="%ip%" goto sur1 else
if /I "%alamor%"=="%ip%" goto alamor1 else
if /I "%autobanco%"=="%ip%" goto autobanco1 else
if /I "%cariamanga%"=="%ip%" goto cariamanga1 else
if /I "%catacocha%"=="%ip%" goto catacocha1 else
if /I "%catamayo%"=="%ip%" goto catamayo1 else
if /I "%pangui%"=="%ip%" goto pangui1 else
if /I "%macara%"=="%ip%" goto macara1 else
if /I "%hipervalle%"=="%ip%" goto hipervalle1 else
if /I "%gualaquiza%"=="%ip%" goto gualaquiza1 else
if /I "%quito%"=="%ip%" goto quito1 else
if /I "%yanzatza%"=="%ip%" goto yanzatza1 else
if /I "%zamora%"=="%ip%" goto zamora1 else
if /I "%recreo%"=="%ip%" goto recreo1 else
if /I "%machala%"=="%ip%" goto machala1 else goto error1
rem Mapeo de Unidad de red hacia Matriz
:matriz1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Agencia Uno
:uno1
IF EXIST S:\fgen1001.fmx goto fin
net use S: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Agencia Sur
:sur1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Alamor
:alamor1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Autobanco
:autobanco1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Cariamanga
:cariamanga1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Catacocha
:catacocha1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Catamayo
:catamayo1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Pangui
:pangui1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Macara
:macara1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Hipervalle
:hipervalle1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Gualaquiza
:gualaquiza1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Quito
:quito1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Yanzatza
:yanzatza1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Zamora
:zamora1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Recreo
:recreo1
IF EXIST J:\fgen1001.fmx goto fin
net use J: \\172.25.0.35\Forms
exit
rem Mapeo de Unidad de red hacia Machala
:machala1
IF EXIST M:\fgen1001.fmx goto fin
net use M: \\172.25.0.35\Forms
exit
:error1
echo IP INCORRECTA
pause
exit
:fin
exit
2
  • 1
    Can you describe what this is doing?  It seems to be very tailored to a specific environment, with a bunch of IP addresses hard-coded in.  How would somebody else adapt this to their site?  Please do not respond in comments;  edit your answer to make it clearer and more complete — and use English. Mar 9, 2017 at 18:24
  • The answer is buried somewhere in this long and over complicated batch file. Please provide a minimal answer with an explanation in English.
    – DavidPostill
    Mar 10, 2017 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .