ssh 将端口作为参数传递时本地转发规范错误

问题描述 投票:0回答:1

我在 Windows PowerShell 的

.ps1
配置文件中创建了此函数:

function sshpc ([int]$port = 8888) {
    ssh -fNL [int]$port:localhost:[int]$port pc
}

但是端口转发命令不起作用。它打印消息:

错误的本地转发规范“[Int][Int]8888”

但是运行命令

ssh -fNL 8888:localhost:8888 pc
工作正常。
我怎样才能做到这一点?


我尝试过的:

我尝试使用参数定义:

($port)
([int]$port)
($port = 8888)
,但这些也失败了。
这个功能运行良好:

function sshpc {
    ssh -fNL 8888:localhost:8888 pc
}
windows powershell ssh port portforwarding
1个回答
0
投票

[int]$port:localhost:[int]$port

您不能以这种方式使用强制转换,而且它们也不是必需的,因为您只能将 strings 作为参数传递给外部程序(除此之外,

$port
已经是 [int] 类型的
is
)。

相反,使用(隐式)可扩展字符串来组成你的论点:

ssh -fNL ${port}:localhost:${port}
© www.soinside.com 2019 - 2024. All rights reserved.