Advertisement
Guest User

Untitled

a guest
May 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import os
  2. import re
  3. import sys
  4. import optparse
  5. import telnetlib
  6. import csv
  7.  
  8. def makeiplist(filename, a, count):
  9. f = open(filename, 'rU')
  10. for line in f:
  11. b = line
  12. a.append(b)
  13. count += 1
  14. return a, count
  15. f.close()
  16.  
  17. def main():
  18. a = []
  19. count = 0
  20.  
  21. parser = optparse.OptionParser()
  22.  
  23. parser.add_option('-f', '--file <List of IPs>', default=None, action="store", dest="filename", help="Filename to edit")
  24. parser.add_option('-p', '--pattern', default=None, action="store", dest="pattt", help="Pattern to find")
  25. parser.add_option('-u', '--username', default=None, action="store", dest="user", help="Username")
  26. parser.add_option('-x', '--password', default=None, action="store", dest="password", help="Password")
  27. parser.add_option('-o', '--output', default=None, action="store", dest="newfile", help="CSV Output file")
  28.  
  29. options, args = parser.parse_args()
  30.  
  31. filename = options.filename
  32. pattt = options.pattt
  33. user = options.user
  34. pw = options.password
  35. newfile = options.newfile
  36.  
  37. if filename == None or pattt == None or user == None or pw == None or newfile == None:
  38. print "-f(--file) List of IPs -p(--pattern) pattern -u (--username) username -x (--password) password -o (--output) output csv file"
  39. sys.exit(1)
  40.  
  41. w=csv.writer(file(newfile,'wb'),dialect='excel')
  42. makeiplist(filename, a, count)
  43. print "Attempting to grab data from : ", len(a), "routers"
  44. for line in a:
  45. tn = telnetlib.Telnet(a[count])
  46. tn.read_until("laptop login: ")
  47. tn.write(user + "\n")
  48. if pw:
  49. tn.read_until("Password: ")
  50. tn.write(pw + "\n")
  51. tn.write("cat target\n")
  52. tn.write("exit\n")
  53. dump = tn.read_all()
  54. new1 = dump.split('\n')
  55. xxx = re.findall('(?<=Product ID : )\w+', dump)
  56. print 'Connected to : ', line, 'Got this ID ', xxx
  57. finish = []
  58. finish.append(line)
  59. finish.append(xxx)
  60. w.writerow(finish)
  61.  
  62. print 'Output written to : ' + newfile
  63.  
  64. #new1 = dump.split('\n')
  65. #print new1
  66. #lala = ''
  67. #lala = re.findall(pattt, dump)
  68. #print lala
  69. #dump.findall("Product ID: /d:/d", dump)
  70. #w.writerows(new1)
  71.  
  72. if __name__ == '__main__':
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement