source: main/trunk/greenstone3/bin/script/IPv4.bat

Last change on this file was 32463, checked in by ak19, 6 years ago

Need to get machine's external IP address from eth0 device, not the first device that ifconfig returns

  • Property svn:executable set to *
File size: 1.8 KB
Line 
1@echo off
2
3rem This script will echo the IPv4 of this windows machine. E.g. 100.200.300.45
4rem If passed -format-for-tomcat-context, it will echo the same with a pipe
5rem symbol up front and all the . escaped with a "\". E.g. "|100\.200\.300\.45"
6rem The 3 spaces in front of the IPv4 match expression ensure we grab the line
7rem that starts with "IPv4 Address", not the many others containing "IPv4 Address".
8rem Without the spaces, it will grab the last line mentioning "IPv4" (ethernet).
9
10for /f "usebackq tokens=2 delims=:" %%a in (`ipconfig ^| find " IPv4"`) do (set result=%%a)
11
12:: To lop off the single extra space in front of IPv4 number, end with:
13::do (set result=%a& echo %result:~1%)
14:: Alternative: for /f "tokens=2 delims=:" %a in ('ipconfig ^| findstr /C:"IPv4 Address"') do echo %a
15:: still need to remove space up front for that too.
16
17:: removes extra space in front
18set result=%result:~1%
19
20:: The above produces the IPv4 number, e.g. 100.200.300.45
21
22
23rem Check if we're requested to format the IPv4 for the solr.xml tomcat context file
24if [%1]==[] goto done
25if not "%1"=="-format-for-tomcat-context" goto done
26
27:: Still need to replace . with \.
28:: http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
29:: Syntax: %string:SEARCH=REPLACE%
30set result=%result:.=\.%
31
32:: http://stackoverflow.com/questions/2541767/what-is-the-proper-way-to-test-if-variable-is-empty-in-a-batch-file-if-not-1
33:: Make sure it is not the empty string or a string of spaces
34:: Test by setting result to space
35::set result=
36set result=%result: =%
37
38::if not "%result%" == ""
39::set result=^|%result%
40
41:: Echo a vertical bar up front, regardless of whether the result is empty
42echo ^|%result%
43goto fin
44
45
46:done
47echo %result%
48
49:fin
Note: See TracBrowser for help on using the repository browser.