0

i am using posh-ssh to connect to my ssh server do some commands start with su root,but i can not switch user to root sucessfully.

PS C:\> $rootpwdSec = ConvertTo-SecureString $rootpwd -AsPlainText -Force
PS C:\> Invoke-SSHStreamExpectSecureAction -Command 'su ' -ExpectString 'Password:' -SecureAction $rootpwdSec -ShellStream $stream
True
PS C:\> $stream.read();

[root@aaaaaa-test admin]#
PS C:\> Invoke-SSHCommandStream  -SessionId $SessionId -Command 'id'
uid=500(admin) gid=500(admin) groups=500(admin) context=user_u:system_r:unconfined_t
PS C:\>

how can run my command as root?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ncowboy
  • 1,311
  • 2
  • 13
  • 19

1 Answers1

2

What I noticed when working with posh-ssh and ubuntu was I was failing to sudo up to root using "sudo su -" due to the ExpectString. It was expecting "[sudo] password for (username):" and I was merely providing "password:"

$stream = $session.Session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 100)
$user = Invoke-SSHCommand $session -Command "whoami"
$SSHusersName = $user.Output | Out-String
$SSHusersName = $SSHusersName.Trim()
$results = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo su -" -ExpectString "[sudo] password for $($SSHusersName):" -SecureAction $secpas
$stream.Read()

This was how I was able to sudo to root. Again it may be different for you depending on what *nix variant you are connecting to.

Chris C
  • 21
  • 3