SFTP con Key
Posted: Fri Jan 12, 2018 7:45 pm
Saludos amigos,
Tengo este código que encontré en la red para conectarme a un servidor SFTP :
Aún no lo he probado, pero la duda que tengo es que, mi proveedor del servicio de SFTP me a enviado junto con los datos de conexión un archivo con nombre "101594424Key" que debo usar para autenticarme, inclusive me dice que para el password lo deje en blanco. Mi duda es como uso este archivo dentro del código arriba mostrado. Alguien con experiencia en SFTP me podria brindar una mano por favor.
Mil gracias.
--------------------------------------------------------------------------------------------------------------------------
Greetings, friends
I have this code that I found on the network to connect to an SFTP server:
I have not tried it yet, but the question I have is that, my SFTP service provider sent me, along with the connection data, a file with the name "101594424Key" that I must use to authenticate myself, it even tells me that the password leave blank. My question is how I use this file within the code shown above. Someone with experience in SFTP could give me a hand please.
Thanks a lot.
Tengo este código que encontré en la red para conectarme a un servidor SFTP :
Code: Select all
#include "hmg.ch"
#include "hbcurl.ch"
#include "common.ch"
#include "fileio.ch"
Function Main(cFile)
Default cFile:='test.txt'
DEFINE WINDOW sftp AT 0 , 0 WIDTH 0 HEIGHT 0 TITLE "" MAIN;
ON INIT (cAns:=SendSFTP(cFile), MsgInfo(cAns), ThisWindow.Release);
NOSHOW;
NOMINIMIZE;
NOMAXIMIZE;
NOSIZE;
NOSYSMENU;
NOCAPTION
END WINDOW
ACTIVATE WINDOW sftp
QUIT
Return Nil
****************************************************************************************
Function SendSFTP(cFile)
Local cRet, UserLogin, UserPass, cUrl, cUploadFolder, cSftpURL, nPort, nFSize
IF EMPTY(cFile)
RETURN "No File"
ENDIF
//produkcyjne srodowisko TEXTHIDDEN(1) zamaskowana zmienna
#pragma TEXTHIDDEN(1)
UserLogin := 'testsftp'
UserPass := '1qazxsw2'
#pragma TEXTHIDDEN(0)
cUrl := "localhost"
cUploadFolder:=""
cSftpURL := "sftp://"+UserLogin+":"+UserPass+"@"+cUrl+"/"+cUploadFolder+cFile
nPort := 22
nFSize := FILESIZE(cFile)
info_('Init cURL')
//Init
curlHandle:=SFTP_api_init()
IF EMPTY(curlHandle)
Info_("Error while init cURL lib.",.T.)
WAIT CLEAR
RETURN "Error while init cURL lib."
ENDIF
info_('Uploading ...')
curl_easy_reset( curlHandle )
curl_easy_setopt(curlHandle, HB_CURLOPT_URL, cSftpURL)
curl_easy_setopt(curlHandle, HB_CURLOPT_PORT, nPort)
curl_easy_setopt(curlHandle, HB_CURLOPT_UPLOAD, 1)
curl_easy_setopt(curlHandle, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_SFTP)
curl_easy_setopt(curlHandle, HB_CURLOPT_UL_FILE_SETUP, cFile)
curl_easy_setopt(curlHandle, HB_CURLOPT_INFILESIZE_LARGE, nFSize)
/* Do everything */
curlErr := curl_easy_perform(curlHandle)
/* Report any errors */
IF !EMPTY( curlErr )
cRet:=curl_easy_strerror(curlErr)
ELSE
cRet:='Sucessfully'
ENDIF
//Close
SFTP_api_close( curlHandle )
WAIT CLEAR
RETURN cRet
*****************************************************************************************
Function SFTP_api_init()
Local curlHandle := curl_easy_init()
RETURN curlHandle
******************************************************************************************
Function SFTP_api_close( curlHandle )
IF !EMPTY(curlHandle)
/* Clean-up libcurl */
curl_global_cleanup( curlHandle )
ENDIF
RETURN
******************************************************************************************
Function info_(cTresc, lCzekaj)
Local nTimeIni := hb_MilliSeconds()
Default lCzekaj:=.F.
Wait Window cTresc NoWait
IF lCzekaj
WHILE( ( hb_MilliSeconds() - nTimeIni ) < 3 * 1000 )
DO EVENTS
hb_releaseCPU()
ENDDO
ENDIF
RETURN Nil
********************************
Mil gracias.
--------------------------------------------------------------------------------------------------------------------------
Greetings, friends
I have this code that I found on the network to connect to an SFTP server:
Code: Select all
#include "hmg.ch"
#include "hbcurl.ch"
#include "common.ch"
#include "fileio.ch"
Function Main(cFile)
Default cFile:='test.txt'
DEFINE WINDOW sftp AT 0 , 0 WIDTH 0 HEIGHT 0 TITLE "" MAIN;
ON INIT (cAns:=SendSFTP(cFile), MsgInfo(cAns), ThisWindow.Release);
NOSHOW;
NOMINIMIZE;
NOMAXIMIZE;
NOSIZE;
NOSYSMENU;
NOCAPTION
END WINDOW
ACTIVATE WINDOW sftp
QUIT
Return Nil
****************************************************************************************
Function SendSFTP(cFile)
Local cRet, UserLogin, UserPass, cUrl, cUploadFolder, cSftpURL, nPort, nFSize
IF EMPTY(cFile)
RETURN "No File"
ENDIF
//produkcyjne srodowisko TEXTHIDDEN(1) zamaskowana zmienna
#pragma TEXTHIDDEN(1)
UserLogin := 'testsftp'
UserPass := '1qazxsw2'
#pragma TEXTHIDDEN(0)
cUrl := "localhost"
cUploadFolder:=""
cSftpURL := "sftp://"+UserLogin+":"+UserPass+"@"+cUrl+"/"+cUploadFolder+cFile
nPort := 22
nFSize := FILESIZE(cFile)
info_('Init cURL')
//Init
curlHandle:=SFTP_api_init()
IF EMPTY(curlHandle)
Info_("Error while init cURL lib.",.T.)
WAIT CLEAR
RETURN "Error while init cURL lib."
ENDIF
info_('Uploading ...')
curl_easy_reset( curlHandle )
curl_easy_setopt(curlHandle, HB_CURLOPT_URL, cSftpURL)
curl_easy_setopt(curlHandle, HB_CURLOPT_PORT, nPort)
curl_easy_setopt(curlHandle, HB_CURLOPT_UPLOAD, 1)
curl_easy_setopt(curlHandle, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_SFTP)
curl_easy_setopt(curlHandle, HB_CURLOPT_UL_FILE_SETUP, cFile)
curl_easy_setopt(curlHandle, HB_CURLOPT_INFILESIZE_LARGE, nFSize)
/* Do everything */
curlErr := curl_easy_perform(curlHandle)
/* Report any errors */
IF !EMPTY( curlErr )
cRet:=curl_easy_strerror(curlErr)
ELSE
cRet:='Sucessfully'
ENDIF
//Close
SFTP_api_close( curlHandle )
WAIT CLEAR
RETURN cRet
*****************************************************************************************
Function SFTP_api_init()
Local curlHandle := curl_easy_init()
RETURN curlHandle
******************************************************************************************
Function SFTP_api_close( curlHandle )
IF !EMPTY(curlHandle)
/* Clean-up libcurl */
curl_global_cleanup( curlHandle )
ENDIF
RETURN
******************************************************************************************
Function info_(cTresc, lCzekaj)
Local nTimeIni := hb_MilliSeconds()
Default lCzekaj:=.F.
Wait Window cTresc NoWait
IF lCzekaj
WHILE( ( hb_MilliSeconds() - nTimeIni ) < 3 * 1000 )
DO EVENTS
hb_releaseCPU()
ENDDO
ENDIF
RETURN Nil
********************************
Thanks a lot.