Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$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:
1 2 3 |
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?
Answer:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
$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() |