bradhsd214

test.py

Mar 14th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ################################
  3. #
  4. # Name: test.py
  5. #
  6. # Description:
  7. #
  8. # Version: 1.0
  9. #
  10. # Date User Changes
  11. # ====== ========= =======
  12. #
  13. ################################
  14. """
  15. test.py
  16.  
  17. Usage: python test.py [options]
  18.  
  19. Options:
  20. -h, --help Show this help
  21. -f, --file Name of file containing list of MMs or AMMs. ONE per line
  22.  
  23. Examples:
  24. test.py -h (shows this help information)
  25. test.py -f mm.txt (full path to file contain the list of AMMs to be looked at)
  26. """
  27.  
  28. # Modules to import
  29. #====================#
  30. import os
  31. import sys
  32. import getopt
  33. import telnetlib
  34. import string
  35. import re
  36. import socket
  37. ######################
  38.  
  39. # Try and open the file supplied on the command line.
  40. def checkInputfile( filename ):
  41. try:
  42. f = open('%s' % filename, 'r')
  43. except IOError:
  44. print "Error: can\'t find file %s or read data" % filename
  45. else:
  46. processInfo()
  47. f.close()
  48.  
  49. # Print out how you use the script
  50. def usage():
  51. print __doc__
  52.  
  53. # Process the information gathered from the MM
  54. def processInfo():
  55. try:
  56. dclayout = open('layout.txt', 'a')
  57. except IOError:
  58. print "Unable to create the output file."
  59. else:
  60. for i, line in enumerate(open("mminfo.txt", 'r'), 1):
  61. # for line in open("mminfo.txt", 'r'):
  62. # Find the name assigned to the AMM and substitute any '_' for '.'.
  63. if re.search("^Hostname:", line):
  64. hostfull = line.strip()
  65. host = string.replace(hostfull, '_', '.')
  66. hos = host[28:]
  67. print "\n"
  68. dclayout.write("\n")
  69. print "\t %s" % hos
  70. dclayout.write("\t %s\n" % hos)
  71. # Find the IP of the AMM.
  72. if re.search("Static IP", line):
  73. IP = line[23:].strip()
  74. #IP = ip[23:]
  75. print "\t %s" % IP
  76. dclayout.write("\t %s\n" % IP)
  77. # Find the number of the slot.
  78. if re.search("system> info", line):
  79. slot = line.strip()
  80. #num = line[22:len(line) -1].strip()
  81. num = slot[22:len(slot) -1]
  82. # Find the name of the host in a slot and the IP of the host.
  83. ################
  84. # TO DO - Use domain part from Hostname above and add as part of dnsname below
  85. ################
  86. if re.search("^Name:", line):
  87. nmixed = line.strip()
  88. n = nmixed.lower()
  89. name = n[5:]
  90. dnsname = name + ".ZONE21.WORK"
  91. host = dnsname.strip()
  92. print "%s\t%s" % (num, name)
  93. dclayout.write("%s\t%s\n" % (num, name))
  94. ###############
  95. # Commented as names in internal DNS - DOES WORK
  96. ###############
  97. # try:
  98. # hip = socket.gethostbyname('%s' % host)
  99. # print "\t %s" % hip
  100. # dclayout.write("\t %s\n" % hip)
  101. # #return hip
  102. # except socket.gaierror:
  103. # print "\t "
  104. # dclayout.write("\t \n")
  105.  
  106. # Find the serial number of the blade.
  107. if re.search("Mach serial number:", line):
  108. serial = line[20:].strip()
  109. if serial != 'Not Available':
  110. print "\t %s" % serial
  111. dclayout.write("\t %s\n" % serial)
  112. # Find the MAC address of the first MAC.
  113. if re.search("MAC Address 1:", line):
  114. mac = line[15:].strip()
  115. print "\t %s" % mac
  116. dclayout.close()
  117.  
  118. def main(argv):
  119.  
  120. try:
  121. opts, args = getopt.getopt(argv, "hf:", ["help=", "file="])
  122. except getopt.GetoptError:
  123. usage()
  124. sys.exit(2)
  125. for opt, arg in opts:
  126. if opt in ("-h", "--help"):
  127. usage()
  128. sys.exit()
  129. elif opt in ("-f", "--file"):
  130. filename = arg
  131. checkInputfile( filename )
  132.  
  133. #===========================================================
  134. # Main loop
  135. #===========================================================
  136. if __name__ == "__main__":
  137.  
  138. if len(sys.argv) < 2:
  139. usage()
  140. else:
  141. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment