Lupo73

UDF to support SFTP protocol using PSFTP

36 posts in this topic

Note that I forgot to write an answer, but the last "red corrections" are implemented in the code.


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites



i like it when it all works the first time :)

very nice work Adventurer -- you made it really easy to use psftp

1 person likes this

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites

#23 ·  Posted (edited)

Hi Lupo73,

I have been doing some tests with your SFTPEx UDF and I find it very useful. I appreciate that you are the first one to come up with a UDF covering SFTP, and you've got my :thumbsup:  for this.

I found an inconsistency in _SFTP_FileMove which I tried to correct, see code hereunder.

The first issue occurred while trying to rename a file to an already existing one, then the function did not return an error, but the file was not renamed.

The second issue occurred while moving (multiple) files to a directory using wildcards, and one of the files already existed in the destination directory.

I reshuffled the If/Else loop so that it tackles all error situations and I believe it captures all errors now.

I added extra error return values, I am not sure if Error 5 is still needed but I left it there, it doesn't harm anyway.

Correct Version (added dummy stdoutread at the end so that other functions are not affected by the remaining data in the buffer,  sorry again)

Update 12/12/2013

New issue found when trying to move multiple files with wildcards.

In some cases, for example when using wildcards, file processing time increases, StdoutRead could still be empty, so i added a loop waiting fro the 'psftp>' prompt before going forward with error checking.

One possible issue: if the prompt never comes, the function will ends into an indefinite loop.  I have no solution for this but adding a time-out as a parameter would be a partial answer.  Question is then 'which time out should be used?'

You will also notice that I empty the console buffer before and at the end of the function. I noticed that  previous failed function, like an error on creating a directory will abort the process with an error but do not clear the console buffer.  I would recommend to always clear the console on exit of a function.

; #FUNCTION# ====================================================================================================================
; Name...........: _SFTP_FileMove
; Description ...: Move/Rename a file on a SFTP server.
; Syntax.........: _SFTP_FileMove ( $hConnection, $sSourceFile, $sDestinationFile )
; Parameters ....: $hConnection - as returned by _SFTP_Connect().
;                  $sSourceFile - The source file to move/rename. Wildcards are allowed if move to a directory
;                  $sDestinationFile - The destination file or directory to move/rename.
; Return values .: Success - 1
;                  Failure - 0, sets @error
;                  |1 - The connection is closed
;                  |2 - File not found
;                  |3 - Failure to rename or move a file. In case of wildcards, failure to move at least one file.
;                  |4 - no file name matched the source file name(s)
;                  |5 - Other error
; Author ........: Lupo73, GreenCan
; Modified.......: Corrected issue while trying to rename a file to a new file that already exists
;                   Issue with StdoutRead being empty still, causing malfunction of _SFTP_FileMove, added wait loop.
; Remarks .......:
; Related .......: _SFTP_Connect
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SFTP_FileMove($hConnection, $sSourceFile, $sDestinationFile)
    If ProcessExists($hConnection) = 0 Then
        Return SetError(1, 0, 0)
    EndIf

    Local $sLine
    $sLine = StdoutRead($hConnection) ; dummy StdoutRead to empty the buffer from previously failed commands

    StdinWrite($hConnection, 'mv "' & $sSourceFile & '" "' & $sDestinationFile & '"' & @CRLF)
    ;loop required to wait for console response
    While 1
        sleep(100)
        $sLine = StdoutRead($hConnection, True) ; keep the stream buffer intact
;~         If $sLine <> "" Then ExitLoop
        If StringInStr($sLine, "psftp>") Then ExitLoop
    WEnd
    While 1
        $sLine = StdoutRead($hConnection)
        If ProcessExists($hConnection) = 0 Then
            Return SetError(1, 0, 0)
        ElseIf StringInStr($sLine, $sSourceFile & " -> ") Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            ContinueLoop
        ElseIf StringInStr($sLine, "no such file or directory") Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            Return SetError(2, 0, 0)
        ElseIf StringInStr($sLine, "failure") Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            Return SetError(3, 0, 0)
        ElseIf StringInStr($sLine, "nothing matched") Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            Return SetError(4, 0, 0)
        ElseIf StringInStr($sLine, "psftp>") Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            ExitLoop
        ElseIf $sLine <> "" Then ; any other failure
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            Return SetError(5, 0, 0)
        ElseIf $sLine == "" Then
            ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " " & $sLine & @CR)
            ExitLoop
        EndIf
        Sleep(10)
        ;If Not @Compiled Then ConsoleWrite( @ScriptLineNumber & " >" & $sLine & "<" & @CR)
    WEnd
    $sLine = StdoutRead($hConnection) ; dummy StdoutRead to empty the buffer
    Return 1
EndFunc   ; ==>_SFTP_FileMove

I hope this helps.

GreenCan

Edited by GreenCan

Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Share this post


Link to post
Share on other sites

#24 ·  Posted (edited)

An example file is always better...  here is one

remember to fill in these lines:

Global $sServerName = "server name"                 ; ==> fill in
Global $sUsername = "user name"                        ; ==> fill in
Global $sPassword = "user password"                  ; ==> fill in
Global $iServerPort = 22                                       ; ==> fill in optionally, default should be 22, not 0 as far as i know
Global $cRemoteDir = "/some remote paths"        ; ==> fill in

and create a 'test.txt' file in your script folder.

Update 12/12/2013

cleanup up example

SFTPEx example - Generic.au3

Edited by GreenCan
2 people like this

Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Share this post


Link to post
Share on other sites

#25 ·  Posted (edited)

Sorry for the late and thanks for these improvements!

A question: why it is needed a second loop before the main one? the check of the "psftp>" string is already included in the main loop.

If you would like to help me to improve and complete this UDF, I think it could be useful to several users.  :thumbsup:

Edited by Lupo73

SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites

It's my turn to apologize for replying late to your query...

A question: why it is needed a second loop before the main one? the check of the "psftp>" string is already included in the main loop.

I added the loop because I noticed that for connections using slow sftp hosts and especially when using wildcards or large files, I got unresolved and replicated errors.  The simplest explanation is that the psftp> prompt might not yet be in the buffer while the function performs the error checking.

I don't remember exactly what kind of error I got, but the file move failed without returning any of the 5 error codes.

Ideally you want to exit here

        ElseIf StringInStr($sLine, "psftp>") Then
or here;
        ElseIf $sLine == "" Then
but either you receive the prompt or you don't get it and your function fails in any or the other case.

If you really need assurance, I can try to replicate the issue but  I don't know when I will have time to check this.

GreenCan

 


Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Share this post


Link to post
Share on other sites

Don't worry.. but obviously feel free to propose (or directly do) other improvements and fixes to the code.


SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites

THANK YOU Green Can lol I'm a hack and this in your face example gave me enough examples to get me started using the udf!!!

 

An example file is always better...  here is one

remember to fill in these lines:

Global $sServerName = "server name"                 ; ==> fill in
Global $sUsername = "user name"                        ; ==> fill in
Global $sPassword = "user password"                  ; ==> fill in
Global $iServerPort = 22                                       ; ==> fill in optionally, default should be 22, not 0 as far as i know
Global $cRemoteDir = "/some remote paths"        ; ==> fill in

and create a 'test.txt' file in your script folder.

Update 12/12/2013

cleanup up example

SFTPEx example - Generic.au3


Don't let that status fool you, I am no advanced memeber!

Share this post


Link to post
Share on other sites

My pleasure :)

But the credits go to Lupo73 as he came with the SFTPEx udf, which by the way, I use every day in my company...

GreenCan


Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Share this post


Link to post
Share on other sites

#31 ·  Posted (edited)

I have discovered a truly insidious error in:

_SFTP_ListToArrayEx ()

Multiple spaces in file and directory names are filtered out by the splitting of the string.

$aSubStringSplit = _StringExplode(StringStripWS($aStringSplit[$A], 7), " ", 8)

But this should not happen, because the names must remain unchanged in all cases.

As a workaround, I have therefore the above line changed to:

$sSplit = $aStringSplit[$A]
$aSubStringSplit = _StringExplode(StringStripWS(StringLeft($sSplit, StringInStr($sSplit, ":") + 3), 7), " ", 7)
_ArrayAdd($aSubStringSplit, StringMid($sSplit, StringInStr($sSplit, ":") + 4))

A very interesting project that will hopefully continue to be cultivated.
 

Edited by Bitnugger

Share this post


Link to post
Share on other sites

#32 ·  Posted (edited)

I have started using this UDF SFTPEx.au3 to download a bunch of files from a remote server to local. 

First, Great job with the UDF and this really works! awesome work Lupo73...

Second, there is no timestamps on the listed files when I use the function _SFTP_ListToArrayEx. Screenshots attached

Third, Is the _SFTP_ProgressDownload function still not working?

I am using the version 1.0 beta 9

_arraydisplay.png      using _ArrayDisplay function

filezilla.png    using Filezilla

Edited by Gauss
snapshot labels updated

Share this post


Link to post
Share on other sites

No replies yet on my question. Anybody please help.

Share this post


Link to post
Share on other sites

No replies yet on my question. Anybody please help.

As a workaround, I have therefore the above line changed to:

$sSplit = $aStringSplit[$A]
$aSubStringSplit = _StringExplode(StringStripWS(StringLeft($sSplit, StringInStr($sSplit, ":") + 3), 7), " ", 7)
_ArrayAdd($aSubStringSplit, StringMid($sSplit, StringInStr($sSplit, ":") + 4))

A very interesting project that will hopefully continue to be cultivated.

did you try that ? 

Share this post


Link to post
Share on other sites

Please help

I want upload folder with all content from local comp to server by sftp

use _SFTP_DirPutContents but on server i dont see new folder

_SFTP_DirPutContents dont return any errors

$hSession = _SFTP_Open()
$hSession = _SFTP_Connect($hSession, $sServerName, $sUsername, $sPassword, $iServerPort)

$server="/home/admin/web/local/public_html/"
$local="C:\test\"
$err =_SFTP_DirPutContents($hSession, $local, $server, 1)

When use _SFTP_FilePut all work fine

Share this post


Link to post
Share on other sites

#36 ·  Posted (edited)

Hi Lupo73 and thank  you for your work, this UDF saves my life.

This said, I found the function _SFTP_DirSetCurrent() "works" but doesn't return *exactly*  the new DIR, because I think the output inside the func is not correctly trimmed.

So when I try to set "/20170517073216" as current DIR the Func returns something like "/20170517073216/
psftp> " (with all the spaces) so to test if  the FUNC has worked well I must do something like 

If StringInStr(_SFTP_DirSetCurrent($Conn, $ftpfolder),$ftpfolder) Then...
 ;ok 
else
 ;not OK
endif

Cheers.

Edited by t0nZ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now