Powershell 中的管道化字符串

发布于 2024-10-11 17:10:55 字数 965 浏览 3 评论 0原文

我正在尝试创建一个简单的 PowerShell 函数来执行 Linux 风格的 ssh 命令。如:

ssh username@url

我使用plink来做到这一点,这是我编写的函数:

function ssh {
    param($usernameAndServer)

    $myArray = $usernameAndServer.Split("@")

    $myArray[0] | C:\plink.exe -ssh $myArray[1]
}

如果用户输入正确,$myArray[0]是用户名,$myArray [1] 是网址。因此,它连接到 URL,当提示您输入用户名时,用户名将使用管道流式传输。一切都运行良好,除了管道不断输入用户名($myArray[0])并且一遍又一遍地输入它作为密码。示例:

PS C:\Users\Mike> ssh xxxxx@yyyyy
login as: xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyy's password:
Access denied
xxxxx@yyyyy's password:
FATAL ERROR: Server sent disconnect message
type 2 (protocol error):
"Too many authentication failures for xxxxx"

用户名已替换为 xxxxx,URL 已替换为 yyyyy。

基本上,我需要找出如何在用户名 ($myArray[0]) 输入一次后停止脚本的管道传输。

有什么想法吗?我在互联网上寻找解决方案,但没有找到任何东西。

I'm trying to make a simple PowerShell function to have a Linux-style ssh command. Such as:

ssh username@url

I'm using plink to do this, and this is the function I have written:

function ssh {
    param($usernameAndServer)

    $myArray = $usernameAndServer.Split("@")

    $myArray[0] | C:\plink.exe -ssh $myArray[1]
}

If entered correctly by the user, $myArray[0] is the username and $myArray[1] is the URL. Thus, it connects to the URL and when you're prompted for a username, the username is streamed in using the pipeline. Everything works perfectly, except the pipeline keeps feeding the username ($myArray[0]) and it is entered as the password over and over. Example:

PS C:\Users\Mike> ssh xxxxx@yyyyy
login as: xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyyy's password:
Access denied
xxxxx@yyyy's password:
Access denied
xxxxx@yyyyy's password:
FATAL ERROR: Server sent disconnect message
type 2 (protocol error):
"Too many authentication failures for xxxxx"

Where the username has been substituted with xxxxx and the URL has been substituted with yyyyy.

Basically, I need to find out how to stop the script from piping in the username ($myArray[0]) after it has been entered once.

Any ideas? I've looked all over the internet for a solution and haven't found anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2024-10-18 17:10:55

plink 不允许您在一个参数中同时指定用户和主机吗?即:

plink -ssh user@host

如果是的话,您的 ssh 功能可以缩减为:

function ssh {
    param($usernameAndServer)

    C:\plink.exe -ssh $usernameAndServer
}

doesn't plink allow you to specify the user and host together in one argument? that is:

plink -ssh user@host

if so your ssh function could be whittled down to:

function ssh {
    param($usernameAndServer)

    C:\plink.exe -ssh $usernameAndServer
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文