RegEx for DNS Servers via IPCONFIG - regex

Stack Overflow RegEx Wizards, I've scoured Google and haven't quite found a good solution for this. I need to pull out 1:N DNS servers from IPCONFIG results. In the example below, I would need the first three. However, there may be an instance where there are more or less.
Update: Optimally we want to place cursor at first colon(:) in the DNS string then capture IPs until we hit an alpha character. So if we can just scrape a string from that colon to that alpha character we can run another RegEx to match IPs.
DNS.*: gets us to the first colon (:)
Need to read-ahead until alpha character.
Important Note: Because of the third-party tool we're using we can only use RegEx :)
Here's the RegEx value I've been using as for IPs. This will capture all IP's instead of just the DNS ones...
(([0-9]){1,3}.){1,3}[0-9]{1,3}
IPCONFIG Example
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 152.225.244.1
DHCP Server . . . . . . . . . . . : 10.204.40.57
DNS Servers . . . . . . . . . . . : 10.204.127.11
10.207.2.50
10.200.10.6
Primary WINS Server . . . . . . . : 10.207.40.145
Secondary WINS Server . . . . . . : 10.232.40.38
Lease Obtained. . . . . . . . . . : Tuesday, August 28, 2012 6:45:12 AM
Lease Expires . . . . . . . . . . : Sunday, September 02, 2012 6:45:12 A

#!/usr/bin/env perl
use strict;
use warnings;
my $data = <<END;
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 152.225.244.1
DHCP Server . . . . . . . . . . . : 10.204.40.57
DNS Servers . . . . . . . . . . . : 10.204.127.11
10.207.2.50
10.200.10.6
Primary WINS Server . . . . . . . : 10.207.40.145
Secondary WINS Server . . . . . . : 10.232.40.38
Lease Obtained. . . . . . . . . . : Tuesday, August 28, 2012 6:45:12 AM
Lease Expires . . . . . . . . . . : Sunday, September 02, 2012 6:45:12 A
END
my #ips = ();
if ($data =~ /^DNS Servers[\s\.:]+((\d{2}\.\d{3}\.\d{1,3}\.\d{1,3}\s*)+)/m) {
#ips = split(/\s+/, $1);
print "$_\n" foreach(#ips);
}

I would use unpack instead of regular expressions for parsing column-based data:
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
my ($ip) = unpack 'x36 A*';
print "$ip\n";
}
__DATA__
DNS Servers . . . . . . . . . . . : 10.204.127.11
10.207.2.50
10.200.10.6
Primary WINS Server . . . . . . . : 10.207.40.145
Secondary WINS Server . . . . . . : 10.232.40.38
You may have to adjust the number 36 to the actual number of characters that should be skipped.

Personally, I'd go in a different direction. Instead of manually parsing the output of ipconfig, I'd use the Win32::IPConfig module.
Win32::IPConfig - IP Configuration Settings for Windows NT/2000/XP/2003
use Win32::IPConfig;
use Data::Dumper;
my $host = shift || "127.0.0.1";
my $ipconfig = Win32::IPConfig->new($host);
my #searchlist = $ipconfig->get_searchlist;
print Dumper \#searchlist;

Match
DNS.+?:(\s*([\d.]+).)+
and pull out the groups. This assumes you have the entire multi-line string in one blob, ans that the extracted text may contain newlines and other whitespace.
The last dot is to match the newline, you need to use /m option

Match against this regex (see in action):
DNS Servers.*:\s*(.*(?:[\n\r]+\s+.*(?:[\n\r]+\s+.*)?)?)
First capture group will be your three IP's (atmost three) as you requested. You need to trim whitespaces surely.
Edit: Regex fixed to match at most three IP's. If there is less IP's, matches them only.

Related

how to grep output from windows command line

