using dig with python to list all host - python

I'm very newbie with python and programming and I just can't find the way to do the code I need. So help, please :)
Background:
I'll be checking multiple different (linux) environments so I want to get the nameserver (ns) and domain information from resolv.conf and pass these values to dig.
Thanks to this forum and google I was able to get the ns and domain info but struggling still how to pass the info to dig.
Python version is 2.7.
Linux is rhel6.x.
Problem:
How to format the dig command using return values for ns and domain and giving few parameters:
dig +search <domain>. axfr #<ns> +noall +answer
The and are coming from the functions.
What I want:
read the ns and domain from /et/resolv.conf. Only first ns is needed
(this has been achieved)
pass the ns and domain to dig (this is the part I just can't figure out). dig command is this: dig +search . axfr # +noall +answer
modify the output of dig so that the list would like this:
host_ip1 host_name1
host_ip2 host_name2
....
would be nice if the output was sorted based on IP
that's about it.
I know there are dns modules available to do the same, but they are no option for me since they are not available.
So far this is what I have:
# -*- coding: utf-8 -*-
### Functions start ###
def get_ns():
with open("/etc/resolv.conf","r") as readFile:
#print [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("nameserver")][0]
x = [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("nameserver")][0]
readFile.close()
return x
def get_domain():
with open("/etc/resolv.conf","r") as readFile:
#print [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("search")][0]
y = [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("search")][0]
readFile.close()
return y
### Functions end ###
# just to test all is working:
ns = get_ns()
# to get the dig command:
domain = get_domain()
domain1 = domain + '.'
ns1 = '#' + ns
print ns1
print domain1
list = os.system("dig +search " + domain1 + " axfr " + ns1 + " +noall +answer")
print list
Any help will be highly appreciated!

Related

IndexError: list index out of range in Python Script

I'm new to python and so I apologize if this question has already been answered. I've used this script before and its worked so I'm not at all sure what is wrong.
I'm trying to transform a MALLET output document into a long list of topic, weight, value rather than a wide list of topics documents and weights.
Here's what the original csv I'm trying to convert looks like but there are 30 topics in it (its a text file called mb_composition.txt):
0 file:/Users/mandyregan/Dropbox/CPH-DH/MiningtheSurge/txt/Abizaid.txt 6.509147794508226E-6 1.8463345214533957E-5 3.301298069640119E-6 0.003825178550032757 0.15240841618294929 0.03903974304065183 0.10454783676528623 0.1316719812119471 1.8018057013225344E-5 4.869261713020613E-6 0.0956868156114931 1.3521101623203115E-5 9.514591058923748E-6 1.822741355900598E-5 4.932324961835634E-4 2.756817586271138E-4 4.039186874601744E-5 1.0503346606335033E-5 1.1466132458804392E-5 0.007003443189848799 6.7094360963952E-6 0.2651753488982284 0.011727025879070194 0.11306132549594633 4.463460490946615E-6 0.0032751230536005056 1.1887304822238514E-5 7.382714572306351E-6 3.538808652077042E-5 0.07158823129977483
1 file:/Users/mandyregan/Dropbox/CPH-DH/MiningtheSurge/txt/Jeffrey,%20Jim%20-%20Chk5-%20ASC%20-%20FINAL%20-%20Sept%202017.docx.txt 4.296636200313062E-6 1.218750594272488E-5 1.5556725986514498E-4 0.043172816021532695 0.04645757277949794 0.01963429696910822 0.1328206370818606 0.116826297071711 1.1893574776047563E-5 3.2141605637859693E-6 0.10242945223692496 0.010439315937573735 0.2478814493196687 1.2031769351093548E-5 0.010142417179693447 2.858721603853616E-5 2.6662348272204834E-5 6.9331747684835E-6 7.745091995495631E-4 0.04235638910274044 4.428844900369446E-6 0.0175105406405736 0.05314379308820005 0.11788631730736487 2.9462944350793084E-6 4.746133386282654E-4 7.846714475661223E-6 4.873270616886766E-6 0.008919869163605806 0.02884824479155971
And here's the python script I'm trying to use to convert it:
infile = open('mallet_output_files/mb_composition.txt', 'r')
outfile = open('mallet_output_files/weights.csv', 'w+')
outfile.write('file,topicnum,weight\n')
for line in infile:
tokens = line.split('\t')
fn = tokens[1]
topics = tokens[2:]
#outfile.write(fn[46:] + ",")
for i in range(0,59):
outfile.write(fn[46:] + ",")
outfile.write(topics[i*2]+','+topics[i*2+1]+'\n')
I'm running this in the terminal with python reshape.py and I get this error:
Traceback (most recent call last):
File "reshape.py", line 12, in <module>
outfile.write(topics[i*2]+','+topics[i*2+1]+'\n')
IndexError: list index out of range
Any idea what I'm doing wrong here? I can't seem to figure it out and am frustrated because I know Ive used this script many times before with success! If it helps I'm on Mac OSx with Python Version 2.7.10
The problem is you're looking for 60 topics per line of your CSV.
If you just want to print out the topics in the list up to the nth topic per line, you should probably define your range by the actual number of topics per line:
for i in range(len(topics) // 2):
outfile.write(fn[46:] + ",")
outfile.write(topics[i*2]+','+topics[i*2+1]+'\n')
Stated more pythonically, it would look something like this:
# Group the topics into tuple-pairs for easier management
paired_topics = [tuple(topics[i:i+2]) for i in range(0, len(topics), 2)]
# Iterate the paired topics and print them each on a line of output
for topic in paired_topics:
outfile.write(fn[46:] + ',' + ','.join(topic) + '\n')
You need to debug your code. Try printing out variables.
infile = open('mallet_output_files/mb_composition.txt', 'r')
outfile = open('mallet_output_files/weights.csv', 'w+')
outfile.write('file,topicnum,weight\n')
for line in infile:
tokens = line.split('\t')
fn = tokens[1]
topics = tokens[2:]
# outfile.write(fn[46:] + ",")
for i in range(0,59):
# Add a print statement like this
print(f'Topics {i}: {i*2} and {i*2+1}')
outfile.write(fn[46:] + ",")
outfile.write(topics[i*2]+','+topics[i*2+1]+'\n')
Your 'topics' list only has 30 elements? It looks like you're trying to access items far outside of the available range, i.e., you're trying to access topics[x] where x > 30.

Python - PING a list of IP Address from database

Python - PING a list of IP Address from database
I have a list of ip addresses consisting of 200 locations, which in that location there are 4 ip addresses that I need to do ping testing. I intend to make a command which when I write the name or code of a particular location then it will directly ping to 4 ip address at that location. I have learned a bit to create a list that contains the ip address I entered through the command input () like this :
import os
import socket
ip = []
y = ['IP 1 : ','IP 2 : ', 'IP 3 : ', 'IP 4 : ']
while True:
for x in y:
server_ip = input(x)
ip.append(server_ip)
break
for x in ip:
print("\n")
rep = os.system('ping ' + x + " -c 3")
please give me a little advice about the command I want to make so that I no longer need to enter the ip address one by one. which still makes me confused, especially on how to make the existing items in the database into a variable x which we will insert into this command;
rep = os.system ('ping' + x + "-c 3")
EDIT: It now iterates over a CSV file rather than a hard-coded Python dictionary.
I believe you will be better off using python dictionaries rather than python lists. Assuming you are using Python 3.X, this is what you want to run:
import os
import csv
# Save the IPs you want to ping inside YOURFILE.csv
# Then iterate over the CSV rows using a For Loop
# Ensure your ip addresses are under a column titled ip_address
with open('YOURFILE.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
rep = os.system("ping " + row['ip_address'] + " -c 3")

python nesting loops

I am trying perform a nested loop to combine data into a line by using matched MAC Addresses in both files.
I am able to pull the loop fine without the regex, however, when using the search regex below, it will only loop through the MAC_Lines once and print the correct results using the first entry in the MAC_Lines and stop. I'm unsure how to make the MAC_Lines go to the next line and repeat the process for all of the entries in the MAC_Lines.
try:
for mac in MAC_Lines:
MAC_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', mac, re.I)
MAC_address_final = MAC_address.group()
for arp in ARP_Lines:
ARP_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', arp, re.I)
ARP_address_final = ARP_address.group()
if MAC_address_final == ARP_address_final:
print mac + arp
continue
except Exception:
print 'completed.'
Results:
13,64,00:0c:29:36:9f:02,giga-swx 0/213,172.20.13.70, 00:0c:29:36:9f:02, vlan 64
completed.
I learned that the issue was how I opened the file. I should have used the 'open':'as' keywords when opening both files to allow the files to properly close and reopen for the next loop. Below is the code I was looking for.
Below is the code:
with open('MAC_List.txt', 'r') as read0:for items0 in read0:
MAC_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', items0, re.I)
if MAC_address:
mac_addy = MAC_address.group().upper()
with open('ARP_List.txt', 'r') as read1:
for items1 in read1:
ARP_address = re.search(r'([a-fA-F0-9]{2}[:|\-]?){6}', items1, re.I)
if ARP_address:
arp_addy = ARP_address.group()
if mac_addy == arp_addy:
print(items0.strip() + ' ' + items1.strip())

how can i find "smallest eigenvalue" o a plate with python scripting in abaqus?

i write a python script to modeling and analysis a plate for buckling.
i need the minimum eigenvalue for run the other script for RIKS analysis.
how can i find "smallest eigenvalue" with python scripting in abaqus?
foo = [3,1,4,5]
print min(foo)
outputs => 1
datFullPath = PathDir+FileName+'.dat'
myOutdf = open(datFullPath,'r')
stline=' MODE NO EIGENVALUE\n'
lines = myOutdf.readlines()
ss=0
for i in range(len(lines)-1):
if lines[i] == stline :
print lines[i]
ss=i
f1=lines[ss+3]
MinEigen=float(f1[15:24])
myOutdf.close()
MinEigen
# Import abaqus odb work related modules
from odbAccess import *
# Read the odb file, change name and path as per your requirement
### This is a typical procedure to read history-outputs, as
### frequency etc. information is not available as field output
odb = openOdb(path =JobName+'.odb')
a=odb.rootAssembly
step=odb.steps['Step-1']
frames=step.frames
numframes=len(frames)
i=0
MinEigen=[]
for frame in frames :
f1=frame.description
if len(f1[28:48])>1:
MinEg=float(f1[28:48])
if MinEg>0.0:
MinEigen.append (MinEg)
print MinEigen,min(MinEigen)
fwall=open("EIGENX.txt", 'a')
fwall.write(str(min(MinEigen))+'\n')
fwall.close()
odb.close()
##stop

Python string comparison not working for matching lines [duplicate]

This question already has answers here:
String comparison doesn't seem to work for lines read from a file
(2 answers)
Closed 5 years ago.
I'm trying to write a small Python script to generate CentOS7 kickstart configs. I have a skeleton config file and based on some user inputs, the script will pop out a custom cfg file by inserting the customized blocks into the skeleton. However, the string comparison is not working for some reason.
#!/usr/bin/python
type = raw_input("Static OR DHCP: ")
gateway = raw_input("Gateway IP: ")
nameserver = raw_input("DNS Server: ")
hostname = raw_input("Hostname: ")
ipaddr = raw_input("IP Address: ")
skeleton = open('ks_skeleton.cfg', 'r')
config = open(hostname + '.cfg', 'w')
for line in skeleton:
if line == "$NETWORK":
print("Interting Network values...");
config.write("network --bootproto=" + type + " --device=ens192 --gateway=" + gateway + " --ip=" + ipaddr + " --nameserver=" + nameserver + " --netmask=255.255.255.0 --ipv6=auto --activate\n");
config.write("network --hostname=" + hostname + "\n");
else:
config.write(line);
The lines that you read from skeleton have new lines at the end, so the exact string comparison are probably not going to work. If you do line = line.strip() as the first line of your loop it will remove whitespace from before and after any text on the line, and might get you closer to what you want.

Categories

Resources