Brando569 Posted December 13, 2012 Posted December 13, 2012 (edited) I'm on a computer running Windows XP with PowerShell installed and I would like to be able to grab the IP address from the output of ipconfig, then place it into a variable that will be used as an argument when executing a program. I use Linux on my home computers, so I would know how to do it with shell scripting, but PowerShell is completely different (it reminds me of Java).I've gotten as far as storing the output of ipconfig in a variable but I don't know how to "grep" it out of all the ouput since I'm not good with RegEx and then store that string into a variable which will be called upon later.C:\Documents and Settings\bran>ipconfigWindows IP ConfigurationEthernet adapter Local Area Connection: Connection-specific DNS Suffix . : domain.net IP Address. . . . . . . . . . . . : 10.6.16.2 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 10.6.16.1Ethernet adapter Local Area Connection 2: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 192.168.42.24 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.42.129The IP address that I would like to be able to grab is that of Ethernet adapter Local Area Connection 2, the first three octets are always the same (192.168.42) it is only the last octet that changes.So in short I would like something along the lines of this:$ip=ipconfigecho $ip [grab IP address and store it in a variable]forcebindip $ipaddress c:\program.exeor for those of you that understand shell scriptingipconfig | grep "192.168.42"| head -n1| cut -c 30 > $ipforcebindip $ip c:\program.exe Edited December 13, 2012 by Brando569
jaclaz Posted December 13, 2012 Posted December 13, 2012 NOT what you asked for, but in batch:http://reboot.pro/topic/5881-ipconfig/the code is "botched" you may want to use this:http://pastehtml.com/view/b4t99xk89.htmlBasically it amounts to:@ECHO OFFfor /f "tokens=2 delims=:" %%A in ('IPCONFIG^|FIND "IP"') do echo %%Ajaclaz
allen2 Posted December 13, 2012 Posted December 13, 2012 Taken from there:((ipconfig | findstr [0-9].\.)[0]).Split()[-1]It only retrieve the first ip address if you have more than one network adapter.
Yzöwl Posted December 13, 2012 Posted December 13, 2012 Just a quick follow up batch file using ideas from both jaclaz and allen2:@ECHO OFF & SETLOCAL ENABLEEXTENSIONSREM Change the line below to reflect your known IP stringSET KIP=192.168.42FOR /F "TOKENS=2 DELIMS=:" %%# IN ('IPCONFIG^|FINDSTR/I IP.*%KIP:.=\.%\.') DO ( CALL :SUB %%#)PAUSEGOTO :EOF:SUBECHO=%*The SUB currently only shows you the relevant IP Address, you would obviously replace that with the code you wish to run, (and when you're happy REMove the PAUSE).
cluberti Posted December 13, 2012 Posted December 13, 2012 I don't know if this works on XP or not as I don't have a box at the moment to check, but if XP has the "Get-NetIPAddress" cmdlet, you could start with this and go from there:Get-NetIPAddress | Sort-Object -Property InterfaceIndex | Format-Table
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now