Get FileList on Remote PC via SFTP

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Get FileList on Remote PC via SFTP

Post by MichaelS » Sun May 24, 2020 8:11 am

Hello,
does anybody know how is it possible to show Directory List via SFTP ?
Later I want to put the file List on a DataGrid, and pick the hilited Line to upload/download Files from my Raspberry.
I have a Indy License...

Code: Select all

local tResult, tCmds
   put "ls" into tCmds
   put tsNetSendCmd("1", "sftp://pi:Wuppertal2019@192.168.178.31", tCmds,"transferComplete") into tResult
   put tResult into fld "Ausgabe"
   tsNetCloseConn "1"
This gives the Error : tsnetError: Not initialised

Can anybody help me to find a solution?

Regards
Michael

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Get FileList on Remote PC via SFTP

Post by FourthWorld » Sun May 24, 2020 3:00 pm

Can you log onto the remote machine in Terminal with SSH?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Re: Get FileList on Remote PC via SFTP

Post by MichaelS » Sun May 24, 2020 4:29 pm

Yes, offcourse i can !
Do you have any Idea?

Michael

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Get FileList on Remote PC via SFTP

Post by SparkOut » Sun May 24, 2020 4:34 pm

You do have to run the command tsNetInit to initialise it. I don't see that in your script sample. If you add it, is that the simple answer?

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Re: Get FileList on Remote PC via SFTP

Post by MichaelS » Sun May 24, 2020 5:31 pm

Hello again,
i added the init Code :

Code: Select all

on mouseUp
   local tResult, tCmds
   put "ls" into tCmds
   tsNetInit 
   put tsNetSendCmd("1", "ftp://pi:Wuppertal2019@192.168.178.31", tCmds,"transferComplete") into tResult
   answer the result
   put tResult into fld "Ausgabe"
   tsNetCloseConn "1"
end mouseUp
it is opening the answer window without text, and there ist nothing on the field "Ausgabe"

Is there another Possibility to get the file List ?

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Get FileList on Remote PC via SFTP

Post by matthiasr » Sun May 24, 2020 5:44 pm

Michael,

Although the dictionary shows both tsNetSendCmdSync and tsNetSendCmd to be available in Indy license.
My understanding was that only the synchronous commands/functions (if 2 similar command/functions are available) of tsNet were available in Indy license. I could be wrong, but could you try tsNetSendCmdSync instead of tsNetSendCMD.

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Re: Get FileList on Remote PC via SFTP

Post by MichaelS » Sun May 24, 2020 6:36 pm

Hi Matthias,
i changed the tsnet command to tsNetSendCmdSync . The Result is :


execution error at line n/a (External handler execution error: parameter 'pBytes' is not a variable) near "parameter 'pBytes' is not a variable"

I do not know what i have to change in this Expression...

Regards
Michael

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Get FileList on Remote PC via SFTP

Post by matthiasr » Sun May 24, 2020 6:45 pm

The parameters for the sync one are not the same.

tsNetSendCmd(pConnectionID, pURL, pCommand, pCallback, [pSettings])

tsNetSendCmdSync(pURL, pCommand, rResult, rBytes, [pSettings])


So in your case you should try this:
put "pi" into pSettings["username"]
put "Wuppertal2019" into pSettings["password"]
tsNetSendCmdSync( "ftp://192.168.178.31", tCMDs, rResult, rBytes, pSettings)

What i noticed... Your topic is saying "...via SFTP". But your URL begins with ftp:. So your code is always trying to connect using ftp.
If you want to connect to a sftp server you have to change ftp:// to sftp:// in your URL.

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Get FileList on Remote PC via SFTP

Post by matthiasr » Sun May 24, 2020 6:57 pm

Btw a simple

Code: Select all

put url "sftp://pi:Wuppertal2019@192.168.178.3/"
should give you also the file listing.
You can even add foldernames

Code: Select all

put url "sftp://pi:Wuppertal2019@192.168.178.3/public_html/"


And of course, if your ftp/sftp server is running on an other port than 21(ftp) / 22 (sftp) then you have to add the port also to the URL.

For example

Code: Select all

put url "sftp://pi:Wuppertal2019@192.168.178.3:8228/public_html/"

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Get FileList on Remote PC via SFTP

Post by SparkOut » Sun May 24, 2020 7:59 pm

I realise that there is nothing in this thread that specifically identifies a public site and credentials, but... you really shouldn't use the real password on a public forum, and I feel that even if it is a raspbi pi on a private network, the live server should have the password changed nevertheless.

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Re: Get FileList on Remote PC via SFTP

