Test if string exists in file with Python - python

I'd like to add only servers currently doesn't exist at file
My current code :
f = open(filename,'a')
for server in cmo.getServers() :
print >>f, server.getListenAddress()
Thanks in advance

try this:
data = set( [i.strip() for i in open( filename, 'r' ).readlines()] )
for server in cmo.getServers() :
data.add( server.getListenAddress() )
open( filename, 'w' ).write('\n'.join(data))

Build a list of servers already present in the file:
present = [l.strip() for l in open(filename)]
(assuming the file format is just one server per line, no other symbols).
Then check if an address is in the list:
for server in cmo.getServers():
address = server.getListenAddress()
if address not in present:
print >>f, address
This assumes that the addresses you get from getServers() will not repeat.
If that's also possible, then build a set of them first:
new = set(server.getListenAddress() for server in cmo.getServers())
for address in new:
if address not in present:
print >>f, address

Related

Python - Readfile and SSH

I currently have this code to read a .CSV file, and SSH into a router with the hostname/IP listed on the .CSV
The problem is that the file reads the line as "192.168.2.1/r/n"
Example: ('Node', 4, '...Checking IP Address...', 'CPE-SIBFOPASO-103179-01\r\n')
Can you please help?
Code Sample:
nodenum=1
f=open('Routers.csv', 'r') #Ficheiro com Hostnames
c=f.readlines()
with open('Output.csv','wb') as f: #Ficheiro novo com o output
write = csv.writer(f)
write.writerow(['Hostname', "Cellular"])
for i in c :
print ("Node", nodenum, "...Checking IP Address...", i)
try:
Connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios" , username="y", password="x", verbose=Fal
se)
Ended up achieving the result I wanted by changing the code to this:
f=open('Routers.csv', 'r')
c=f.read()
file_as_list = c.splitlines()

Python/regex: Change 4th Octet of all IP addresses in a file to .0

I have been battling with this for over a day now; haven't done Python for 3 years now and its showing!
I want the code to work through the file and using substitute I want to change the last octet of each IP address to 0. The re sub line just isn't working and instead of the IP address I get the following in the output file:
<_sre.SRE_Match object at 0x000000000342D718>0
Which is the regex match object plus the 0 that I want as the last octet.
And the code....
import fileinput
import re, sys, os
file1 = 'output.txt'
ipv4Regex = re.compile(r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
def Octet_4_0():
for line in fileinput.input():
line = ipv4Regex.sub(str(re.search(ipv4Regex,line))+'.'.join(ipv4Regex.split('.')[:-1]+["0"]), line.strip())
#print(line)
with open(file1, 'a') as f:
f.write(line+'\n')
fileinput.close()
print "Current working dir : %s" % os.getcwd()
if __name__ == "__main__":
# Delete the previous file
try:
os.remove(file1)
except OSError:
pass
# Run the function
Octet_4_0()
I suggest keeping it simple and just match all IP addresses with the help of a capture group:
ip = 'some text here 127.123.456.789 blah blah'
ip_out = re.sub(r'\b(\d+\.\d+\.\d+)\.\d+\b', r'\1.0', ip)
print(ip_out) # some text here 127.123.456.0 blah blah

Using a txt file to define multiple variables in python

Background Information
I have a program that I'm using for pinging a service and printing the results back to a window. I'm currently trying to add to this program, by adding a kind of 'settings' file that users can edit to change the a) host that is pinged and b) timeout
What I've tried so far
file = open("file.txt", "r")
print (file.read())
settings = file.read()
# looking for the value of 'host'
pattern = 'host = "(.*)'
variable = re.findall(pattern, settings)[0]
print(test)
As for what is contained within the file.txt file:
host = "youtube.com"
pingTimeout = "1"
However, my attempts have been unsuccessful as this comes up with the following
error:
IndexError: list index out of range
And so, my question is:
Can anyone point me in the right direction to do this? To recap, I am asking how I can take an input from file (in this case host = "youtube.com" and save that as a variable 'host' within the python file).
First, as Patrick Haugh pointed out, you can't call read() twice on the same file object. Second, using regex to parse a simple key = value format is a bit overkill.
host, pingTimeout = None,None # Maybe intialize these to a default value
with open("settings.txt", "r") as f:
for line in f:
key,value = line.strip().split(" = ")
if key == 'host':
host = value
if key == 'pingTimeout':
pingTimeout = int(value)
print host, pingTimeout
Note that the expected input format would have no quotes for the example code above.
host = youtube.com
pingTimeout = 1
I tried this, it may help :
import re
filename = "<your text file with hostname>"
with open(filename) as f:
lines = f.read().splitlines()
for str in lines:
if re.search('host', str):
host, val = str.split('=')
val = val.replace("\"", "")
break
host = val
print host
f.close()

program to read a file contents

