Page 1 of 1

Have you example using liburl with scp:// ?

Posted: Mon Dec 21, 2020 1:36 am
by sec
Thanks!

Re: Have you example using liburl with scp:// ?

Posted: Mon Dec 21, 2020 5:56 pm
by infratec
In windows you need the external libcurl.dll, since scp is not included in the internal libcurl.

Code: Select all

EnableExplicit

#LibCurl_DLL$ = "libcurl.dll"

IncludeFile "libcurl.pbi"


ProcedureC.l my_fread(*buffer, Size, NMemB, fd)
  
  Protected Result.i
  
  
  Result = ReadData(fd, *buffer, (Size & 255) * NMemB)
  
  Debug Str(Result) + " bytes read from file"
  
  ProcedureReturn Result
  
EndProcedure




Define curl.i, fd.i, res.i, speed_upload.d, total_time.d

fd = ReadFile(#PB_Any, "scp.pb") ; open file to upload
If fd
  
  curl = curl_easy_init()
  If curl
    ; upload To this place
    curl_easy_setopt_str(curl, #CURLOPT_URL, "scp://<my_host>:<my_port>/home/xxx/scp.pb")
    
    ; adjust user And password
    curl_easy_setopt_str(curl, #CURLOPT_USERPWD, "<username>:<password>")
    
    ; tell it To "upload" To the URL
    curl_easy_setopt(curl, #CURLOPT_UPLOAD, #True)
    
    ; set where To Read from (on Windows you need To use READFUNCTION too)
    curl_easy_setopt(curl, #CURLOPT_READFUNCTION, @my_fread())
    
    curl_easy_setopt(curl, #CURLOPT_READDATA, fd)
    
    ; And give the size of the upload (optional)
    ; curl_easy_setopt(curl, #CURLOPT_INFILESIZE_LARGE, Lof(fd))
    
    ; And give the size of the upload (optional)
    curl_easy_setopt(curl, #CURLOPT_INFILESIZE, Lof(fd))
    
    ; accept insecure certificates
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, #False)
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, #False)
    
    ; enable verbose For easier tracing
    curl_easy_setopt(curl, #CURLOPT_VERBOSE, #True)
    
    res = curl_easy_perform(curl)
    If res = #CURLE_OK
      ; now extract transfer info
      curl_easy_getinfo(curl, #CURLINFO_SPEED_UPLOAD, @speed_upload)
      curl_easy_getinfo(curl, #CURLINFO_TOTAL_TIME, @total_time)
      
      Debug "Speed: " + StrD(speed_upload, 3) + " bytes/sec during " + StrD(total_time, 3) + " seconds"
    Else
      Debug "Error: " + curl_easy_strerror(res)
    EndIf
    
    ; always cleanup
    curl_easy_cleanup(curl)
  EndIf
  
  CloseFile(fd)
Else
  Debug "Was not able to open file"
EndIf

Re: Have you example using liburl with scp:// ?

Posted: Tue Dec 22, 2020 12:14 am
by sec
Greatly appreciate your help.
Where to get those files?:
#LibCurl_DLL$ = "libcurl.dll"

IncludeFile "libcurl.pbi"

And has the way to set timeout or which its default timeout?
In fact, i used pscp and one thread with timeout to kill 'pscp process' that be hanged.. that is not always working, so looking for better the way to handle those stuffs. That is why i ask.
infratec wrote:In windows you need the external libcurl.dll, since scp is not included in the internal libcurl.

Code: Select all

EnableExplicit

#LibCurl_DLL$ = "libcurl.dll"

IncludeFile "libcurl.pbi"


ProcedureC.l my_fread(*buffer, Size, NMemB, fd)
  
  Protected Result.i
  
  
  Result = ReadData(fd, *buffer, (Size & 255) * NMemB)
  
  Debug Str(Result) + " bytes read from file"
  
  ProcedureReturn Result
  
EndProcedure




Define curl.i, fd.i, res.i, speed_upload.d, total_time.d

fd = ReadFile(#PB_Any, "scp.pb") ; open file to upload
If fd
  
  curl = curl_easy_init()
  If curl
    ; upload To this place
    curl_easy_setopt_str(curl, #CURLOPT_URL, "scp://<my_host>:<my_port>/home/xxx/scp.pb")
    
    ; adjust user And password
    curl_easy_setopt_str(curl, #CURLOPT_USERPWD, "<username>:<password>")
    
    ; tell it To "upload" To the URL
    curl_easy_setopt(curl, #CURLOPT_UPLOAD, #True)
    
    ; set where To Read from (on Windows you need To use READFUNCTION too)
    curl_easy_setopt(curl, #CURLOPT_READFUNCTION, @my_fread())
    
    curl_easy_setopt(curl, #CURLOPT_READDATA, fd)
    
    ; And give the size of the upload (optional)
    ; curl_easy_setopt(curl, #CURLOPT_INFILESIZE_LARGE, Lof(fd))
    
    ; And give the size of the upload (optional)
    curl_easy_setopt(curl, #CURLOPT_INFILESIZE, Lof(fd))
    
    ; accept insecure certificates
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, #False)
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, #False)
    
    ; enable verbose For easier tracing
    curl_easy_setopt(curl, #CURLOPT_VERBOSE, #True)
    
    res = curl_easy_perform(curl)
    If res = #CURLE_OK
      ; now extract transfer info
      curl_easy_getinfo(curl, #CURLINFO_SPEED_UPLOAD, @speed_upload)
      curl_easy_getinfo(curl, #CURLINFO_TOTAL_TIME, @total_time)
      
      Debug "Speed: " + StrD(speed_upload, 3) + " bytes/sec during " + StrD(total_time, 3) + " seconds"
    Else
      Debug "Error: " + curl_easy_strerror(res)
    EndIf
    
    ; always cleanup
    curl_easy_cleanup(curl)
  EndIf
  
  CloseFile(fd)
Else
  Debug "Was not able to open file"
EndIf

Re: Have you example using liburl with scp:// ?

Posted: Tue Dec 22, 2020 10:17 am
by infratec
You have so a high level, according to your ICs (or what else they should represent) on the left,
that I thought you are a master and you will know where you can get these files. :wink:

If you are not such a master, then you can use this link:
https://www.von-der-salierburg.de/downl ... urldll.zip

You can set a timeout in seconds via:

Code: Select all

curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
Or in ms:

Code: Select all

curl_easy_setopt(curl, #CURLOPT_TIMEOUT_MS, Timeout)
From:
https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html

Code: Select all

Default timeout is 0 (zero) which means it never times out during transfer. 

Re: Have you example using liburl with scp:// ?

Posted: Tue Dec 22, 2020 10:49 am
by sec
Its works! Thank you :mrgreen:

I have 7 ICs , only 2 active IC :twisted:
infratec wrote:You have so a high level, according to your ICs (or what else they should represent) on the left,
that I thought you are a master and you will know where you can get these files. :wink:

If you are not such a master, then you can use this link:
https://www.von-der-salierburg.de/downl ... urldll.zip

You can set a timeout in seconds via:

Code: Select all

curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
Or in ms:

Code: Select all

curl_easy_setopt(curl, #CURLOPT_TIMEOUT_MS, Timeout)
From:
https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html

Code: Select all

Default timeout is 0 (zero) which means it never times out during transfer.