Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import sys
- import optparse
- import telnetlib
- import csv
- def makeiplist(filename, a, count):
- f = open(filename, 'rU')
- for line in f:
- b = line
- a.append(b)
- count += 1
- return a, count
- f.close()
- def main():
- a = []
- count = 0
- parser = optparse.OptionParser()
- parser.add_option('-f', '--file <List of IPs>', default=None, action="store", dest="filename", help="Filename to edit")
- parser.add_option('-p', '--pattern', default=None, action="store", dest="pattt", help="Pattern to find")
- parser.add_option('-u', '--username', default=None, action="store", dest="user", help="Username")
- parser.add_option('-x', '--password', default=None, action="store", dest="password", help="Password")
- parser.add_option('-o', '--output', default=None, action="store", dest="newfile", help="CSV Output file")
- options, args = parser.parse_args()
- filename = options.filename
- pattt = options.pattt
- user = options.user
- pw = options.password
- newfile = options.newfile
- if filename == None or pattt == None or user == None or pw == None or newfile == None:
- print "-f(--file) List of IPs -p(--pattern) pattern -u (--username) username -x (--password) password -o (--output) output csv file"
- sys.exit(1)
- w=csv.writer(file(newfile,'wb'),dialect='excel')
- makeiplist(filename, a, count)
- print "Attempting to grab data from : ", len(a), "routers"
- for line in a:
- tn = telnetlib.Telnet(a[count])
- tn.read_until("laptop login: ")
- tn.write(user + "\n")
- if pw:
- tn.read_until("Password: ")
- tn.write(pw + "\n")
- tn.write("cat target\n")
- tn.write("exit\n")
- dump = tn.read_all()
- new1 = dump.split('\n')
- xxx = re.findall('(?<=Product ID : )\w+', dump)
- print 'Connected to : ', line, 'Got this ID ', xxx
- finish = []
- finish.append(line)
- finish.append(xxx)
- w.writerow(finish)
- print 'Output written to : ' + newfile
- #new1 = dump.split('\n')
- #print new1
- #lala = ''
- #lala = re.findall(pattt, dump)
- #print lala
- #dump.findall("Product ID: /d:/d", dump)
- #w.writerows(new1)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement