使用sudo执行SSH命令行,然后输入密码

时间:2020-06-05 13:51:27

标签: vb.net ssh ssh.net

我一直试图在VB.net上发送一条需要sudo权限的简单行。我无法在SSH请求中使用Expect,因为它在我的服务器(Synology)上不可用

我尝试了两种选择:

Dim coString As New Renci.SshNet.PasswordConnectionInfo(serv, port, name, dsPass)
Dim sshClient As New Renci.SshNet.SshClient(coString)
Dim cmd1 As Renci.SshNet.SshCommand

Dim commandString As String = "echo ""plaintext_key"" | sudo -S synoshare --enc_mount genericFolderName myPassphrase"

Try
    sshClient.Connect()
    test.progress.Value = 10
    cmd1 = sshClient.RunCommand(commandString)
    sshClient.Disconnect()

Catch ex As Exception
    MsgBox(ex.Message)
End Try

这只是在几秒钟后跳过sshClient.RunCommand(commandString),但是什么也没做。

Dim coString As New Renci.SshNet.PasswordConnectionInfo(serv, port, name, dsPass)
Dim sshClient As New Renci.SshNet.SshClient(coString)
Dim modes As New Dictionary(Of Renci.SshNet.Common.TerminalModes, UInt32)

Try
    sshClient.Connect()
    Dim stream = sshClient.CreateShellStream("xterm", 255, 50, 800, 600, 1024, modes)
    Using stream
        test.progress.Value = 10
        stream.Write("sudo synoshare --enc_unmount genericFolderName")
        stream.Expect("Password: ")
        stream.Write(dsPass)
    End Using
    sshClient.Disconnect()

Catch ex As Exception
        MsgBox(ex.Message)
End Try

这个人永远被卡在stream.Expect("Password: ")上。我试图用Password:Password:替换password(在SSH控制台中键入时写的内容),但是它没有任何改变。

0 个答案:

没有答案