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 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?

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311

8 Answers8

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%
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%
Nishi
  • 578
  • 3
  • 9
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
barlop
  • 25,198
2

Windows 7 one-liner:

for /F "tokens=14" %i in ('"ipconfig | findstr IPv4"') do SET LOCAL_IP=%i
Fordi
  • 121
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"

CWOrl
  • 21
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])

ntg
  • 230
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.

CWOrl
  • 21
-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
DavidPostill
  • 162,382