Python Script to add Grub Password Automatically

So you need to add Grub password in 50+ servers? No need to do this manually.


import os, fileinput
pass1 = raw_input('Enter the password for grub ')
cmd1 = 'echo -e "' + pass1 + '\n' + pass1 + '" | /sbin/grub-md5-crypt > log.txt'
os.system(cmd1)

with open('log.txt') as f:
    for line in f:
        cipher = line
insert1 = 'password --md5 ' + cipher

with open ('/etc/grub.conf') as f:
    lines = f.readlines()
    f.close()
lines.insert(lines.index('timeout=5\n') + 1, insert1)

with open('/etc/grub.conf', 'w') as f:
    for l in lines:
        f.write(l)

Comments