File: elevated_server.py

package info (click to toggle)
pyro5 5.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,124 kB
  • sloc: python: 14,328; makefile: 161; sh: 66; javascript: 62
file content (30 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import subprocess
import Pyro5.api


class DmesgServer:
    @Pyro5.api.expose
    def dmesg(self):
        # reading last 20 lines of the kernel's dmesg buffer... (requires root privilege)
        try:
            result = subprocess.check_output(["dmesg", "--nopager", "--level", "info"])
            return result.decode().splitlines()[-20:]
        except subprocess.SubprocessError as x:
            raise OSError("couldn't run the dmesg command in the server: " + str(x))


if __name__ == "__main__":
    print("Server is running as:")
    print(" uid/gid", os.getuid(), os.getgid())
    print(" euid/egid", os.geteuid(), os.getegid())

    if os.getuid() != 0:
        print("Warning: lacking root privileges to run the 'dmesg' command to read the kernel's buffer. "
              "Executing the command will fail. For the desired outcome, run this program as root.")
    else:
        print("Running as root. This is okay as we're just running the 'dmesg' command for you.")

    Pyro5.api.serve({
        DmesgServer: "dmesg"
    }, host="localhost", use_ns=False)