I would like to connect to a server via SSH, with PowerShell, and then change to a different user.
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.
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.
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]
However, /users/user1/tempfile.txt
never gets written and I do not get any further output.
Do you see what I'm doing wrong?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With