Files
inquiry_robot/deploy/_remote_redis_auth_check.ps1
T

63 lines
2.0 KiB
PowerShell

# 用本机 Python 测 Redis AUTH(只打印 PONG/错误类型,不打印口令)
$ErrorActionPreference = 'Stop'
Get-Content 'E:\wwwroot\.env' | ForEach-Object {
$line = $_.Trim()
if (-not $line -or $line.StartsWith('#')) { return }
$i = $line.IndexOf('=')
if ($i -lt 1) { return }
Set-Item -Path ("Env:" + $line.Substring(0,$i).Trim()) -Value $line.Substring($i+1).Trim().Trim('"')
}
$py = 'C:\yutongda\tools\Python312\python.exe'
$code = @'
import os, socket, sys
url = os.environ.get("REDIS_URL","")
host = os.environ.get("YTD_REDIS_HOST","10.206.0.14")
port = int(os.environ.get("YTD_REDIS_PORT","6379"))
user = os.environ.get("YTD_REDIS_USERNAME","")
pwd = os.environ.get("YTD_REDIS_PASSWORD","")
# parse url if needed
if url.startswith("redis://"):
rest = url[len("redis://"):]
if "@" in rest:
cred, hp = rest.split("@",1)
if ":" in cred:
u,p = cred.split(":",1)
if u: user = u
pwd = p
else:
pwd = cred
host = hp.split("/")[0].split(":")[0]
if ":" in hp.split("/")[0]:
port = int(hp.split("/")[0].split(":")[1])
def send(auth_cmd):
s = socket.create_connection((host, port), 5)
s.sendall(auth_cmd)
data = s.recv(256)
s.close()
return data
# RESP AUTH
def resp_auth_password(p):
p = p.encode()
return b"*2\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n" % (len(p), p)
def resp_auth_user(u, p):
u=u.encode(); p=p.encode()
return b"*3\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n" % (len(u), u, len(p), p)
print("host=%s port=%s user_len=%s pwd_len=%s" % (host, port, len(user or ""), len(pwd or "")))
try:
r1 = send(resp_auth_password(pwd or ""))
print("auth_password:", r1[:80])
except Exception as e:
print("auth_password_err", type(e).__name__, str(e)[:80])
if user:
try:
r2 = send(resp_auth_user(user, pwd or ""))
print("auth_user:", r2[:80])
except Exception as e:
print("auth_user_err", type(e).__name__, str(e)[:80])
'@
$code | & $py -