Guest User

Untitled

a guest
Apr 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. ############################################################################################
  2. # This script connects to every server, obtains root
  3. # credentials (sudo is disabled), get info for every drive from the file (roots)
  4. # (Hostname, RAID_type, Disk_Name, HDD_type, Power_ON_Hours, Model_type, SerialNo)
  5. # and create .csv report
  6. #
  7. # format of the roots file should be:
  8. # hostname<TAB>rootpassword<TAB>adminpassword
  9. #
  10. ############################################################################################
  11.  
  12. #!/usr/bin/python2
  13. import paramiko
  14. import re
  15.  
  16. def mdadminfo():
  17. fileread=open("/home/roots", "r")
  18. w=open("/home/report.csv","a+")
  19. CSVFIRSTLINE="Hostname,RAID_type,Disk_Name,HDD_type,Power_ON_Hours,Model_type,SerialNo" + "\n"
  20. w.write(CSVFIRSTLINE)
  21. w.close()
  22.  
  23. f1 = fileread.readlines()
  24. for x in f1:
  25. w=open("/home/dgonczol/Desktop/report.csv","a+")
  26. NAME,ROOTPASS,ADMINPASS = re.split(r'\t+', x)
  27. ADMINPASS=ADMINPASS[:-1] #removing last character because Python adds new line automatically
  28. print(NAME,ADMINPASS,ROOTPASS)
  29. print(ROOTPASS)
  30. print(ADMINPASS)
  31.  
  32. ssh = paramiko.SSHClient()
  33. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  34. ssh.connect(NAME, port=22, username='admin', password=ADMINPASS)
  35.  
  36. stdin, stdout, stderr = ssh.exec_command('lsblk | grep "0 disk" | cut -d" " -f1' )
  37. DISKSCOUNT = stdout.read()
  38.  
  39. for DISK in DISKSCOUNT.splitlines():
  40. stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"', get_pty=True)
  41. stdin.write(ROOTPASS)
  42. stdin.write('\n')
  43. DEBUG1=stdout.read()
  44. stdin, stdout, stderr = ssh.exec_command('head -1 /proc/mdstat' )
  45. MDSTAT = stdout.read()
  46.  
  47. if "raid" in MDSTAT:
  48. RAID_TYPE="software"
  49. print "RAID_TYPE=RAID_TYPE=RAID_TYPE=RAID_TYPE=" + (RAID_TYPE) #[DEBUG]
  50. else:
  51. RAID_TYPE="hardware"
  52.  
  53. print "RAID_TYPE=RAID_TYPE=RAID_TYPE=RAID_TYPE=" + RAID_TYPE #[DEBUG]
  54.  
  55. if RAID_TYPE=="software":
  56. stdin, stdout, stderr = ssh.exec_command('hostname')
  57. HOSTNAME = stdout.read()
  58. stdin, stdout, stderr = ssh.exec_command('cat /tmp/' + DISK + ' | grep "Power_On_Hours" | cut -d" " -f44' )
  59. POWERON = stdout.read()
  60. stdin, stdout, stderr = ssh.exec_command('cat /tmp/' + DISK + ' | grep "Rotation Rate" | cut -d" " -f6' )
  61. TYPE = stdout.read()
  62. stdin, stdout, stderr = ssh.exec_command('cat /tmp/' + DISK + ' | grep "Device Model:" | cut -d" " -f7' )
  63. MODEL = stdout.read()
  64. stdin, stdout, stderr = ssh.exec_command('cat /tmp/' + DISK + ' | grep "Serial Number:" | cut -d" " -f6' )
  65. SERIAL = stdout.read()
  66.  
  67. typelen=len(TYPE)
  68.  
  69. if typelen == 0:
  70. TYPE = "HDD"
  71. else:
  72. TYPE = "SSD"
  73.  
  74. print "Hostname: " + HOSTNAME + "HDD type:" + TYPE + "\n" + "Power on time: " + POWERON + "Model: " + MODEL + "Serial No: " + SERIAL
  75. #CSVFIRSTLINE="Hostname,Disk Name,HDD type,Power ON time,Model type,Serial No"
  76. #CSVLINE = HOSTNAME[:-1] + "," + RAID_TYPE + "," + DISK + "," + TYPE + "," + POWERON[:-1] + "," + MODEL[:-1] + "," + SERIAL
  77. CSVLINE = NAME + "," + RAID_TYPE + "," + DISK + "," + TYPE + "," + POWERON[:-1] + "," + MODEL[:-1] + "," + SERIAL
  78. #print "CSVLINE = " + CSVLINE #[DEBUG]
  79. w.write(CSVLINE)
  80. else:
  81. CSVLINE = NAME + "," + RAID_TYPE + ",,,,,\n"
  82. w.write(CSVLINE)
  83.  
  84. w.close()
  85. stdin.flush()
  86. ssh.close()
  87.  
  88.  
  89. if __name__== "__main__":
  90. mdadminfo()
Add Comment
Please, Sign In to add comment