Python execfile print none, not print value from variable - python

I have file mikrotik.py
'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)
and this some code
import os
localfilepath = os.getcwd()
staticDir = localfilepath+"/website/plugin/config/routing/static/"
vendors = staticDir+"MIKROTIK.py"
destination = 192.168.2.0
prefix = 24
gateway = 192.168.1.1
x = execfile(vendors)
print x
the result is
None
I want the result is
ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1
When I use
x = open(vendors)
print x
the result is
'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)
Thanks in advance
so finally i use eval(x)
and the result is ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1, i dont know its the best way or no

If I understand correctly, you only need simple string manipulation:
import os
destination = 192.168.2.0
prefix = 24
gateway = 192.168.1.1
vendors = 'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)
print x
If you really want to put the string/format in a separate file, use text file, json file etc, but no .py file.
BTW, identification is important in python.

Related

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

Print Header Information for Human Readability in Python

I learned and code below which gives the Hostnames and Its IP address by the reading the hostname from the "mylabList.txt" file, Now i am looking the way to print the output in a pretty Human readable from Like Column header at the top of each and then the name (Below that)..
Is there way to set width between columns while printing...
#!/usr/bin/python
import sys
import socket
with open("mylabList.txt", 'r') as f:
for host in f:
print("{0[0]}\t{0[2][0]}".format(socket.gethostbyname_ex(host.rstrip())))
Current output is Like:
mylab1.example.com 172.10.1.1
mylab2.example.com 172.10.1.2
mylab3.example.com 172.10.1.3
mylab4.example.com 122.10.1.4
Expected Output is:
Server Name IP ADDRESS
===================================
mylab1.example.com 172.10.1.1
mylab2.example.com 172.10.1.2
mylab3.example.com 172.10.1.3
mylab4.example.com 122.10.1.4
Just a note.. in my output Srever Name's lenghth is upto 30 Char long.
You could use ljust and rjust
http://www.tutorialspoint.com/python/string_ljust.htm
http://www.tutorialspoint.com/python/string_rjust.htm
print("A String".ljust(30, " ") + "Another String")
results in
A String Another String
This is a possible way to do the trick:
#!/usr/bin/python
import sys
import socket
print("Server Name".ljust(30, " ") + "IP ADRESS")
print("="*39)
with open("mylabList.txt", 'r') as f:
for host in f:
print("{0[0]}\t{0[2][0]}".format(socket.gethostbyname_ex(host.rstrip())))

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'])
]

Test if string exists in file with 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

Categories