在英文论坛找到一个UDF SFTPEX.au3.是调用PSFTP实现登陆SFTP的。但在实际运行的过程中会程式卡主没反应。#include <SFTPEx.au3>
#include <Array.au3>
; 修改以下变量为你的FTP服务器信息和登录凭据
$ftpServer = "xxx.xxx.xxx.xxx" ; FTP服务器地址
$ftpUsername = "xxxx" ; FTP用户名
$ftpPassword = "xxxxx" ; FTP密码
;If ProcessExists("psftp.exe") Then ProcessClose("psftp.exe")
Sleep(1000)
$hSession=_SFTP_Open()
If @error Then
MsgBox(0,0,"Open PSFTP.exe error!!")
EndIf
$hConnection=_SFTP_Connect($hSession, $SFTP, $SFTP_User, $SFTP_Password,22)
If @error Then
_MsgBox(0,0,"Connect the SFTP["&$ftpServer&"] by PSFTP.exe fail!!")
EndIf
MsgBox(0,0,"OK!!")
下面是UDF下载链接和PSFTP.exe 链接。
UDF to support SFTP protocol using PSFTP - AutoIt Example Scripts - AutoIt Forums
追查发现是$sLine = StdoutRead($hSession) 这一句读取不到数据。 大神帮忙看看这个问题如何解?Func _SFTP_Connect($hSession, $sServerName, $sUsername = "", $sPassword = "", $iServerPort = 0)
If ProcessExists($hSession) = 0 Then
Return SetError(1, 0, 0)
EndIf
If $iServerPort = 0 Then
$iServerPort = ""
EndIf
Local $sLine, $sStringSplit, $iWaitKeySaving = 0, $bSaveKey = True
StdinWrite($hSession, 'open ' & $sServerName & ' ' & $iServerPort & @CRLF)
Local $Ltimeout = 0 ; NSC modified 10ms * 1000 = 10.000 = 10 seconds timeout
While 1
$Ltimeout += 1 ; NSC modified
$iWaitKeySaving += 1
If $iWaitKeySaving >= 500 And $bSaveKey Then
StdinWrite($hSession, 'y' & @CRLF)
$bSaveKey = False
EndIf
<span style="background-color: yellow;"><font color="#ff0000"> $sLine = StdoutRead($hSession)</font></span>
If ProcessExists($hSession) = 0 Then
Return SetError(1, 0, 0)
ElseIf StringInStr($sLine, "psftp>") Then
ExitLoop
ElseIf StringInStr($sLine, "login as:") Then
StdinWrite($hSession, $sUsername & @CRLF)
While 1
$sLine = StdoutRead($hSession)
If ProcessExists($hSession) = 0 Then
Return SetError(1, 0, 0)
ElseIf StringInStr($sLine, "psftp>") Then
ExitLoop 2
ElseIf StringInStr($sLine, "password:") Then
StdinWrite($hSession, $sPassword & @CRLF)
ExitLoop 2
EndIf
Sleep(10)
WEnd
ElseIf $sLine <> "" Then
Return SetError(3, 0, 0)
EndIf
Sleep(10)
If $Ltimeout > 1000 Then ; NSC modified
Return SetError(4, 0, 0); NSC generic timeout
EndIf
WEnd
If $sLine <> "psftp>" Then ; Connection With User And Password.
$Ltimeout = 0 ; NSC modified 10ms * 1000 = 10.000 = 10 seconds timeout
While 1
$Ltimeout += 1 ; NSC modified
$sLine = StdoutRead($hSession)
If ProcessExists($hSession) = 0 Then
Return SetError(1, 0, 0)
ElseIf StringInStr($sLine, "psftp>") Then
ExitLoop
ElseIf StringInStr($sLine, "Access denied") Then
Return SetError(2, 0, 0) ; The Password Is Required Again.
EndIf
Sleep(10)
If $Ltimeout > 1000 Then ; NSC modified
Return SetError(5, 0, 0) ; NSC pass not valid
EndIf
WEnd
EndIf
If StringInStr($sLine, "Remote working directory is") Then
$sStringSplit = StringSplit($sLine, @CRLF)
$__gsRemoteDir_SFTP = StringTrimLeft($sStringSplit[1], 28)
EndIf
If $__gsRemoteDir_SFTP = 0 Then
$__gsRemoteDir_SFTP = _SFTP_DirGetCurrent($hSession)
EndIf
Return $hSession
EndFunc ;==>_SFTP_Connect
|