-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_session.py
More file actions
executable file
·108 lines (91 loc) · 3.39 KB
/
monitor_session.py
File metadata and controls
executable file
·108 lines (91 loc) · 3.39 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.8"
# dependencies = []
# ///
import argparse
import sys
import urllib.parse
import webbrowser
from pathlib import Path
def main():
parser = argparse.ArgumentParser(
description="Connect to terminal monitoring session",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
monitor_session.py http://localhost:8888
monitor_session.py --browser firefox http://192.168.1.100:8888
monitor_session.py --no-open http://server.example.com:9999
"""
)
parser.add_argument(
"url",
help="Monitor server URL (http://host:port)"
)
parser.add_argument(
"--browser",
help="Browser to use (chrome, firefox, safari, default)"
)
parser.add_argument(
"--no-open",
action="store_true",
help="Don't auto-open browser, just show URL"
)
args = parser.parse_args()
# Validate URL format
parsed_url = urllib.parse.urlparse(args.url)
if not parsed_url.scheme or not parsed_url.netloc:
print(f"Error: Invalid URL format: {args.url}")
print("Expected format: http://hostname:port")
sys.exit(1)
# Ensure http scheme
if parsed_url.scheme not in ['http', 'https']:
print(f"Error: URL must use http:// or https:// scheme: {args.url}")
sys.exit(1)
if args.no_open:
print(f"Monitor URL: {args.url}")
print("Open this URL in your browser to view the terminal session.")
print("The monitor will show recent output and then live updates.")
else:
print(f"Opening monitor session: {args.url}")
success = False
if args.browser:
# Try to use specific browser
browser_map = {
'chrome': 'google-chrome',
'firefox': 'firefox',
'safari': 'safari',
'edge': 'microsoft-edge'
}
browser_cmd = browser_map.get(args.browser.lower(), args.browser)
try:
controller = webbrowser.get(browser_cmd)
controller.open(args.url)
success = True
print(f"Opened in {args.browser}")
except webbrowser.Error:
print(f"Warning: Could not open {args.browser}, trying default browser...")
if not success:
try:
webbrowser.open(args.url)
print("Opened in default browser")
success = True
except webbrowser.Error:
print("Error: Could not open browser automatically")
success = False
if success:
print("\nBrowser opened. If the session is active, you should see:")
print(" - Connection status at the top")
print(" - Recent terminal output (if joining mid-session)")
print(" - Live terminal updates")
print(" - Session information (shell, start time)")
print("\nPress Ctrl+C to exit this utility (won't affect the session).")
try:
input() # Keep utility running
except KeyboardInterrupt:
print("\nMonitor utility exiting.")
else:
print(f"\nManually open this URL in your browser: {args.url}")
if __name__ == "__main__":
main()