I have this script below how check the countries of domaine names. and i want to put each country code in a specific file.
For example :
FR ==> FR.txt
US ==> US.TXT
The script output is like this :
domain.com [xx.xx.xx.xx]: US
Script:
#!/usr/bin/python
import socket
from geolite2 import geolite2
def origin(ip, domain_str, result):
print("{0} [{1}]: {2}".format(domain_str.strip(), ip, result))
def getip(domain_str):
ip = socket.gethostbyname(domain_str.strip())
reader = geolite2.reader()
output = reader.get(ip)
result = output['country']['iso_code']
origin(ip, domain_str, result)
with open("hostnames.txt", "r") as ins:
for domain_str in ins:
try:
getip(domain_str)
except socket.error as msg:
print("{0} [could not resolve]".format(domain_str.strip()))
if len(domain_str) > 2:
subdomain = domain_str.split('.', 1)[1]
try:
getip(subdomain)
except:
continue
geolite2.close()
I appreciate any help provided
def origin(ip, domain_str, result):
file_name = "{}.txt".format(result)
with open(file_name, 'a') as f:
f.write("{}".format(domain_str.strip()))
Related
I am trying to import a csv file that has hostnames and do ping test for such ~100 devices.
CSV1:
Hostname
abc
bcd
efg
.
.
and so on
I want to do ping test and write the ping status whether it is UP or Down in the output csv file along with the corresponding hostnames.
Something like this:
Output:
Hostname Ping Status
abc UP
bcd DOWN
efg DOWN
. .
. .
and so on
I have tried my best to lookup on the internet and SO, but found no luck. I am very new to python and I am still in the learning stages. Please help!!
import os
import csv
with open('CSV1.csv', 'r') as f:
reader = csv.DictReader(f)
rows = list(reader)
hosts = [row['CI_Name'] for row in rows]
statuses = [row['Status'] for row in rows]
for row in rows:
#ping hosts
hostname = row['CI_Name']
response = os.system("ping -n 1 " + hostname)
print ("[DEBUG]", response)
if response == 0:
print (hostname, 'is up')
row['Status'] = 'Up'
else:
print (hostname, 'is down')
row['Status'] = 'Down'
#write results
with open("Output Final.csv", "w") as w:
writer = csv.writer(w)
I am getting the output in Python console, and it is displaying the same details of ping that we get when done from 'CMD'. but my output csv file goes untouched.
This is much finished version of the code; although i am still working on it to print the ip address of those devices and get the headers correct when writing it to the csv file.
import socket
import os
import csv
name = {}
CI = {}
hostname = {}
status = {}
with open('Output1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
for rows in reader:
CI = rows['CI_Name']
name = socket.getfqdn(CI)
data = name
hostname = rows['CI_Name']
response = os.system('ping -n 2 ' + hostname)
if response == 0:
status = 'Up'
else:
status = 'Down'
with open('Output Final.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
output.writerow([hostname] + [data] + [status])
#write results
with open("Output Final.csv", "w", newline='') as w:
writer = csv.writer(w)
writer.writerow(["CI_NAME","Status"])
for r in rows:
writer.writerow((r['CI_Name'], r['Status']))
Finally fixed the whole code:
Note: I am integrating nslookup, ping test and ip address in this script to write FQDN, PING TEST and IP ADDRESS by passing in a list of devices that I have in my csv file
import socket
import os
import csv
import subprocess
name = {}
CI = {}
hostname = {}
status = {}
with open('Output1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
for rows in reader:
CI = rows['CI_Name']
try:
ip = socket.gethostbyname(CI)
except socket.error:
pass
name = socket.getfqdn(CI)
data = name
hostname = rows['CI_Name']
response = subprocess.Popen(['ping.exe',hostname], stdout = subprocess.PIPE).communicate()[0]
response = response.decode()
print(response)
if 'bytes=32' in response:
status = 'Up'
elif 'destination host unreachable' in response:
status = 'Unreachable'
else:
status = 'Down'
if status == 'Down':
ip = 'Not Found'
with open('Output Final.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
output.writerow([hostname] + [data] + [status] + [ip])
import re, urllib.request
patern = re.compile(r'image/\w*\W*\w*\.\jpg', re.I|re.M)
file = open('APODLinks.txt','r')
rf = file.read()
a = rf.split('\n')
file.close()
def lic(li):
if not li:
pass
else:
print(li[0])
f.write('http://apod.nasa.gov/apod/%s\n' % li[0])
def main():
for i in range(len(a)):
ur = urllib.request.urlopen(a[i])
mf = re.findall(patern, str(ur.read()))
lic(mf)
f = open('APODImgs.txt','w')
main()
f.close()
What's wrong with my code i'm try to write a txt file with all the jpg pictures
from Astronomy picture of the day but the file APODImgs.txt is empty...
The mf list some times is empty maybe this is my problem...
The APODLinks.txt contain urls like this:
apod.nasa.gov/apod/ap140815.html
apod.nasa.gov/apod/ap140814.html
apod.nasa.gov/apod/ap140813.html
7000 lines of urls
The APODImgs.txt must be like this:
apod.nasa.gov/apod/image/1408/Persei93_1abolfath.jpg
apod.nasa.gov/apod/image/1408/Supermoon_20140810.JPG
apod.nasa.gov/apod/image/1408/m57_nasagendler_3000.jpg
apod.nasa.gov/apod/image/1408/HebesChasma_esa_1024.jpg
...
Please help and sorry for my English...
Most probably not li is always true in lic, because your regexp doesn't match.
To figure it out, print the HTTP response body:
urr = urllib.request.urlopen(a[i]).read()
print repr(urr)
mf = re.findall(patern, urr)
print repr(mf)
lic(mf)
I change my code and it works!!!
import re, urllib.request
patern = re.compile(r'image/\w*\W*\w*\.jpg', re.I|re.M)
file = open('APODLinks.txt','r')
rf = file.read()
a = rf.split('\n')
file.close()
def lic(li):
if not li:
print("No matches found")
else:
print('http://apod.nasa.gov/apod/%s' % li[0])
f.write('http://apod.nasa.gov/apod/%s\n' % li[0])
def main():
for i in range(len(a)):
try:
ur = urllib.request.urlopen(a[i])
except:
print('404 not found!')
mf = re.findall(patern, str(ur.read()))
lic(mf)
f = open('APODImgs.txt','w')
main()
f.close()
I'm trying to extract only the IPs from a file, organize them numerically and put the result in another file.
The data looks like this:
The Spammer (and all his/her info):
Username: user
User ID Number: 0
User Registration IP Address: 77.123.134.132
User IP Address for Selected Post: 177.43.168.35
User Email: email#address.com
Here is my code, which does not sort the IPs correctly (i.e. it lists 177.43.168.35 before 77.123.134.132):
import re
spammers = open('spammers.txt', "r")
ips = []
for text in spammers.readlines():
text = text.rstrip()
print text
regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$',text)
if regex is not None and regex not in ips:
ips.append(regex)
for ip in ips:
OrganizedIPs = open("Organized IPs.txt", "a")
addy = "".join(ip)
if addy is not '':
print "IP: %s" % (addy)
OrganizedIPs.write(addy)
OrganizedIPs.write("\n")
spammers.close()
OrganizedIPs.close()
organize = open("Organized IPs.txt", "r")
ips = organize.readlines();
ips = list(set(ips))
print ips
for i in range(len(ips)):
ips[i] = ips[i].replace('\n', '')
print ips
ips.sort()
finish = open('organized IPs.txt', 'w')
finish.write('\n'.join(ips))
finish.close()
clean = open('spammers.txt', 'w')
clean.close()
I had tried using this IP sorter code but it needs a string were as the regex returns a list.
Or this (saving you string formatting cost):
def ipsort (ip):
return tuple (int (t) for t in ip.split ('.') )
ips = ['1.2.3.4', '100.2.3.4', '62.1.2.3', '62.1.22.4']
print (sorted (ips, key = ipsort) )
import re
LOG = "spammers.txt"
IPV4 = re.compile(r"(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})")
RESULT = "organized_ips.txt"
def get_ips(fname):
with open(fname) as inf:
return IPV4.findall(inf.read())
def numeric_ip(ip):
return [int(i) for i in ip.split(".")]
def write_to(fname, iterable, fmt):
with open(fname, "w") as outf:
for i in iterable:
outf.write(fmt.format(i))
def main():
ips = get_ips(LOG)
ips = list(set(ips)) # uniquify
ips.sort(key=numeric_ip)
write_to(RESULT, ips, "IP: {}\n")
if __name__=="__main__":
main()
Try this:
sorted_ips = sorted(ips, key=lambda x: '.'.join(["{:>03}".format(octet) for octet in x.split(".")])
I am trying to do a taxonomic classification of my blast output, using scripts on this web-site https://github.com/bartaelterman/BlastTaxonomy.
I am quite new to python and sometimes I try to find already existing scripts to do complicated jobs. However, scripts like this confuse me a lot. I have a very basic question, i.e. in the script below, where do I replace with my input file, can you give me a short explanation of the script like this to have an idea?
Also, if you have any other ideas how to do it, would be great!
import sys
sys.path.append("./scripts")
import blasthittaxonomy
def checkarguments():
if len(sys.argv) != 2:
print "usage: ./addTaxonomyToBlastOutput.py <blast output file>"
sys.exit(-1)
def main():
checkarguments()
taxfetcher = blasthittaxonomy.TaxonomyFetcher()
filename = sys.argv[1]
infile = open(filename)
header = infile.readline()
print "\t".join(["seqnr", "hitginr", "hitname", "evalue", "bitscore", "similarity", "score", "division", "scientificName", "rank1", "rank2"])
for line in infile:
newline = line.rstrip("\n")
seqnr, ginr, hitname, evalue, bitscore, sim, score = newline.split("\t")
division = ""
scientName = ""
rank1 = ""
rank2 = ""
if ginr != "":
taxonomy = taxfetcher.getTaxonomy(int(ginr))
if taxonomy != "":
scientName = taxonomy[0]["ScientificName"]
if scientName == "unidentified":
scientName = ""
else:
division = taxonomy[0]["Division"]
try:
rank1 = taxonomy[0]["LineageEx"][0]["ScientificName"]
except:
rank1 = ""
try:
rank2 = taxonomy[0]["LineageEx"][1]["ScientificName"]
except:
rank2 = ""
print "\t".join([seqnr, ginr, hitname, evalue, bitscore, sim, score, division, scientName, rank1, rank2])
taxfetcher.die()
main()
You don't replace anything, just run the script with your file name as a command-line argument
That's where the script reads the input file name:
filename = sys.argv[1]
I have a file that has one sentence per line. I am trying to read the file and search if the sentence is a question using regex and extract the wh-word from the sentences and save them back into another file according the order it appeared in the first file.
This is what I have so far..
def whWordExtractor(inputFile):
try:
openFileObject = open(inputFile, "r")
try:
whPattern = re.compile(r'(.*)who|what|how|where|when|why|which|whom|whose(\.*)', re.IGNORECASE)
with openFileObject as infile:
for line in infile:
whWord = whPattern.search(line)
print whWord
# Save the whWord extracted from inputFile into another whWord.txt file
# writeFileObject = open('whWord.txt','a')
# if not whWord:
# writeFileObject.write('None' + '\n')
# else:
# whQuestion = whWord
# writeFileObject.write(whQuestion+ '\n')
finally:
print 'Done. All WH-word extracted.'
openFileObject.close()
except IOError:
pass
The result after running the code above: set([])
Is there something I am doing wrong here? I would be grateful if someone can point it out to me.
Something like this:
def whWordExtractor(inputFile):
try:
with open(inputFile) as f1:
whPattern = re.compile(r'(.*)who|what|how|where|when|why|which|whom|whose(\.*)', re.IGNORECASE)
with open('whWord.txt','a') as f2: #open file only once, to reduce I/O operations
for line in f1:
whWord = whPattern.search(line)
print whWord
if not whWord:
f2.write('None' + '\n')
else:
#As re.search returns a sre.SRE_Match object not string, so you will have to use either
# whWord.group() or better use whPattern.findall(line)
whQuestion = whWord.group()
f2.write(whQuestion+ '\n')
print 'Done. All WH-word extracted.'
except IOError:
pass
Not sure if it's what you're looking for, but you could try something like this:
def whWordExtractor(inputFile):
try:
whPattern = re.compile(r'who|what|how|where|when|why|which|whom|whose', re.IGNORECASE)
with open(inputFile, "r") as infile:
for line in infile:
whMatch = whPattern.search(line)
if whMatch:
whWord = whMatch.group()
print whWord
# save to file
else:
# no match
except IOError:
pass
Change '(.*)who|what|how|where|when|why|which|whom|whose(\.*)' to
".*(?:who|what|how|where|when|why|which|whom|whose).*\."