2016-05-30 116 views
0

我正在使用​​连接到我的ssh服务器,一些命令以su根开头,但我无法成功切换用户到root。如何使用posh-ssh(powershell)“sudo root”

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(); 

[[email protected] 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:\> 

如何以root身份运行我的命令?

回答

1

我在使用posh-ssh和ubuntu时注意到的是,由于ExpectString的原因,我无法使用“sudo su - ”来启动root。这是“对(用户名)[须藤]密码:”期待和我只是提供“密码:”

$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() 

这就是我如何能够sudo来根。根据你连接的nix变种,它可能会有所不同。