问题
I have a csv file which has the list of the following ip addresses:
SSH IP NFS IP iSCSI IP
10.xxx.xxx.aaa 172.xxx.xxx.aaa 172.yyy.xxx.aaa
10.xxx.xxx.bbb 172.xxx.xxx.bbb 172.yyy.xxx.bbb
10.xxx.xxx.ccc 172.xxx.xxx.ccc 172.yyy.xxx.ccc
10.xxx.xxx.ddd 172.xxx.xxx.ddd 172.yyy.xxx.ddd
... ... ...
... ... ...
I want to compare the last octets in SSH IP, NFS IP and iSCSI IP and if they match, i want to execute a few commands, by ssh'ing into the box.
I want to know the most efficient way to compare the last octets, considering my case. Any help around this would be highly appreciated.
P.S. I do not have any problems in ssh'ing into the box, i am planning to use the paramiko library.
回答1:
I'd like to use re
, and here is one example.
code:
import re
regex = re.compile('([^\.]+\.){3}(?P<ssh>[^\s\n\.]+)(\s+([^\.]+\.){3}(?P=ssh)){2}\n')
with open(file) as ff:
for line in ff:
m = regex.search(line)
if m:
# do what you want
print m.group()
file content:
SSH IP NFS IP iSCSI IP
10.xxx.xxx.aaa 172.xxx.xxx.aaa 172.yyy.xxx.aaa
10.xxx.xxx.bbb 172.xxx.xxx.bbb 172.yyy.xxx.bbb
10.xxx.xxx.ccc 172.xxx.xxx.ccc 172.yyy.xxx.ccc
10.xxx.xxx.ddd 172.xxx.xxx.ddd 172.yyy.xxx.ddd
10.xxx.xxx.eee 172.xxx.xxx.eee 172.yyy.xxx.eeef
10.xxx.xxx.fff 172.xxx.xxx.fff 172.yyy.xxx.ffg
ouput:
10.xxx.xxx.aaa 172.xxx.xxx.aaa 172.yyy.xxx.aaa
10.xxx.xxx.bbb 172.xxx.xxx.bbb 172.yyy.xxx.bbb
10.xxx.xxx.ccc 172.xxx.xxx.ccc 172.yyy.xxx.ccc
10.xxx.xxx.ddd 172.xxx.xxx.ddd 172.yyy.xxx.ddd
回答2:
One simple way would be to use the Python CSV library to import the data a row at a time. Then to use a list comprehension to split the IP address into components, taking the last one and adding it to a set. If the set results in a length of 1, you know all columns are the same.
I am assuming the above data shows the CSV file, and is tab delimited:
import csv
reader = csv.reader(open(r"addresses.csv", "rb"), delimiter="\t")
for lCols in reader:
try:
if len(set([szIP.split(".")[3] for szIP in lCols])) == 1:
print "\t".join(lCols)
except:
pass # Skip non IP address formatted rows
If you are looking for the fastest solution, I strongly suggest you time any regular expression solution against this code to determine which is the best, as it not always obvious which would be best.
If your CSV file is in a different format, it would simply need an additional list comprehension step to convert it to how the data is shown above.
来源:https://stackoverflow.com/questions/30838776/python-efficient-way-to-compare-ip-addresses-in-a-csv-file