需要帮助自动化Putty / Plink

时间:2018-06-09 04:31:11

标签: vb.net ssh putty

出于某种目的,我需要通过SSH运行以下命令。但经过几个小时的尝试......没有什么可以帮助我的。

这是我需要使用plink(putty cmd version)执行的命令

  1. -ssh -P {Port} root @ {IP} -pw {PW} //这将直接登录ssh
  2. sftp -P {Port} root @ {Host} //这将在下一步中提出密码
  3. 我需要在这里传递密码
  4. 把/DL/1.test /home1/user/public_html/DL/1.test //这会上传文件并显示进度。
  5. 再见//从sftp
  6. 退出
  7. 退出//从ssh
  8. 批出

    我在vb.net中尝试过以下代码,但只有第1步可以挂起:(

    Private Sub cmdTest_Click(sender As System.Object, e As System.EventArgs) Handles cmdTest.Click
        Dim objProcess As System.Diagnostics.Process
        intProcState = 0
        objProcess = New System.Diagnostics.Process
        objProcess.StartInfo.FileName = "c:\Program Files\PuTTY\plink.exe"
        objProcess.StartInfo.Arguments = "-ssh -P {Port} root@{Host} -pw {PW}"
        objProcess.StartInfo.UseShellExecute = False
        objProcess.StartInfo.CreateNoWindow = False
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        objProcess.StartInfo.RedirectStandardError = True
        objProcess.StartInfo.RedirectStandardInput = True
        objProcess.StartInfo.RedirectStandardOutput = True
        AddHandler objProcess.OutputDataReceived, AddressOf consoleOutputHandler
        AddHandler objProcess.ErrorDataReceived, AddressOf consoleErrorHandler
        objProcess.Start()
        objProcIn = objProcess.StandardInput
        objProcess.BeginOutputReadLine()
        objProcess.BeginErrorReadLine()
        objProcess.WaitForExit()
        objProcess.Dispose()
        objProcess = Nothing
        MessageBox.Show("done")
    End Sub
    
    Sub consoleOutputHandler(ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(outLine.Data) Then
            If outLine.Data.IndexOf("Last login:") <> -1 Then
                intProcState = 0
                sRunCommand()
            End If
            If outLine.Data = "Connected to {sftp host}." Then
                intProcState += 1
                sRunCommand()
            End If
            If outLine.Data.Substring(outLine.Data.Length - 2) = "# " Then
                intProcState += 1
                sRunCommand()
            End If
        End If
    End Sub
    
    Private Sub sRunCommand()
        Select Case intProcState
            Case 0
                objProcIn.WriteLine("sftp -P {Port} user@{Host}")
                System.Threading.Thread.Sleep(2000)
                objProcIn.WriteLine("{PW}")
            Case 1
                objProcIn.WriteLine("put DL/test.test /home1/user/public_html/DL/test.test")
            Case 2
                objProcIn.WriteLine("bye")
            Case 3
                objProcIn.WriteLine("logout")
        End Select
    End Sub
    

    有没有更好的方法来处理它?<​​/ p>

    我知道chilkatsoft SSH可以处理它,但我对该组件非常新手。

    提前致谢

    最好的问候

0 个答案:

没有答案