Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何";sudo root“;使用高级ssh(powershell)_Powershell_Ssh_Posh Ssh - Fatal编程技术网

如何";sudo root“;使用高级ssh(powershell)

如何";sudo root“;使用高级ssh(powershell),powershell,ssh,posh-ssh,Powershell,Ssh,Posh Ssh,我正在使用连接到我的ssh服务器。一些命令以su root开始,但我无法成功地将用户切换到root PS C:\> $rootpwdSec = ConvertTo-SecureString $rootpwd -AsPlainText -Force PS C:\> Invoke-SSHStreamExpectSecureAction -Command 'su ' -ExpectString 'Password:' -SecureAction $rootpwdSec -ShellStre

我正在使用连接到我的ssh服务器。一些命令以su root开始,但我无法成功地将用户切换到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();

[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:\>

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

在使用posh ssh和ubuntu时,我注意到,由于ExpectString,我无法使用“sudo su-”将sudo升级到root用户。它需要“[sudo]密码(用户名):”而我只是提供了“密码”:


这就是我能让sudo生根的原因。同样,根据您所连接的*nix变体的不同,它可能会有所不同

在使用posh ssh和ubuntu时,我注意到,由于ExpectString,我无法使用“sudo su-”将sudo升级到root。它需要“[sudo]密码(用户名):”而我只是提供了“密码”:

这就是我能让sudo生根的原因。同样,根据您所连接的*nix变体的不同,它可能会有所不同

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