Files
inquiry_robot/deploy/_remote_try_local_mysql_pg.ps1
T

63 lines
2.8 KiB
PowerShell

$ErrorActionPreference = "Continue"
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
$psql = "C:\yutongda\tools\pgsql\bin\psql.exe"
$dbCred = Get-Content "C:\yutongda\secure\application-database-credentials.json" -Raw | ConvertFrom-Json
$pgpass = (Get-Content "C:\yutongda\secure\pgpass.txt" -Raw).Trim()
Write-Output "=== who owns local 3306/5432 ==="
foreach ($p in 3306,5432) {
$c = Get-NetTCPConnection -LocalPort $p -State Listen -EA SilentlyContinue | Select-Object -First 1
if ($c) {
$proc = Get-CimInstance Win32_Process -Filter "ProcessId=$($c.OwningProcess)" -EA SilentlyContinue
"$p pid=$($c.OwningProcess) name=$($proc.Name) path=$($proc.ExecutablePath)"
}
}
Write-Output "=== local mysql root/pgpass/jeecg ==="
$tests = @(
@{h='127.0.0.1'; u='root'; p=$pgpass; t='local_root_pgpass'},
@{h='127.0.0.1'; u='root'; p='root'; t='local_root_root'},
@{h='127.0.0.1'; u='root'; p=$dbCred.mysql_password; t='local_root_jeecgpass'},
@{h='127.0.0.1'; u=$dbCred.mysql_username; p=$dbCred.mysql_password; t='local_jeecg'},
@{h='localhost'; u='root'; p=$pgpass; t='localhost_root_pgpass'}
)
foreach ($t in $tests) {
$out = & $mysql -h $t.h -P 3306 -u $t.u "-p$($t.p)" --connect-timeout=5 -e "SELECT CURRENT_USER(); SHOW DATABASES;" 2>&1
$text = ($out | ForEach-Object { "$_" }) -join " | "
if ($text -match 'CURRENT_USER' -or $text -match 'Database') {
Write-Output "MYSQL_HIT $($t.t)"
# show db names only
$out | ForEach-Object {
$s = "$_"
if ($s -match 'Using a password') { return }
if ($s -match '^(Database|information_schema|mysql|performance_schema|sys|jeecg|ytd|inquiry|test)') { $s }
elseif ($s -match 'CURRENT_USER|root@|ytd_') { $s }
}
} else {
$why = if ($text -match 'Access denied') {'denied'} elseif ($text -match 'Can''t connect|refused') {'noconnect'} else {'fail'}
Write-Output "mysql_miss $($t.t) $why"
}
}
Write-Output "=== local postgres ==="
$pgTests = @(
@{h='127.0.0.1'; u='postgres'; p=$pgpass; t='local_postgres_pgpass'},
@{h='127.0.0.1'; u='postgres'; p=$dbCred.postgres_password; t='local_postgres_runtimepass'},
@{h='127.0.0.1'; u=$dbCred.postgres_username; p=$dbCred.postgres_password; t='local_runtime'},
@{h='127.0.0.1'; u=$dbCred.postgres_username; p=$pgpass; t='local_runtime_pgpass'}
)
foreach ($t in $pgTests) {
$env:PGPASSWORD = $t.p
$env:PGCONNECT_TIMEOUT = "5"
$out = & $psql -h $t.h -p 5432 -U $t.u -d postgres -tAc "SELECT current_user||','||rolsuper||','||rolcreatedb FROM pg_roles WHERE rolname=current_user" 2>&1
$text = ($out | ForEach-Object { "$_" }) -join " "
if ($text -match ',t,' -or $text -match ',t$') {
Write-Output "PG_HIT $($t.t) $text"
} elseif ($text -match ',') {
Write-Output "PG_HIT_LIMITED $($t.t) $text"
} else {
Write-Output "pg_miss $($t.t)"
}
}
Write-Output "LOCAL_TRY_DONE"