SSH Tutorial or Example

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

SSH Tutorial or Example

Post by ghettocottage » Mon Mar 02, 2015 9:20 pm

Hello all. I am digging into livecode again, and would like to try a few things.

My first hurdle is creating an SSH connection. I have run across SSH External for Livecode: https://github.com/trevordevore/SSH-Ext ... r-LiveCode

I think I have that installed, but am not sure. It has some example files but those seem to be for Windows. I am using Ubuntu and am developing for Ubuntu.

Does anyone have a tutorial, or even just an example file of making an ssh connection to a remote server in livecode?

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

Re: SSH Tutorial or Example

Post by FourthWorld » Mon Mar 02, 2015 9:22 pm

What do you want to do with that connection?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Mon Mar 02, 2015 11:41 pm

A few things:

just generally play around

connect to a mysql database

experiment with sftp

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

Re: SSH Tutorial or Example

Post by FourthWorld » Tue Mar 03, 2015 3:51 pm

ghettocottage wrote:just generally play around
Terminal is probably the ideal place to exploring shell commands. LiveCode can be useful to automate some aspects of working with shell, but any such automation is best served by first making sure the details are well understood and addressed in the tools the system provides for that.
connect to a mysql database
For use on a private LAN you may need Trevor's external if you want to connect to MySQL over ssh.

If the server is connected to the public Internet, it's often considered good practice to use an intermediary layer to handle incoming requests, sanitizing inputs and enforcing restrictions, and then communicate those to MySQL, rather than expose MySQL to the Internet directly. This intermediary program is most commonly PHP, but LiveCode Server, Perl, Python, and other scripting languages can be used as well.
experiment with sftp
SFTP is useful in some contexts, and plans are underway to add direct support for that protocol in a future version of LiveCode.

In the meantime, it may be helpful to consider other options which may be either more secure and/or more efficient.

For example, if the goal is to allow public uploads, as from a Web form, there are many file upload scripts available for PHP and even LiveCode. Using HTTP for public uploads allows you to have more control over the interaction than SFTP, which would require embedding passwords in scripts.

For your own personal use or use by trusted team members, sharing ssh public keys with the server allows passwordless login, which is not only more convenient but also more secure. With shared ssh keys you can also make more convenient use of tools like scp or rsync, the latter being a more efficient choice for file transfers by virtue of the smarter algorithm it employs. With your computer's shared ssh public key on your server, you can use LiveCode to construct and execute rsync, scp, and other commands for a wide range of automated tasks.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Tue Mar 03, 2015 6:31 pm

Thanks for the reply. I appreciate your time and expertise
If the server is connected to the public Internet, it's often considered good practice to use an intermediary layer to handle incoming requests, sanitizing inputs and enforcing restrictions, and then communicate those to MySQL, rather than expose MySQL to the Internet directly. This intermediary program is most commonly PHP, but LiveCode Server, Perl, Python, and other scripting languages can be used as well.
I get that, but does not using ssh allow a similar thing? What I mean is that by connecting via ssh I can then connect to localhost rather than leaving my database server open to the internet.

I have a test server set up where I am playing with this. I have Livecode Server installed and it seems to be working. However, what I would like is to be able to create a desktop app that will be used by certian people who have ssh keys installed. The server will be restricted to ssh keys only.
The application will access the db...

Does that sound like a good use-case for ssh/db connection? Or do you still recommend using Livecode Server as an intermediary program?

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Tue Mar 03, 2015 9:01 pm

I have been dinkering around more with SSH-External-for-LiveCode https://github.com/trevordevore/SSH-Ext ... r-LiveCode and am starting to have doubts that it is functional. There is one issue on Github where someone is trying to use it and the git owner replies with:
I'm not sure. I don't use this external myself, I just hosted it for the person who created it.
Has anyone been successful in using this?

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: SSH Tutorial or Example

Post by dbm » Thu Mar 05, 2015 10:56 am

Hi,

i'm ubuntu user and developing with Livecode under this platform too. Using ssh without problem. I'm a beginner and my code is far far far away to something near good.

What are your problems using the ssh external?

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Thu Mar 05, 2015 4:22 pm

Hello. Thanks for responding.

For starters, I am not sure if I even have the external installed properly nor not. I have followed the directions as closely as possible. When I run "make livessh.so" I had some output, some errors, but nothing very clear on if it was properly done or not.

When I then open a demo file, and try to load the binary using the "Load" button ( after putting in the path to the binary) I am not having any output other than the single line "Found these external functions"

Test connections halts on the script line "put ssh_new() into ssh_session" and the error is "button "TestButton": execution error at line 3 (Function: error in function handler) near "ssh_new", char 8"

which makes me think that the external is perhaps not loaded properly.


do you have any advice for me?

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Thu Mar 05, 2015 4:28 pm

also, I would love to see one of your files that you have working with ssh, even a simple one.

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

Re: SSH Tutorial or Example

Post by FourthWorld » Thu Mar 05, 2015 5:03 pm

I haven't used the ssh external, but I use commands over ssh frequently in LiveCode, with shared keys. Here's a simple example of using rsync:

Code: Select all

