hi....


I am doing an snmpwalk on MIB variables and dumping the output to a text file.
I dont need all the info and need to read only a part of it.

How can this be done efficiently in python?

Recommended Answers

All 8 Replies

hi....


I am doing an snmpwalk on MIB variables and dumping the output to a text file.
I dont need all the info and need to read only a part of it.

How can this be done efficiently in python?

I am also new to Python so I can't tell you too much about it. But open() should be what you're looking if you want to read and write files.

rf = open( 'FILE PATH', 'r' ) 
Input=rf.readlines() # Reads the lines
rf.close()

open('file path','w') is for writing
Here is a reference. http://docs.python.org/tutorial/inputoutput.html

I am also new to Python so I can't tell you too much about it. But open() should be what you're looking if you want to read and write files.

rf = open( 'FILE PATH', 'r' ) 
Input=rf.readlines() # Reads the lines
rf.close()

open('file path','w') is for writing
Here is a reference. http://docs.python.org/tutorial/inputoutput.html

thanks for the reply:) but this is not what I am looking for. readlines() just reads all the lines in the file. I need to read only a selected part of it

How about attaching a sample file and telling us what part of the data you want to extract? That way those of us who are not network engineers might understand what you want to do. For example, does your file contain data like the following?

.1.3.6.1.2.1.25.3.7.1.1.1537.1 = INTEGER: 1
.1.3.6.1.2.1.25.3.7.1.1.1537.2 = INTEGER: 2
.1.3.6.1.2.1.25.3.7.1.1.1537.3 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.2.1537.1 = STRING: "/dev/hdb1"
.1.3.6.1.2.1.25.3.7.1.2.1537.2 = STRING: "/dev/hdb2"
.1.3.6.1.2.1.25.3.7.1.2.1537.3 = STRING: "/dev/hdb3"
.1.3.6.1.2.1.25.3.7.1.3.1537.1 = STRING: "0x341"
.1.3.6.1.2.1.25.3.7.1.3.1537.2 = STRING: "0x342"
.1.3.6.1.2.1.25.3.7.1.3.1537.3 = STRING: "0x343"
.1.3.6.1.2.1.25.3.7.1.4.1537.1 = INTEGER: 14877060 KBytes
.1.3.6.1.2.1.25.3.7.1.4.1537.2 = INTEGER: 9920624 KBytes
.1.3.6.1.2.1.25.3.7.1.4.1537.3 = INTEGER: 0 KBytes
.1.3.6.1.2.1.25.3.7.1.5.1537.1 = INTEGER: 1
.1.3.6.1.2.1.25.3.7.1.5.1537.2 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.5.1537.3 = INTEGER: 0

I copied that from http://www.unleashnetworks.com/resources/articles/95-work-with-output-from-snmpwalk.html but I don't know what portion of the file you want to extract.

How about attaching a sample file and telling us what part of the data you want to extract? That way those of us who are not network engineers might understand what you want to do. For example, does your file contain data like the following?

.1.3.6.1.2.1.25.3.7.1.1.1537.1 = INTEGER: 1
.1.3.6.1.2.1.25.3.7.1.1.1537.2 = INTEGER: 2
.1.3.6.1.2.1.25.3.7.1.1.1537.3 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.2.1537.1 = STRING: "/dev/hdb1"
.1.3.6.1.2.1.25.3.7.1.2.1537.2 = STRING: "/dev/hdb2"
.1.3.6.1.2.1.25.3.7.1.2.1537.3 = STRING: "/dev/hdb3"
.1.3.6.1.2.1.25.3.7.1.3.1537.1 = STRING: "0x341"
.1.3.6.1.2.1.25.3.7.1.3.1537.2 = STRING: "0x342"
.1.3.6.1.2.1.25.3.7.1.3.1537.3 = STRING: "0x343"
.1.3.6.1.2.1.25.3.7.1.4.1537.1 = INTEGER: 14877060 KBytes
.1.3.6.1.2.1.25.3.7.1.4.1537.2 = INTEGER: 9920624 KBytes
.1.3.6.1.2.1.25.3.7.1.4.1537.3 = INTEGER: 0 KBytes
.1.3.6.1.2.1.25.3.7.1.5.1537.1 = INTEGER: 1
.1.3.6.1.2.1.25.3.7.1.5.1537.2 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.5.1537.3 = INTEGER: 0

I copied that from http://www.unleashnetworks.com/resources/articles/95-work-with-output-from-snmpwalk.html but I don't know what portion of the file you want to extract.

Exactly!!
my file contains similar data
for example, say from the above file, i need to extract only

.1.3.6.1.2.1.25.3.7.1.2.1537.1 = STRING: "/dev/hdb1"
.1.3.6.1.2.1.25.3.7.1.2.1537.2 = STRING: "/dev/hdb2"
.1.3.6.1.2.1.25.3.7.1.2.1537.3 = STRING: "/dev/hdb3"
.1.3.6.1.2.1.25.3.7.1.3.1537.1 = STRING: "0x341"
.1.3.6.1.2.1.25.3.7.1.3.1537.2 = STRING: "0x342"
.1.3.6.1.2.1.25.3.7.1.3.1537.3 = STRING: "0x343"

and

.1.3.6.1.2.1.25.3.7.1.5.1537.2 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.5.1537.3 = INTEGER: 0

how can this be done??

This should work (not tested)

of = open(my_out_filename, 'w')
for line in open(my_in_filename,'r'):
    if 'STRING' in line or 'INTEGER' in line:
        of.write(line)
of.close()

For example:

interesting = [dataline for dataline in open('data.txt')
               if any(part in dataline for part in ('/dev/hd','0x34'))]
print ''.join(interesting)

You want all the lines containing the literal 'STRING' plus only two of the lines containing the literal 'INTEGER'? Here is how you could match the lines you want using a regex pattern.

#!/usr/bin/env python
import re

#You didn't say what rule you had in mind
#when you chose only the last two lines containing 'INTEGER'
#so the following pattern represents my guess
pattern = r'(STRING:.+)|(5\.1537\.(2|3) = INTEGER: \d)$'

for line in open('/home/david/Programming/Python/data.txt'):
    match = re.search(pattern, line)
    if match:
        print line.rstrip()

This gives the following output.

.1.3.6.1.2.1.25.3.7.1.2.1537.1 = STRING: "/dev/hdb1"
.1.3.6.1.2.1.25.3.7.1.2.1537.2 = STRING: "/dev/hdb2"
.1.3.6.1.2.1.25.3.7.1.2.1537.3 = STRING: "/dev/hdb3"
.1.3.6.1.2.1.25.3.7.1.3.1537.1 = STRING: "0x341"
.1.3.6.1.2.1.25.3.7.1.3.1537.2 = STRING: "0x342"
.1.3.6.1.2.1.25.3.7.1.3.1537.3 = STRING: "0x343"
.1.3.6.1.2.1.25.3.7.1.5.1537.2 = INTEGER: 3
.1.3.6.1.2.1.25.3.7.1.5.1537.3 = INTEGER: 0
import re

snmpdata = open(snmpwalk_output)
patt = re.compile(r"(\.\d+)+ = [A-Z]+\:")
out = [ d for d in snmpdata if patt.match(d) ]
    
outfile = open("z.txt","w")
outfile.write('\n'.join(out))
outfile.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.