PowerShell,读/写 SSH.NET 流 [英] PowerShell, read/write to SSH.NET streams
问题描述
我想使用 PowerShell 通过 SSH 连接到服务器,然后更改为其他用户.
I would like to connect to a server via SSH, with PowerShell, and then change to a different user.
为了通过 SSH 使用 PowerShell 连接到服务器,我使用了 SSH 从 PowerShell 使用SSH.NET 库 和 如何从 powershell 通过 SSH 进行连接.这很棒,让我可以使用简单的命令行开关,例如 new-sshsession 和 invoke-sshcommand.
In order to accomplish connecting to a server with PowerShell via SSH, I used SSH from PowerShell using the SSH.NET library and How to connect via SSH from powershell. That was great and gave me simple commandlets such as new-sshsession and invoke-sshcommand to work with.
接下来,我想su给不同的用户.当我尝试通过命令行开关执行此操作时,出现错误:standard in must be a tty.
Next, I wanted to su to a different user. When I tried to do that via the commandlets, I got the error: standard in must be a tty.
我知道我可以编辑服务器上的安全设置,让我在不处于交互模式的情况下su给用户.但是,我无法在要连接的所有服务器上更改该设置.
I understand that I can edit the security settings on the server to let me su to a user without being in interactive mode. However, I can't change that setting on all the servers I want to connect to.
处理我在 https://sshnet.codeplex.com/discussions/285853 上找到的 C# 示例,我放在一起:
Working off of a C# example I found at https://sshnet.codeplex.com/discussions/285853, I put together:
$server = "server1"
$port = 22
$username = "user1"
$password = "password1"
$ssh = new-object Renci.SshNet.SshClient($server, $port, $username, $password)
$ssh.Connect()
$inputstream = new-object System.IO.MemoryStream
$streamwriter = new-object System.IO.StreamWriter($inputstream)
$outputstream = new-object System.IO.MemoryStream
$streamreader = new-object System.IO.StreamReader($outputstream)
$shell = $ssh.CreateShell($inputstream, $outputstream, $outputstream)
$shell.Start()
$streamwriter.WriteLine("echo hello > tempfile.txt")
$streamwriter.Flush()
$streamreader.ReadToEnd()
$shell.Stop()
正在运行,我得到如下输出:
Running, that I get output such as:
Last login: Fri Nov 8 11:37:45 2013 from 10.XXX.XXX.XXX
[user1@server1 /users/user1]
然而,/users/user1/tempfile.txt
永远不会被写入,我也没有得到任何进一步的输出.
However, /users/user1/tempfile.txt
never gets written and I do not get any further output.
你看到我做错了什么吗?
Do you see what I'm doing wrong?
推荐答案
使用来自 https://sshnet.codeplex.com 的示例/discussions/439210,我能够用下面的代码解决我的问题.主要问题是我为输入/输出创建了两个不同的流,而我只需要使用一个流.
Using an example from https://sshnet.codeplex.com/discussions/439210, I was able to solve my problem with the below code. The main issue was that I was creating two different streams for input/output and I needed to just use one stream.
$server = "server1"
$port = 22
$username = "user1"
$password = "password1"
###############################################################
function ReadStream($reader)
{
$line = $reader.ReadLine();
while ($line -ne $null)
{
$line
$line = $reader.ReadLine()
}
}
function WriteStream($cmd, $writer, $stream)
{
$writer.WriteLine($cmd)
while ($stream.Length -eq 0)
{
start-sleep -milliseconds 500
}
}
###############################################################
$ssh = new-object Renci.SshNet.SshClient($server, $port, $username, $password)
$ssh.Connect()
$stream = $ssh.CreateShellStream("dumb", 80, 24, 800, 600, 1024)
$reader = new-object System.IO.StreamReader($stream)
$writer = new-object System.IO.StreamWriter($stream)
$writer.AutoFlush = $true
while ($stream.Length -eq 0)
{
start-sleep -milliseconds 500
}
ReadStream $reader
WriteStream "su - root" $writer $stream
ReadStream $reader
WriteStream "password" $writer $stream
ReadStream $reader
WriteStream "pwd" $writer $stream
ReadStream $reader
$stream.Dispose()
$ssh.Disconnect()
$ssh.Dispose()
这篇关于PowerShell,读/写 SSH.NET 流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!