I have a snort log file named "logs" and want to extract IP addresses from it and store them to another file named "blacklist". it can extract unique IP Addresses but if I run the program again, it adds the previous IPs as well. I want the program to first check whether IP is already in blacklist file? if so, just ignore it otherwise add unique IPs from logs file to blacklist. code:
#!/usr/bin/python
import re
mylist1 = []
mylist2 = []
mylist3 = []
mylist4 = []
logfile = open('/var/log/snort/logs', 'r')
blklist = open('blacklist', 'ab+')
for line in open ('blacklist', 'r').readlines():
mylist4.append(line)
for l in logfile.readlines():
l = l.rstrip()
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}',l)
if ip is not None and ip not in mylist1:
mylist1.append(ip)
for ip in mylist1:
addr = ",".join(ip)
if ',' in addr:
a = addr.split(',')
for ip in a:
addr = "".join(ip)
if addr is not '':
mylist2.append(addr)
else:
mylist3.append(addr)
for x in blklist:
mylist2.append(x.strip())
for x in mylist2:
if x not in mylist3 and x not in mylist4:
blklist.write(x+'\n')
mylist3.append(x)
Logs file is:
12/16-10:34:27.070967 [**] [1:10000001:1] snort alert [1:0000001] [**][classification ID: 0] [Priority ID: 0] {ICMP} 192.168.40.19 -> 192.168.50.29
12/16-10:34:27.070967 [**] [1:10000001:1] snort alert [1:0000001] [**][classification ID: 0] [Priority ID: 0] {ICMP} 192.168.50.29 -> 192.168.30.20
Output of blacklist file after first program run:
192.168.30.20
192.168.50.29
192.168.40.19
Output of blacklist file after second program run:
192.168.30.20
192.168.50.29
192.168.40.19
192.168.30.20
192.168.50.29
192.168.40.19
any help please?
You can read everything in from your blacklist file and log into lists. Join those list and then ouput a set back to the blacklist file (sets are unique values) since the read empties the file your will have a unique list of all new and old IPs. If the order matters (doubt it does) then a set will cause issues. Let me know and I can revamp the below.
if __name__ == '__main__':
import re
blacklist = list(open("blacklist", 'r').read().split('\n'))
logfile = list(open("/var/log/snort/logs", 'r').read().split('\n'))
newentry = []
for entry in logfile:
ips = re.findall( r'[0-9]+(?:\.[0-9]+){3}', entry)
for ip in ips:
newentry.append(ip)
newblacklist = blacklist + newentry
with open("blacklist", 'w+') as f:
f.write('\n' .join(set(newblacklist)))
f.close()
You could utilize the Python container type set which stores only unique elements. The procedure below should work for you:
create a 'current' blacklist set
read the blacklist file IP's into the current set
create a 'delta' blacklist set
for each IP address in the log file
if not already in current blacklist
add the IP into the delta set
append (by writing) the delta set into the black list file

Fetching a IP from text file using python

I am creating custom code for fetching the IP and some other needed details from text file.
Consider the textfile is having following content: tenant_id and IP
cbdf25542c194a069464f69efff4859a 45.45.45.45
cbdf25542c194a069464f69efff4859a 1.6.7.3
cbdf25542c194a069464f69efff4859a 1.7.6.2
1235b3a73ad24b9c86cf301525310b24 2.3.7.5
1235b3a73ad24b9c86cf301525310b24 6.5.2.1
Now I have already created the code for fetching the IP and tenant separately.
Code is as follows:
files = open("/root/flattext", "r")
# create an empty list
ips = []
tenants = []
# read through the files
for text in files.readlines():
# strip off the \n
text = text.rstrip()
# IP and Tenant Fetch
regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$', text)
regex1 = re.findall(r'[0-9A-Za-z]{32}', text)
if regex is not None and regex not in ips:
ips.append(regex)
if regex1 is not None and regex1 not in tenants:
tenants.append(regex1)
ip_valuess = [''.join(ip) for ip in ips if ip]
tenant_ids = [''.join(tenant) for tenant in tenants if tenant]
# cleanup and close files
files.close()
So It will be giving result which consists of IP and Tenant_id as separate list.
Here what I need is fetching the IP which is coming under particular tenant ID.
Consider 1235b3a73ad24b9c86cf301525310b24 as a tenant_id,
So it should be giving result as
2.3.7.5 , 6.5.2.1.
Some one please have a look and give me a better way to sort it out.
Why use regex just split works fine just use defaultdict-
from collections import defaultdict
data = defaultdict(list)
with open(r"D:\ip.txt",'rb') as fl:
for i in fl.readlines():
i=i.strip()
data[i.split(" ")[0]].append(i.split(" ")[1])
print data.items()
Output-
[('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']), ('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])]
If your file is not structured and no space to split with then try regex-
import re
from collections import defaultdict
data = defaultdict(list)
pattern_ip = r'([\d]{1,3}(?=\.|$))'
pattern_tenat = r'^[a-z0-9]{32}'
with open(r"D:\ip.txt",'rb') as fl:
for i in fl.readlines():
i=i.strip()
ip = '.'.join(re.findall(pattern_ip,i))
tent = ''.join(re.findall(pattern_tenat,i))
data[tent].append(ip)
print data.items()
Output-
[('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']), ('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])]
See regex LIVE DEMOTENANT and DEMOIP
Use split and defaultdict:
from collections import defaultdict
results = defaultdict(list)
with open('flattext', 'r') as f:
for row in f.read().strip().split('\n'):
if row.strip() != "":
tenant_id, ip = row.split()
results[tenant_id].append(ip)
print results.get('1235b3a73ad24b9c86cf301525310b24', None)
print results.items()
Output:
['2.3.7.5', '6.5.2.1']
And results content:
[
('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']),
('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])
]

Categories