I use windows command prompt cmd in admin mode for various task like finding ip address interface names etc. When I issue some command like
ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . :
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : domain.name
Ethernet adapter Ethernet 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : domain.name
Description . . . . . . . . . . . : Realtek PCIe GbE Family Controller #2
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . :
IPv4 Address. . . . . . . . . . . : 192.168.56.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : 10 December 2019 22:10:04
Lease Expires . . . . . . . . . . : 11 December 2019 03:10:04
Default Gateway . . . . . . . . . :
DHCP Server . . . . . . . . . . . : 192.168.56.254
DHCPv6 IAID . . . . . . . . . . . : 33574998
DHCPv6 Client DUID. . . . . . . . :
DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter VMware Network Adapter VMnet8:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . :
IPv4 Address. . . . . . . . . . . : 192.168.136.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : 10 December 2019 22:10:04
Lease Expires . . . . . . . . . . : 11 December 2019 03:10:04
Default Gateway . . . . . . . . . :
DHCP Server . . . . . . . . . . . : 192.168.136.254
DHCPv6 IAID . . . . . . . . . . . : 268456022
DHCPv6 Client DUID. . . . . . . . :
DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
Primary WINS Server . . . . . . . : 192.168.136.2
NetBIOS over Tcpip. . . . . . . . : Enabled
Wireless LAN adapter Wi-Fi 2:
Connection-specific DNS Suffix . : domain.name
Description . . . . . . . . . . . : D-Link DWA-131 Wireless N Nano USB Adapter(rev.E)
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . :
IPv4 Address. . . . . . . . . . . : 192.168.1.14(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : 10 December 2019 22:10:09
Lease Expires . . . . . . . . . . : 11 December 2019 22:10:12
Default Gateway . . . . . . . . . :
192.168.1.1
DHCP Server . . . . . . . . . . . : 192.168.1.1
DHCPv6 IAID . . . . . . . . . . . : 353154770
DHCPv6 Client DUID. . . . . . . . :
DNS Servers . . . . . . . . . . . : 109.169.85.7
8.8.8.8
NetBIOS over Tcpip. . . . . . . . : Enabled
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : domain.name
Description . . . . . . . . . . . : Realtek RTL8723DE 802.11b/g/n PCIe Adapter
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
I get a lot of output on my screen.If I want only those lines which have physical addeess in
text output or say line that have DNS Servers only in ouput. Then is there an easy way to do
a grep on these in outout in Windows 10 cmd.
Or suppose in above output I want to see only output for section Vmnet8
then how can I grep it?
I want only those lines which have physical addeess
ipconfig|findstr /r ":: [0-9]\."
filters for lines that contain :: (for IPv6 addresses) or <number><dot> (for IPv4 addresses)
(as you mentioned being a "Linux-Guy" - don't expect too much from findstr REGEX. It's just a small crippled subset of real REGEX - see findstr /?)
Download the free MOBAXTERM then navigate through your windows OS same as if you are using Linux. Then you will be able to use grep etc.
With the introduction of Windows Subsystem for Linux (WSL), you can use grep directly on ipconfig.
** I have limited knowledge on grep. Here is a basic example usage:
On the classic Windows Command Prompt, run:
C:\> ipconfig /all | wsl grep 'IPv4'
On a Linux Terminal (e.g. Ubuntu on WSL), run (notice the .exe extension):
$ ipconfig.exe /all | grep 'IPv4'
Sample output
IPv4 Address. . . . . . . . . . . : 192.198.100.1(Preferred)
IPv4 Address. . . . . . . . . . . : 192.198.15.9(Preferred)
IPv4 Address. . . . . . . . . . . : 192.198.89.1(Preferred)
...

Remove middle character from variable names

I have variable names ending with an underscore (_), followed by a year code:
clear
set obs 1
foreach var in age_58 age_64 age_75 age_184 age_93 age99 {
generate `var' = rnormal()
}
list
+----------------------------------------------------------------------+
| age_58 age_64 age_75 age_184 age_93 age99 |
|----------------------------------------------------------------------|
1. | .1162236 -.8781271 1.199268 -1.475732 .9077238 -.0858719 |
+----------------------------------------------------------------------+
I would like to rename them into:
age58 age64 age75 age184 age93 age99
I know I can do this by renaming one variable at a time as follows:
rename age_58 age58
rename age_64 age64
rename age_75 age75
rename age_184 age184
rename age_93 age93
How can I remove the underscore from all the variable names at once?
In Stata 13 and later versions, this can be done in one line using the built-in command rename.
One merely has to specify the relevant rules, which can include wildcard characters:
rename *_# *#
list
+----------------------------------------------------------------------+
| age58 age64 age75 age184 age93 age99 |
|----------------------------------------------------------------------|
1. | .1162236 -.8781271 1.199268 -1.475732 .9077238 -.0858719 |
+----------------------------------------------------------------------+
Type help rename group for details on the various available specifiers.
For Stata 8 up, the community-contributed command renvars offers a solution:
renvars age_*, subst(_)
For documentation and download, see
. search renvars, historical
Search of official help files, FAQs, Examples, SJs, and STBs
SJ-5-4 dm88_1 . . . . . . . . . . . . . . . . . Software update for renvars
(help renvars if installed) . . . . . . . . . N. J. Cox and J. Weesie
Q4/05 SJ 5(4):607
trimend() option added and help file updated
STB-60 dm88 . . . . . . . . Renaming variables, multiply and systematically
(help renvars if installed) . . . . . . . . . N. J. Cox and J. Weesie
3/01 pp.4--6; STB Reprints Vol 10, pp.41--44
renames variables by changing prefixes, postfixes, substrings,
or as specified by a user supplied rule
For the 2001 paper, see this .pdf file.
You can loop over the variables using the macro extended function subinstr:
foreach var of varlist * {
local newname : subinstr local var "_" "", all
if "`newname'" != "`var'" {
rename `var' `newname'
}
}

