I've tried hard to figure out on how to append items in one list into a new list. The data in the list is actually information from ipconfig/all. Because I wanted to separate the items in sections based on the network titles, I added the titles into another list to be used as reference. Here is my code:
listofstates = []
allconnections = []
leftconnections = []
mainlist = []
commands = "ipconfig/all"
pipe = Popen(commands, shell=True, stdout=PIPE)
for line in pipe.stdout:
listofstates.append(line.strip())
for items in listofstates:
splittedvalues = str(items).split(':')
if "b''" not in splittedvalues:
splittedvalues = [s for s in splittedvalues if s]
element = [splittedvalues[0].replace("b'", "").replace(".", "")] + splittedvalues[1:]
cleanedelement = (re.split(r'\s{2,}',str(element[0])) + element[1:])
cleanedelement = [s for s in cleanedelement if s]
allconnections.append(cleanedelement)
leftvalues = str(splittedvalues[0])
if "." not in leftvalues:
wordcount = len(re.findall(r'\w+', leftvalues))
if wordcount > 2:
newvalues = leftvalues.replace("b'", "").replace("'", "")
leftconnections.append(newvalues)
print (allconnections)
print (leftconnections)
Output for allconnections:
[["Windows IP Configuration'"], ['Host Name', " Bla Bla Bla'"], ['Primary Dns Suffix', " vitrox.local'"], ['Node Type', " Hybrid'"], ['IP Routing Enabled', " No'"], ['WINS Proxy Enabled', " No'"], ['DNS Suffix Search List', " black sheep'"], ['Ethernet adapter Ethernet', "'"], ['Media State', " Media disconnected'"], ['Connection-specific DNS Suffix', "'"], ['Description', " Intel(R) Ethernet Connection I-LM'"], ['Physical Address', " 00-B5-00-1E-F4-5G'"], ['DHCP Enabled', " Yes'"], ['Autoconfiguration Enabled', " Yes'"], ['Ethernet adapter Local Area Network 5', "'"], ['Connection-specific DNS Suffix', "'"], ['Description', " VirtualBox Host-Only Ethernet Adapter'"], ['Physical Address', " 0A-00-50-00-11-0B'"], ['DHCP Enabled', " No'"], ['Autoconfiguration Enabled', " Yes'"], ['Link-local IPv6 Address', ' fe69', 'aa2b', '4b5d', '2345', "5f07%08(Preferred)'"], ['IPv4 Address', " 10.0.0.05(Preferred)'"], ['Subnet Mask', " 255.255.255.0'"], ['Default Gateway', "'"], ['DHCPv6 IAID', " 539312188'"], ['DHCPv6 Client DUID', " 00-04-11-01-25-75-14-A4-54-B1-03-1E-F4-5E'"], ['DNS Servers', ' fec0', '0', '0', 'ffff', "1%1'"], ['fec0', '0', '0', 'ffff', "2%1'"], ['fec0', '0', '0', 'ffff', "3%1'"], ['NetBIOS over Tcpip', " Enabled'"]..............]
The above is basically the list I'm extracting the data from.
Output for leftconnections:
['Windows IP Configuration', 'Ethernet adapter Ethernet', 'Ethernet adapter Local Area Network 5', 'Wireless LAN adapter Local Area Connection* 3', 'Wireless LAN adapter Local Area Connection* 12', 'Wireless LAN adapter Wi-Fi', 'Ethernet adapter Bluetooth Network Connection']
Here is the list to use as reference.
Ultimately, I want to have an output like this by separating data in all connections by the titles in leftconnections.
[[["Windows IP Configuration'"], ['Host Name', " Bla Bla Bla'"], ['Primary Dns Suffix', " vitrox.local'"], ['Node Type', " Hybrid'"], ['IP Routing Enabled', " No'"], ['WINS Proxy Enabled', " No'"], ['DNS Suffix Search List', " black sheep'"]],[['Ethernet adapter Ethernet', "'"], ['Media State', " Media disconnected'"], ['Connection-specific DNS Suffix', "'"], ['Description', " Intel(R) Ethernet Connection I-LM'"], ['Physical Address', " 00-B5-00-1E-F4-5G'"], ['DHCP Enabled', " Yes'"], ['Autoconfiguration Enabled', " Yes'"], ['Ethernet adapter Local Area Network 5', "'"], ['Connection-specific DNS Suffix', "'"], ['Description', " VirtualBox Host-Only Ethernet Adapter'"], ['Physical Address', " 0A-00-50-00-11-0B'"], ['DHCP Enabled', " No'"], ['Autoconfiguration Enabled', " Yes'"], ['Link-local IPv6 Address', ' fe69', 'aa2b', '4b5d', '2345', "5f07%08(Preferred)'"], ['IPv4 Address', " 10.0.0.05(Preferred)'"], ['Subnet Mask', " 255.255.255.0'"], ['Default Gateway', "'"], ['DHCPv6 IAID', " 539312188'"], ['DHCPv6 Client DUID', " 00-04-11-01-25-75-14-A4-54-B1-03-1E-F4-5E'"], ['DNS Servers', ' fec0', '0', '0', 'ffff', "1%1'"], ['fec0', '0', '0', 'ffff', "2%1'"], ['fec0', '0', '0', 'ffff', "3%1'"], ['NetBIOS over Tcpip', " Enabled'"]],[[...],[...],[...],[[...],[...]]]
What I could think of, was to make this for loop (not a working code):
for connection in leftconnections:
if (*connection is the first*)
mainlist.append(leftconnections) *until second connection is found*
The main purpose of the code is just to separate all the details in ipconfig/all by network. So, I'm actually open to other methods because I know, my code is a little messy right now.
Thank you so much if you are willing to help me out.