nuno 修订了这个 Gist 1 month ago. 转到此修订
1 file changed, 32 insertions
gistfile1.txt(文件已创建)
| @@ -0,0 +1,32 @@ | |||
| 1 | + | def net_resolve(file, name): | |
| 2 | + | """Resolve service name to ip or ip:port:proto lines""" | |
| 3 | + | data = _net_read(file) | |
| 4 | + | if ':' in name: | |
| 5 | + | svc_name, port_name = name.split(':', 1) | |
| 6 | + | if svc_name not in data: | |
| 7 | + | print(f"Error: Service not found: {svc_name}", file=sys.stderr) | |
| 8 | + | sys.exit(1) | |
| 9 | + | svc = data[svc_name] | |
| 10 | + | ip = svc.get('ip', '') | |
| 11 | + | if port_name == 'ports': | |
| 12 | + | # All ports | |
| 13 | + | for pname, pdef in svc.get('ports', {}).items(): | |
| 14 | + | print(f"{ip}:{pdef['port']}:{pdef.get('proto','tcp')}") | |
| 15 | + | else: | |
| 16 | + | if port_name not in svc.get('ports', {}): | |
| 17 | + | print(f"Error: Port not found: {port_name}", file=sys.stderr) | |
| 18 | + | sys.exit(1) | |
| 19 | + | pdef = svc['ports'][port_name] | |
| 20 | + | print(f"{ip}:{pdef['port']}:{pdef.get('proto','tcp')}") | |
| 21 | + | else: | |
| 22 | + | if name not in data: | |
| 23 | + | print(f"Error: Service not found: {name}", file=sys.stderr) | |
| 24 | + | sys.exit(1) | |
| 25 | + | print(data[name].get('ip', '')) | |
| 26 | + | ||
| 27 | + | ||
| 28 | + | function net::resolve() { | |
| 29 | + | local name="${1:?}" | |
| 30 | + | json::net_resolve "$(ctx::net)" "$name" | |
| 31 | + | } | |
| 32 | + | ||
上一页
下一页