Skip to content

Instantly share code, notes, and snippets.

@aarqon
Created March 8, 2017 20:57
Show Gist options
  • Save aarqon/4b2dcf7a4f83f1d91af646103edb38c9 to your computer and use it in GitHub Desktop.
Save aarqon/4b2dcf7a4f83f1d91af646103edb38c9 to your computer and use it in GitHub Desktop.
Makes a fancy /etc/motd
import subprocess
import re
last_re = re.compile('\s{2,}')
ip_re = re.compile('\d+\.\d+\.\d+\.\d+')
temp = open("/etc/motd-base",'r')
base = temp.read()
temp.close()
temp = open("/etc/hostname",'r')
hostname = temp.read().strip().rstrip()
temp.close()
ip = ' '.join(ip_re.findall(subprocess.check_output(['hostname','-I'])))
uptime = subprocess.check_output(['uptime','-p']).strip()
# Gets output of 'df -h /dev/sdc1', trims it down to just
# the first result and splits it into an array to use for
# the following string submission
#storage = subprocess.check_output(['df','-h','/dev/sdb1']).strip().splitlines()[1:][0].split()
#storage = "{0}/{1} ({2})".format(storage[2],storage[1],storage[4])
last = last_re.sub(':::',subprocess.check_output(['last -R | head -n 1'], shell=True).strip()).split(':::')
#print(last)
last = "{0} :: {1}".format(last[0],last[2])
if len(hostname) < 15:
pad = (15 - len(hostname))/2
hostname = ' '*pad+hostname+' '*pad
else:
hostname = hostname[:15]
#print(hostname)
#print(last)
#print(ip)
#print(uptime)
#print(storage)
base = base.replace("( %hostname )",hostname)
base = base.replace("%last-login",last)
base = base.replace("%IP",ip)
base = base.replace("%uptime",uptime)
#base = base.replace("%storage-full",storage)
#print(base)
temp = open('/etc/motd','w')
temp.write(base)
temp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment