SFTP con Key

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
edufloriv
Posts: 122
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB
Location: PERU
Has thanked: 1 time
Been thanked: 4 times

SFTP con Key

Post by edufloriv » Fri Jan 12, 2018 7:45 pm

Saludos amigos,

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
********************************
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:

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
********************************
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.

Eduardo Flores Rivas


LIMA - PERU

jparada
Posts: 221
Joined: Fri Jan 23, 2009 5:18 pm

Post by jparada » Tue Jan 16, 2018 6:15 pm

Hola Eduardo,
Me pareció interesante tu pregunta y aunque no tengo la respuesta porque no he probado por falta de tiempo, encontré esto y parece que por ahí podrías probar.

Es el link donde está la información y el comando o la forma que se propone.

/*
https://winscp.net/forum/viewtopic.php?t=2992
sftp://user@example.com /privatekey="c:\mykey.ppk" >out.txt
*/

/*
https://serverfault.com/questions/27396 ... command-li
You can simply use the -i argument for your sftp or ssh command.
sftp -i /path/to/private/keyfile ...
If the -i option is not available, you can use the -o option with a syntax like:
sftp -oIdentityFile=/path/to/private/keyfile ...
*/

/* archivo BAT
https://superuser.com/questions/483593/ ... to-another
sftp -b /tmp/batchfile.txt -oIdentityFile=/path/to/private_key user@host
*/

/*
https://geek.jasonhancock.com/2010/12/2 ... ivate-key/
*/

Si decides hacer las pruebas, cuéntame por favor como te va.

Saludos,
Javier

edk
Posts: 233
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 67 times
Been thanked: 171 times

Post by edk » Tue Jan 16, 2018 9:38 pm

Maybe try setup private key with:

Code: Select all

curl_easy_setopt(curlHandle, HB_CURLOPT_SSH_PRIVATE_KEYFILE,  cKeyFile)
You can also look at:
https://curl.haxx.se/libcurl/c/CURLOPT_ ... YFILE.html
https://curl.haxx.se/libcurl/c/CURLOPT_SSLKEY.html

Note that the curl parameters in harbour port have the prefix HB_

Post Reply