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
  • 86,445
  • 63
  • 260
  • 306
Ville Koskinen
  • 875
  • 5
  • 10
  • 14
  • *The* LAN address? Be careful with that. – u1686_grawity Jan 07 '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. – u1686_grawity Jan 07 '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 /// does not work. – Ville Koskinen Jan 07 '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. :/ – u1686_grawity Jan 07 '11 at 16:26
  • @grawity: OK, I have edited the question. – Ville Koskinen Jan 07 '11 at 16:41
  • @Ville: Thanks. Could you also tell me what OS the router runs? Is it a standard Linux distro? What I meant by "use hostnames" is something like `wget -q -O - http://villepc:8080/audio` (where instead of `villepc` you would put the actual hostname your computer has). You would need a little configuration on the router, though. – u1686_grawity Jan 07 '11 at 18:25
  • @Ville: Also, you "ping" an address: `ping www.google.com`, `ping 4.2.2.2`, `ping superuser.com`. You _do not_ ping an URL or a path; `ping //hostname/` _will never work_. – u1686_grawity Jan 07 '11 at 18:26
  • @Ville: Alternatively, if the router runs OpenSSH `sshd`, you could use `wget -q -O - "http://${SSH_CONNECTION## *}:8080/audio"`, and the IP address would be filled in automatically by your router. (I'm not sure if this is OpenSSH-specific or if it will work with Dropbear too.) – u1686_grawity Jan 07 '11 at 18:29
  • @Ville: While I'm flooding this page with comments, here's one about your batch file itself. In the last line, you _must_ escape the characters `|` and `&` (change to `^|` and `^&`), because otherwise they will be interpreted by your computer's local shell - it will actually try to run `madplay` in your own computer. (I would also add that `-pw` can be quite insecure, and one should use key-based authentication instead...) – u1686_grawity Jan 07 '11 at 18:32
  • @grawity: Thanks for your help: I wasn't able to get the router to resolve the laptop's hostname (and also got into trouble with plink). Audio is streaming nicely though. – Ville Koskinen Jan 09 '11 at 19:25

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%
  • 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 http://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 07 '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. –  Jan 07 '11 at 23:52
  • Thanks Dan M, this doesn't require additional programs and works on my localized Windows 7 version. – Ville Koskinen Jan 09 '11 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%
Nishi
  • 580
  • 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
  • 23,380
  • 43
  • 145
  • 225
  • 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 08 '11 at 11:42
2

Windows 7 one-liner:

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

Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
CWOrl
  • 21
  • 2
  • 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
-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
  • 153,128
  • 77
  • 353
  • 394
  • 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. – Scott - Слава Україні Mar 09 '17 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 '17 at 16:47