motd: rewrite sysctl_get function for better python 3 support
authorSofian Brabez <sbz@6dev.net>
Tue, 29 Dec 2020 17:48:26 +0000 (18:48 +0100)
committersbz <sbz@6dev.net>
Tue, 29 Dec 2020 17:48:26 +0000 (18:48 +0100)
motd.py

diff --git a/motd.py b/motd.py
index 77691d22c4d3877eb33380635737ffcef37a9879..5b57228c2c20341aea3141d8dc3a54aadabca88a 100644 (file)
--- a/motd.py
+++ b/motd.py
@@ -1,48 +1,69 @@
 import os
 import os
+import shlex
+import subprocess
 
 
-template="""%s
+
+template = r"""%s
  +--------------------------------------+           ,        ,
  |        Welcome to %s           |          /(        )`
  |                                      |          \ \___   / |
  |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|          /- _  `-/  '
  +--------------------------------------+           ,        ,
  |        Welcome to %s           |          /(        )`
  |                                      |          \ \___   / |
  |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|          /- _  `-/  '
- |                                      |         (/\/ \ \   /\\
- |  kern.hostname: %.8s               |         / /   | `    \\
+ |                                      |         (/\/ \ \   /\
+ |  kern.hostname: %.8s               |         / /   | `    \
  |    kern.ostype: %s              |         O O   ) /    |
  |    kern.ostype: %s              |         O O   ) /    |
- | kern.osrelease: %s     |         `-^--'`<     '
+ | kern.osrelease: %s          |         `-^--'`<     '
  |       hw.model: %.20s |        (_.)  _  )   /
  |     hw.machine: %s                |         `.___/`    /
  |        hw.ncpu: %s                    |           `-----' /
  |       hw.model: %.20s |        (_.)  _  )   /
  |     hw.machine: %s                |         `.___/`    /
  |        hw.ncpu: %s                    |           `-----' /
- | dev.cpu.0.freq: %s                <----.     __ / __   \\
+ | dev.cpu.0.freq: %s                <----.     __ / __   \
  |     hw.physmem: %s         <----|====O)))==) \) /====
  |     hw.physmem: %s         <----|====O)))==) \) /====
- |           inet: %s        <----'    `--' `.__,' \\
+ |           inet: %s        <----'    `--' `.__,' \
  |                                      |           |        |
  |          admin: %s         |            \       /
  |                                      |       ______( (_  / \______
  |                                      |           |        |
  |          admin: %s         |            \       /
  |                                      |       ______( (_  / \______
- |                                      |     ,'  ,-----'   |        \\
+ |                                      |     ,'  ,-----'   |        \
  +--------------------------------------+     `--{__________)        \/
 """
 
  +--------------------------------------+     `--{__________)        \/
 """
 
-def sysctl_get(oid_string):
-    result=""
+
+def run_command(cmd: str) -> str:
+    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
+    try:
+        out, err = p.communicate(timeout=10)
+
+    except subprocess.TimeoutExpired:
+        p.kill()
+        out, _ = p.communicate()
+
+    return out.strip()
+
+
+def sysctl_get(oid_string: str) -> str:
+    result = ""
     try:
         import freebsd
     try:
         import freebsd
-        result=freebsd.sysctl('%s' % oid_string)
-    except:
-        result=os.popen('sysctl -n %s' % oid_string).read().strip()
+        result = freebsd.sysctl('%s' % oid_string)
+    except ImportError:
+        cmd = 'sysctl -n {0}'.format(oid_string)
+        result = run_command(cmd)
     finally:
     finally:
-        return result
+        return str(result, 'utf-8')
+
 
 uname = " ".join(" ".join(os.uname()[:4]).split(' ')[:13])
 fqdn = "6dev.net"
 admin = "%s@%s" % (os.environ['USER'], fqdn)
 inet = os.popen("ifconfig|grep 'inet .* broad*'").read().split()[1]
 
 
 uname = " ".join(" ".join(os.uname()[:4]).split(' ')[:13])
 fqdn = "6dev.net"
 admin = "%s@%s" % (os.environ['USER'], fqdn)
 inet = os.popen("ifconfig|grep 'inet .* broad*'").read().split()[1]
 
-oids = ["kern.hostname", "kern.osrelease", "kern.ostype", "hw.model", "hw.machine",
-"hw.ncpu", "dev.cpu.0.freq", "hw.physmem"]
+oids = ["kern.hostname", "kern.osrelease", "kern.ostype", "hw.model",
+        "hw.machine", "hw.ncpu", "dev.cpu.0.freq", "hw.physmem"]
 
 for oid in oids:
 
 for oid in oids:
-    locals()[oid.replace('.','_')] = sysctl_get(oid)
+    locals()[oid.replace('.', '_')] = sysctl_get(oid)
+
+hw_physmem = "%.1fGB" % float(int(hw_physmem)/1000000000.0)
 
 
-print(template % (uname, fqdn, kern_hostname.split('.')[0], kern_ostype, kern_osrelease, hw_model,
-        hw_machine, hw_ncpu, dev_cpu_0_freq, "%.1fGB" %
-        float(int(hw_physmem)/1000000000.0), inet, admin))
+print(template % (uname, fqdn,
+                  kern_hostname.split('.')[0], kern_ostype, kern_osrelease,
+                  hw_model, hw_machine, hw_ncpu,
+                  dev_cpu_0_freq, hw_physmem, inet, admin))