PowerShell中,读/写SSH.NET流PowerShell、SSH、NET

2023-09-04 00:20:48 作者:乖一点就抱你

我想通过SSH连接到服务器,使用PowerShell,,然后更改为不同的用户

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连接。这是伟大的,给了我简单的命令行开关,例如新sshsession 和调用-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.

接下来,我想的苏的给不同的用户。当我试图做到这一点通过命令行开关,我得到了错误:标准必须是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.

我知道我可以在服务器上修改安全设置,让我的苏导入用户,将不会在交互模式之中。但是,我不能所有我想连接到服务器上更改该设置。

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.

工作掀起了C#示例我发现了在 HTTPS的://sshnet.$c$cplex .COM /讨论/ 285853 ,我放在一起:

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.

你看我在做什么错了?

推荐答案

通过一个示例从https://sshnet.$c$cplex.com/discussions/439210,我能解决我的问题,下面code。主要的问题是,我是创建两个不同的流输入/输出,我需要只使用一个流。

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()
 
精彩推荐
图片推荐