on SnycFolder
  -- Uses rsync to post the contents of the local folder specified 
  -- in tSrcPath to the remote folder in tDestPath:
  --
  put "myUserName@somedomain.com" into tLogin
  put "~/somefolder/" into tSrcPath
  put "/home/remoteuser/somefolder/" into tDestPath
  --
  put "rsync -avz "&quote& rSrcPath &quote& \
        space&quote& tLogin &":"& tDestPath &quote into tCmd
  get shell(tCmd)
end SnycFolder
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Thu Mar 05, 2015 9:46 pm

Thanks Richard, that helped, and got me thinking things from a different angle.

I was (after much dinkering) able to create an SSH tunnel, open a database on localhost of my remote server...do db stuff, close the connection.

The problem I ran into doing things this way was that if I ran the tunnel from livecode shell, everything would come to a halt until I closed the tunnel from a terminal.
Apparently livecode wants a shell command to complete before proceeding. My hackery workaround was to put the ssh tunnel on a separate bash script and use "launch" in livecode rather than shell.

this allowed me to continue working in livecode while the ssh tunnel is running in the background.

So my db connection button has this at the start:

Code: Select all

on mouseUp
   --open ssh tunnel
   launch "~/livecode/database/tunnel"
   put "Tunnel Open" into fld "SQL"
and my "tunnel" script is simply:

#!/bin/bash -e

Code: Select all

#Start SSH
    ssh  -fN -L  3306:localhost:3306   user@remotehost.com

and then I close that tunnel with a different script that says:

Code: Select all

#!/bin/bash -e
trap "ps aux | grep ssh | grep 3306 | awk '{print \$2}' | xargs kill" EXIT SIGINT SIGTERM

I am totally open to someone telling me that is complete hackery trash and there is a much simpler way to accomplish the same thing with one line in livecode.

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: SSH Tutorial or Example

Post by dbm » Thu Mar 19, 2015 7:08 pm

ghettocottage wrote:Hello. Thanks for responding.

For starters, I am not sure if I even have the external installed properly nor not. I have followed the directions as closely as possible. When I run "make livessh.so" I had some output, some errors, but nothing very clear on if it was properly done or not.

When I then open a demo file, and try to load the binary using the "Load" button ( after putting in the path to the binary) I am not having any output other than the single line "Found these external functions"

Test connections halts on the script line "put ssh_new() into ssh_session" and the error is "button "TestButton": execution error at line 3 (Function: error in function handler) near "ssh_new", char 8"

which makes me think that the external is perhaps not loaded properly.


do you have any advice for me?

Sorry for the delay answering.

I'm having the same problem but I discovered that appending a

Code: Select all

wait 2 seconds
before each call to the ssh external, then it works.

In the example stack coming with the github ssh external (connection.livecode) add the "wait 2 seconds" as I did in the test button code:

Code: Select all

on mouseUp
   local ssh_session
   put ssh_new() into ssh_session
   ssh_options_set ssh_session, "host", field "host"
   ssh_options_set ssh_session, "port", field "port"
   ssh_connect ssh_session
 [b]  wait 2 seconds[/b]
   put ssh_userauth_password(ssh_session, field "login", field "password") into authResult
[b]   wait 2 seconds[/b]
   livessh_run_command ssh_session, "ls", "outVar"
   put outVar into field "LogField"
   ssh_disconnect ssh_session
   ssh_free ssh_session
end mouseUp
After 4 seconds, you will have the result of the ls

This workaround works under Ubuntu 12.04 x86, using the livessh.so that comes with the example ( i didn't compile it). Unfortunately, it doesn't work under Windows (I tested only with Windows 7)

For linux, may be LiveCode is doing some type of multithreading and after executing the ssh_connect ssh_session the put ssh_userauth_password(ssh_session, field "login", field "password") into authResult is executed in parallel or ssh_connect has not returned yet a needed value for ssh_userauth

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SSH Tutorial or Example

Post by ghettocottage » Thu Mar 19, 2015 7:40 pm

Thanks dbm. I appreciate you following up on that; however, I ended up finding a nicer solution using a seperate bash script. I find it works perfectly for what I want.

Interestingly, when I wrote my script, I had an almost identical issue and solution:

Code: Select all

   --launch tunnel
   launch defaultFolder & "/.ssh/tunnel" 
  
   --brief pause for connection to occur
   wait for 1 seconds
   put "*" into fld "SQL"
   wait for 1 seconds
   put "**" into fld "SQL"
   wait for 1 seconds
   put "***" into fld "SQL"
   wait for 1 seconds
   put "****" into fld "SQL"
which gives the ssh tunnel a few seconds to establish.

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: SSH Tutorial or Example

Post by karmacomposer » Fri Nov 01, 2019 5:02 pm

Hello. Old thread I know.

I've tried installing this but cannot get it to work in Windows 10 64 bit. Any help on how to install it is appreciated.

We need SSH capability in LiveCode.

Thanks.

Mike

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

Re: SSH Tutorial or Example

Post by FourthWorld » Fri Nov 01, 2019 9:09 pm

I use ssh from the built-in shell function. It never would have occurred to me to use an external for that.

Have you tried using it from shell()? And does it help to change the consoleCommand global property?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”