# SSH 隧道 - 使用 Posh-SSH 自动输入密码 # 首次运行需安装: Install-Module Posh-SSH -Scope CurrentUser $hostName = "www.yuxindazhineng.com" $port = 2223 $user = "yxd" $password = "yuxindazn001" $localPort = 27017 $remotePort = 27017 if (-not (Get-Module -ListAvailable -Name Posh-SSH)) { Write-Host "正在安装 Posh-SSH 模块(仅需一次)..." Install-Module Posh-SSH -Scope CurrentUser -Force } $secPass = ConvertTo-SecureString $password -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($user, $secPass) Write-Host "正在建立 SSH 隧道..." $session = New-SSHSession -ComputerName $hostName -Port $port -Credential $cred -AcceptKey -Force if (-not $session) { Write-Host "SSH 连接失败" pause exit 1 } $forward = New-SSHLocalPortForward -Index 0 -LocalAddress 127.0.0.1 -LocalPort $localPort -RemoteAddress localhost -RemotePort $remotePort Write-Host "SSH 隧道已建立 (localhost:$localPort -> 远程:$remotePort)" Write-Host "保持此窗口打开,关闭即断开" pause