Copying Specific Lines To a .txt File

OK, I am using 'ipconfig /displaydns' to display all websites visited (since the last 'ipconfig /flushdns') and I would like to copy just the website's URL to Websites.txt.
A typical layout of the output is:
ocsp.digicert.com
----------------------------------------
Record Name . . . . . : ocsp.digicert.com
Record Type . . . . . : 5
Time To Live . . . . : 17913
Data Length . . . . . : 4
Section . . . . . . . : Answer
CNAME Record . . . . : cs9.wac.edgecastcdn.net
badge.stumbleupon.com
----------------------------------------
Record Name . . . . . : badge.stumbleupon.com
Record Type . . . . . : 1
Time To Live . . . . : 39560
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 199.30.80.32
0.gravatar.com
----------------------------------------
Record Name . . . . . : 0.gravatar.com
Record Type . . . . . : 5
Time To Live . . . . : 2047
Data Length . . . . . : 4
Section . . . . . . . : Answer
CNAME Record . . . . : cs91.wac.edgecastcdn.net
But, I would wish just to have
ocsp.digicert.com
badge.stumbleupon.com
0.gravatar.com
as the output. Any ideas on how to do that, also I am using a Windows RT device, so external applications are not an option and the output is usually 10 times longer than that, and not all records are the same.
Use PowerShell:
ipconfig /displaydns | Select-String 'Record Name' | ForEach-Object {$_ -replace "Record Name . . . . . :", ""}

C++ 2d array when subtracting 1 from col in position 0 it goes back a row to last spot in col

I'm making a reversi game in c++ and whenever I use a 10 by 10 array and search within the array to switch letters the game likes to switch up my rows and columns. This output would cause the o to switch to an x.
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . x o
x . . . . . . . . .

Extract IP address from ipconfig using Regex

I would like to extract the IPv4 address only from the output below, after running ipconfig, using Regex.
Connection-specific DNS Suffix . : domain.local
Link-local IPv6 Address . . . . . : fe80::49b6:17f6:f059:1c3d%12
IPv4 Address. . . . . . . . . . . : 192.168.2.180
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.2.254
Therefore I would like to extract the '192.168.2.180' address only.
^ *IPv4 Address[.\s]*:\s*([\d.]+)\s*$
It does not do any sanity checks on the IP address.
There are better ways to parse IP addresses, but this one is simple and grabs only what you need:
http://rubular.com/r/PxEX4Lk56w
\s+IPv4 Address.*: ([\d\.]+)