motd: rewrite sysctl_get function for better python 3 support
[freebsd-maintainer-scripts/.git] / motd.py
CommitLineData
b2d0d87b 1import os
16f0ace7
SB
2import shlex
3import subprocess
b2d0d87b 4
16f0ace7
SB
5
6template = r"""%s
b2d0d87b
SB
7 +--------------------------------------+ , ,
8 | Welcome to %s | /( )`
9 | | \ \___ / |
10 |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| /- _ `-/ '
16f0ace7
SB
11 | | (/\/ \ \ /\
12 | kern.hostname: %.8s | / / | ` \
b2d0d87b 13 | kern.ostype: %s | O O ) / |
16f0ace7 14 | kern.osrelease: %s | `-^--'`< '
b2d0d87b
SB
15 | hw.model: %.20s | (_.) _ ) /
16 | hw.machine: %s | `.___/` /
17 | hw.ncpu: %s | `-----' /
16f0ace7 18 | dev.cpu.0.freq: %s <----. __ / __ \
b2d0d87b 19 | hw.physmem: %s <----|====O)))==) \) /====
16f0ace7 20 | inet: %s <----' `--' `.__,' \
b2d0d87b
SB
21 | | | |
22 | admin: %s | \ /
23 | | ______( (_ / \______
16f0ace7 24 | | ,' ,-----' | \
b2d0d87b
SB
25 +--------------------------------------+ `--{__________) \/
26"""
27
16f0ace7
SB
28
29def run_command(cmd: str) -> str:
30 p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
31 try:
32 out, err = p.communicate(timeout=10)
33
34 except subprocess.TimeoutExpired:
35 p.kill()
36 out, _ = p.communicate()
37
38 return out.strip()
39
40
41def sysctl_get(oid_string: str) -> str:
42 result = ""
b2d0d87b
SB
43 try:
44 import freebsd
16f0ace7
SB
45 result = freebsd.sysctl('%s' % oid_string)
46 except ImportError:
47 cmd = 'sysctl -n {0}'.format(oid_string)
48 result = run_command(cmd)
b2d0d87b 49 finally:
16f0ace7
SB
50 return str(result, 'utf-8')
51
b2d0d87b
SB
52
53uname = " ".join(" ".join(os.uname()[:4]).split(' ')[:13])
54fqdn = "6dev.net"
55admin = "%s@%s" % (os.environ['USER'], fqdn)
56inet = os.popen("ifconfig|grep 'inet .* broad*'").read().split()[1]
57
16f0ace7
SB
58oids = ["kern.hostname", "kern.osrelease", "kern.ostype", "hw.model",
59 "hw.machine", "hw.ncpu", "dev.cpu.0.freq", "hw.physmem"]
b2d0d87b
SB
60
61for oid in oids:
16f0ace7
SB
62 locals()[oid.replace('.', '_')] = sysctl_get(oid)
63
64hw_physmem = "%.1fGB" % float(int(hw_physmem)/1000000000.0)
b2d0d87b 65
16f0ace7
SB
66print(template % (uname, fqdn,
67 kern_hostname.split('.')[0], kern_ostype, kern_osrelease,
68 hw_model, hw_machine, hw_ncpu,
69 dev_cpu_0_freq, hw_physmem, inet, admin))