Post by MichaelS » Mon May 25, 2020 7:41 am

Good morning,
@Sparkout : The Raspi is not reachable extern, it is only to Test the sftp-Connection. The Code will be used to Connect an Android System with my rented Root Server, and i will not make public this Passwords. But Thanks for the Tip.

@ matthiasr : (Sorry for changing Sftp with ftp, i tried everything out)
This Code is not Working ; no Result, no Output:

Code: Select all

put url "sftp://pi:Wuppertal2019@192.168.178.31/" into Ausgabe
   answer the result
   put Ausgabe into fld"Ausgabe"
The following Code is working as expected :

Code: Select all

put url "sftp://pi:Wuppertal2019@192.168.178.31/home/pi/Notiz.txt" into Ausgabe
   answer the result
   put Ausgabe into fld"Ausgabe"
At least i tried this :

Code: Select all

local rResult, tCmds, rBytes, pSettings
   put "ls" into tCmds
   tsNetInit 
   put "pi" into pSettings["username"]
   put "Wuppertal2019" into pSettings["password"]
   tsNetSendCmdSync( "sftp://192.168.178.31", tCMDs, rResult, rBytes, pSettings)
   answer the result
   put rResult into fld "Ausgabe"
   tsNetCloseConn "1"
This is similar like the sample in the Dictionary and should work with Indy Licence.

But I got this Error :

execution error at line 7 (Handler: can't find handler) near "tsNetSendCmdSync", char 1

What does it mean "Can not find Handler ? Where is my Mistake ?

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Get FileList on Remote PC via SFTP

Post by matthiasr » Mon May 25, 2020 7:46 am

I am not sure if that is the problem, but there is a blank between ( and "sftp:..../b]

Code: Select all

tsNetSendCmdSync( "sftp://192.168.178.31", tCMDs, rResult, rBytes, pSettings)
Btw. did you try to connect using FTP already? Just to make sure there is no other problem with SFTP?

if not, could you try just

put URL "ftp://pi:Wuppertal2019@192.168.178.31"

matthiasr
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 190
Joined: Sat Apr 08, 2006 7:55 am
Location: Lübbecke, Germany
Contact:

Re: Get FileList on Remote PC via SFTP

Post by matthiasr » Mon May 25, 2020 8:47 am

MichaelS wrote:
Mon May 25, 2020 7:41 am

At least i tried this :

Code: Select all

local rResult, tCmds, rBytes, pSettings
   put "ls" into tCmds
   tsNetInit 
   put "pi" into pSettings["username"]
   put "Wuppertal2019" into pSettings["password"]
   tsNetSendCmdSync( "sftp://192.168.178.31", tCMDs, rResult, rBytes, pSettings)
   answer the result
   put rResult into fld "Ausgabe"
   tsNetCloseConn "1"
I just realize that you are calling tsNetSendCmdSync wrong. It´s a function, so your code should look something like this.

Code: Select all

local rResult, tCmds, rBytes, pSettings
   put "ls" into tCmds
   tsNetInit 
   put "pi" into pSettings["username"]
   put "Wuppertal2019" into pSettings["password"]
   get tsNetSendCmdSync( "sftp://192.168.178.31", tCMDs, rResult, rBytes, pSettings)   -- the var it contains the data
   answer the result
   put rResult into fld "Ausgabe"   -- as dictionary says, will contain 0 for sftp connections or on errors it will contain the tsNet error
rem   tsNetCloseConn "1" -- not needed  as you are calling the synchronous function
   
   

I will test your script later this day with my NAS which is running SFTP server. If there is still something wrong then i will create a sample script.

MichaelS
Posts: 24
Joined: Sat Jan 18, 2020 5:24 pm

Re: Get FileList on Remote PC via SFTP

Post by MichaelS » Mon May 25, 2020 8:49 am

I removed the Blank between the ( and " , it has no effect.

FTP Connection is although at the Terminal not working.
The Message is : ftp: pi@192.168.178.31: Name or service not known

I tried it in LC out but nothing happen, no Result.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Get FileList on Remote PC via SFTP

Post by bangkok » Mon May 25, 2020 9:03 am

Have a look a this sample stack, written by the creator... of tsNet.

https://downloads.techstrategies.com.au ... k.livecode

There are 4 buttons for you :
"SFTP Dir List Example"

"SFTP Delete Example""
"SFTP Upload Example"
"SFTP Download Example

Post Reply

Return to “Internet”