Python: Efficient way to compare ip addresses in a csv file -
i have csv file has list of 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 compare last octets in ssh ip, nfs ip , iscsi ip , if match, want execute few commands, ssh'ing box.
i want know efficient way compare last octets, considering case. around highly appreciated.
p.s. not have problems in ssh'ing box, planning use paramiko library.
i'd use re
, , here 1 example.
code:
import re regex = re.compile('([^\.]+\.){3}(?p<ssh>[^\s\n\.]+)(\s+([^\.]+\.){3}(?p=ssh)){2}\n') open(file) ff: line in ff: m = regex.search(line) if m: # 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
Comments
Post a Comment