@@ -12,30 +12,47 @@ def get_public_key():
1212SSH_PUB_PATH = SSH_PK_PATH + ".pub"
1313SSH_PUB_STR = get_public_key ()
1414
15- ansi_escape_pattern = re .compile (r'\x1B[@-_][0-?]*[ -/]*[@-~]' )
1615
17- # Remove ANSI escape sequences
1816
17+ ansi_escape_pattern = re .compile (r"\x1B[@-_][0-?]*[ -/]*[@-~]" )
18+
19+
20+ def _text (stream : bytes | str | None ) -> str :
21+ if stream is None :
22+ return ""
23+ if isinstance (stream , bytes ):
24+ return stream .decode (errors = "replace" )
25+ return str (stream )
26+
27+
28+ def _format_result (result : asyncssh .SSHCompletedProcess ) -> str :
29+ return f"{ _text (result .stdout )} { _text (result .stderr )} " .strip ()
30+
31+
32+ async def test_ssh (host : str ,port : int ,username : str , key_path : str ) -> str :
33+ async with asyncssh .connect (host ,port = port ,username = username ,client_keys = [key_path ],known_hosts = None ,connect_timeout = 10 ,encoding = "utf-8" ) as conn :
34+ result = await conn .run ("cat /opt/hiddify-manager/VERSION" , check = False )
35+ out = _format_result (result )
36+ print (f"VERSION (exit { result .exit_status } ):\n { out } \n " )
37+
38+ try :
39+ status = await conn .run ("/opt/hiddify-manager/status.sh" , check = False )
40+ cleaned = ansi_escape_pattern .sub ("" , _text (status .stdout ))
41+ cleaned = cleaned .replace (" " , " " )
42+ cleaned = cleaned .replace ("-----------------------------------" , " " )
43+ out += f"\n ```bash\n { cleaned } \n { _text (status .stderr )} ```"
44+ print (f"status.sh (exit { status .exit_status } ):\n { cleaned } \n " )
45+ except Exception as e :
46+ out += f"\n Error running status.sh: { e } "
47+ print (f"status.sh failed: { e } " )
48+
49+ return out
1950
2051async def test_ssh_connection (ssh_info ):
2152 if not ssh_info :
2253 return False
23- print ("TEST" )
2454 try :
25- async with asyncssh .connect (
26- ssh_info ["host" ], port = ssh_info ["port" ], username = ssh_info ["user" ], client_keys = [SSH_PK_PATH ], known_hosts = None , connect_timeout = 2
27- ) as conn :
28- # result = await conn.run("pip3 freeze | grep hiddifypanel | awk -F ' == ' '{ print $2 }'")
29- result = await conn .run ("cat /opt/hiddify-manager/VERSION" )
30- out = f"{ result .stdout } { result .stderr } " .strip ()
31- try :
32- status = await conn .run ("/opt/hiddify-manager/status.sh" )
33- out += f"\n ```bash\n { ansi_escape_pattern .sub ('' , status .stdout )} \n { status .stderr } ```"
34- except Exception as e :
35- out += (f"Error: { e } " )
36- print ("SUCCESS" )
37- return f'{ out } '
38- return "WTF?"
55+ return await test_ssh (ssh_info ["host" ], ssh_info ["port" ], ssh_info ["user" ], SSH_PK_PATH )
3956 except Exception as e :
4057 print (f"Error: { e } " )
4158 return False
0 commit comments