Regular expression to get IPCONFIG information

Dear all
I want to use ruby regular expression to get ipconfig information from a
string. I wrote some code, but it is not works well. would anybody help
me, thanks!

the coding is like that

$msdos=“ipconfig /all”
str_ipcfg=eval(""+$msdos+"")

str_ipcfg.scan %r{
^ \S [^:]* : \s* $
(?:
^ \s+ [^:]+ : [^:]+ $ [\n\r]+
)*
}x do |adapter|
p adapter

  1. if use $msdos=“ipconfig”, get a string like the following:

“Windows IP Configuration\n\n\nWireless LAN adapter 2:\n\n Media
State . . . . . . . . . . . : Media disconnected\n Connection-specific
DNS Suffix . : \n\n”
“Ethernet adapter Bluetooth :\n\n Media State . . . . . . . . . . . :
Media disconnected\n Connection-specific DNS Suffix . : \n\n”
“Wireless LAN adapter :\n\n Connection-specific DNS Suffix . : \n
IPv4 Address. . . . . . . . . . . : 192.168.1.50\n Subnet Mask . . . .
. . . . . . . : 255.255.255.0\n Default Gateway . . . . . . . . . :
192.168.1.1\n”

Process finished with exit code 0

  1. if use "$msdos=“ipconfig /all”, the string like that:

“Wireless LAN adapter 2:\n\n Media State . . . . . . . . . . . :
Media disconnected\n Connection-specific DNS Suffix . : \n
Description . . . . . . . . . . . : Microsoft Virtual WiFi Miniport
Adapter\n Physical Address. . . . . . . . . : 16-2F-68-CB-81-37\n
DHCP Enabled. . . . . . . . . . . : Yes\n Autoconfiguration Enabled .
. . . : Yes\n\n”
“Ethernet adapter Bluetooth :\n\n Media State . . . . . . . . . . . :
Media disconnected\n Connection-specific DNS Suffix . : \n
Description . . . . . . . . . . . : Bluetooth Device (Personal Area
Network)\n Physical Address. . . . . . . . . : 74-2F-68-CC-41-BF\n
DHCP Enabled. . . . . . . . . . . : Yes\n Autoconfiguration Enabled .
. . . : Yes\n\n”
“Wireless LAN adapter :\n\n Connection-specific DNS Suffix . : \n
Description . . . . . . . . . . . : Atheros AR9485WB-EG Wireless Network
Adapter\n Physical Address. . . . . . . . . : 74-2F-68-CB-81-37\n
DHCP Enabled. . . . . . . . . . . : Yes\n Autoconfiguration Enabled .
. . . : Yes\n IPv4 Address. . . . . . . . . . . :
192.168.1.50(Preferred) \n Subnet Mask . . . . . . . . . . . :
255.255.255.0\n”

my question is : where is wrong in my 2ed regular express, causing the
string is not complete, i could not get the whole ipconfig information?
because the gateway, dhcp, dns information is after the subnet mask
information.

many thanks!
Have a nice day.
Edward

Hi,

Edward QU wrote in post #1064855:

my question is : where is wrong in my 2ed regular express, causing the
string is not complete, i could not get the whole ipconfig information?
because the gateway, dhcp, dns information is after the subnet mask
information.

Well, since you didn’t give us the actual output of “ipconfig /all”, we
can only guess. It’s probably a value with colons (like a date or an
IPv6 address) causing the trouble. The parser will stop at this line,
because you don’t allow colons in values.

I’m not exactly sure what you want to do, anyway. If you only want to
get the “blocks”, you should split the output at the headings:

blocks = str_ipcfg.split /(?<=\n\n)(?=\S)/
blocks.each do |block|
puts block, ‘---------- end of block ----------’
end

You may then read the lines and split them at the colon to get the
values.

Hello Jan E.
good idea, it is easy to get information in each block, thanks!
regards
Edward