James Feist | 7248e96 | 2020-09-10 11:10:47 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # Copyright 2020 Intel Corp. |
| 5 | |
| 6 | import subprocess |
| 7 | from flask import Flask |
| 8 | from flask import send_file |
| 9 | import argparse |
| 10 | |
| 11 | REPLACE_CHAR = '~' |
| 12 | |
| 13 | app = Flask(__name__) |
| 14 | |
| 15 | parser = argparse.ArgumentParser(description='Remote DBus Viewer') |
| 16 | parser.add_argument('-u', '--username', default='root') |
| 17 | parser.add_argument('-p', '--password', default='0penBmc') |
| 18 | parser.add_argument('-a', '--address', required=True) |
| 19 | parser.add_argument('-x', '--port', required=True) |
| 20 | args = parser.parse_args() |
| 21 | |
| 22 | busctl = 'sshpass -p {} busctl -H {}@{} '.format( |
| 23 | args.password, args.username, args.address) |
| 24 | header = '<head><link rel="icon" href="https://avatars1.githubusercontent.com/u/13670043?s=200&v=4" /></head>' |
| 25 | |
| 26 | |
| 27 | def getBusNames(): |
| 28 | out = subprocess.check_output(busctl + 'list --acquired', shell=True) |
| 29 | out = out.split(b'\n') |
| 30 | out = out[1:] |
| 31 | names = [] |
| 32 | for line in out: |
| 33 | name = line.split(b' ')[0] |
| 34 | if name: |
| 35 | names.append(name.decode()) |
| 36 | return names |
| 37 | |
| 38 | |
| 39 | def doTree(busname): |
| 40 | out = subprocess.check_output(busctl + 'tree ' + busname, shell=True) |
| 41 | out = out.split(b'\n') |
| 42 | tree = [] |
| 43 | for line in out: |
| 44 | path = line.split(b'/', 1)[-1].decode() |
| 45 | path = '/' + path |
| 46 | tree.append(path) |
| 47 | return tree |
| 48 | |
| 49 | |
| 50 | def doIntrospect(busname, path): |
| 51 | out = subprocess.check_output( |
| 52 | busctl + 'introspect {} {}'.format(busname, path), shell=True) |
| 53 | return out.decode().split('\n') |
| 54 | |
| 55 | |
| 56 | @app.route('/') |
| 57 | def root(): |
| 58 | out = header |
| 59 | out += '<div><H2>Bus Names {}</H2></div>'.format(args.address) |
| 60 | for name in getBusNames(): |
| 61 | out += '<div> ' |
| 62 | out += '<a href="{}"> {} </a>'.format(name, name) |
| 63 | out += '</div>' |
| 64 | return out |
| 65 | |
| 66 | |
| 67 | @app.route('/favicon.ico') |
| 68 | def favicon(): |
| 69 | return '<link rel="icon" type="image/png" href="https://avatars1.githubusercontent.com/u/13670043?s=200&v=4" />' |
| 70 | |
| 71 | |
| 72 | @app.route('/<name>') |
| 73 | def busname(name): |
| 74 | out = header |
| 75 | out += '<div><H2>tree {}</H2></div>'.format(name) |
| 76 | for path in doTree(name): |
| 77 | out += '<div> ' |
| 78 | out += '<a href="{}/{}"> {} </a>'.format( |
| 79 | name, path.replace('/', REPLACE_CHAR), path) |
| 80 | out += '</div>' |
| 81 | return out |
| 82 | |
| 83 | |
| 84 | @app.route('/<name>/<path>') |
| 85 | def path(name, path): |
| 86 | path = path.replace(REPLACE_CHAR, '/') |
| 87 | out = header |
| 88 | out += '<div><H2>introspect {} {}</H2></div>'.format(name, path) |
| 89 | for line in doIntrospect(name, path): |
| 90 | out += '<div> ' |
| 91 | out += '<pre> {} </pre>'.format(line) |
| 92 | out += '</div>' |
| 93 | return out |
| 94 | |
| 95 | |
| 96 | app.run(port=args.port, host='0.0.0.0') |