DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

start app via ssh

Sat Sep 01, 2018 9:53 am

Hi,

I seem to find some contradictory answers while searching, so here is my syntax question:

I have an app installed on the Rpi, written in python 3. (app name = "Remote1.py")
It has no GUI, but captures input from an rpi-installed matrix keypad.
The Rpi is connected to my laptop via USB.

I start a connection on the laptop FROM WITHIN a VB.net program as follows:
Dim sftp As New SftpClient("Aterminal.local", "user", "pasword")
sftp.Connect()
i can succesfully detect files, remove them and create them on the Pi.
>> works fine!
Question:
Now i would like to start "remote1.py" with some command +/- "python3 remote1.py" , from my vb program of course.
Here i am a bit stuck on what the syntax could be.
sftp.???

thx,
Paul

jahboater
Posts: 4595
Joined: Wed Feb 04, 2015 6:38 pm

Re: start app via ssh

Sat Sep 01, 2018 10:27 am

I just use:-

ssh user@host mycommand

For example:-

ssh pi@pi0 'ls -lh /tmp/file'

ssh pi@pi3 '(cd src; make check)'

Or in your case perhaps:

ssh pi@host "python3 remote1.py"

You may need "ssh -t" if your program writes to the terminal at all.

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: start app via ssh

Sat Sep 01, 2018 2:01 pm

Hi,

Thanks, but now i am totally confused.
I'm got things mixed up.
My "working" vb.net code is:

Dim sftp As New SftpClient("Aterminal.local", "user", "pasword")
sftp.Connect()
and e.g.
sftp.Create("/home/filename.txt")

The command lines in post #2 show "ssh..." commands.
I was expecting something along the lines of
sftp.run....
But that does not exist.
Stuck :(

Paul

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: start app via ssh

Sat Sep 01, 2018 2:10 pm

This is really more of a Visual Basic (and Windows) question than a RPi one.

That said, the obvious answer to the question of "How do I invoke ssh from within a VB program?" is "Use whatever the VB equivalent is of the C [and other languages] system command to run the ssh command as a sub-process."

But that's not what Opie is going for here. He wants some kind of "native/built-in" ("object oriented") VB thing - like he currently has for sftp.

Googling "launch ssh from vb" found, among other, this page:

https://example-code.com/vbdotnet/ssh_exec.asp
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: start app via ssh

Sat Sep 01, 2018 2:29 pm

Yes, it might be more a VB thing.

But i was encouraged by the success of all these other commands that do work (connect, create, read file contents),
that i found in Rpi postings.

Just surprised that executing a (native) command like starting up a program would not exist.
Guess i'll have to try this "chilcat" stuff :shock:

thx,
DPaul

pfletch101
Posts: 481
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: start app via ssh

Sat Sep 01, 2018 3:44 pm

A relatively easy way of implementing what you are trying to do while just using your SFTP connection would be to have the Pi program running all the time but Sleeping unless/until a specific file appears in a specific location. To 'start' it, you create the file; to 'stop' it, you delete the file.

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: start app via ssh

Sun Sep 02, 2018 6:59 am


"A relatively easy way of implementing what you are trying to do while just using your SFTP connection would be to have the Pi program running all the time but Sleeping unless/until a specific file appears in a specific location. To 'start' it, you create the file; to 'stop' it, you delete the file."



Hi,
I already do similar things to start and stop certain functions on the pi.

But the problem remains the same, how do i start (even sleeping) the program in the first place, from my VB program?
I could use a putty session on my laptop, and start the program from there. (Takes no programming at all).
But if one of you experts see me doing this, i would be embarrassed. :oops:
It looks primitive.

I have programmed the "F3" key to make the connection to the Rpi, i want to add a line to start up a certain python program.
It's the last thing missing from my setup.

Paul

EDIT: btw, the chilkat solution , proposed above, is carrying a license cost of 289 $. I'll try to find a workaround. :shock:

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: start app via ssh

Sun Sep 02, 2018 7:40 am

OK, my solution could be to start the program at bootup,
i found this:
https://www.dexterindustries.com/howto/ ... t-startup/

5 methods are proposed:

rc.local
.bashrc
init.d tab
systemd
crontab

Any preferred one that won't get me into trouble?
thx,
Paul

EDIT: rc.local method is extremely simple to implement, and seems to perform exactly as expected. We may be in business :)

jahboater
Posts: 4595
Joined: Wed Feb 04, 2015 6:38 pm

Re: start app via ssh

Sun Sep 02, 2018 9:05 am

DPaul wrote:
Sun Sep 02, 2018 7:40 am

5 methods are proposed:

rc.local
.bashrc
init.d tab
systemd
crontab
For info, ".bashrc" runs when a user log's on, not when the machine boots.
A systemd service is a bit more fiddly to set up, but gives you complete control.
(rc.local is in fact a systemd service ...).
rc.local is fine for most simple stuff.

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: start app via ssh

Sun Sep 02, 2018 10:45 am

SftpClient for creating files, SshClient for running commands.

I'm not a vb coder but you should beable to work it out.
/home/pi/Desktop/Scripts/test.py is the path to a script on my pi, so i'll use it as an example.

Code: Select all

Dim ssh As New SshClient("IP", "user", "password")
ssh.Connect()
Dim cmd = ssh.CreateCommand("python /home/pi/Desktop/Scripts/test.py")
cmd.Execute()
Execute also outputs any errors as a string.
Don't try connecting every time you run a command, that's just an example.

I've made something similar using C#. I do all my python coding from my pc, then upload for testing.
The app gives me everything I need, quick and easy.
GPIO pix.png
GPIO pix.png (110.02 KiB) Viewed 488 times

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: start app via ssh

Mon Sep 03, 2018 9:22 am

OK, thanks for the clarifications.
I read about the "sshclient", but somehow could not find the right syntax.

I now have 2 solutions for my ex-problem, which is embarrassing, because I have to make a decision :)

Paul

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: start app via ssh

Mon Sep 03, 2018 3:33 pm

Heres the vb source. I'll also upload the entire app.
Tested and works for me.
Send Command.zip
(187.02 KiB) Downloaded 8 times

Code: Select all

Imports Renci.SshNet
Imports Renci.SshNet.Sftp
Imports System.IO
Public Class GPIO

    Dim ssh As New SshClient("Ip here", "Username here", "Password here")

    Private Sub GPIO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ssh.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10)
        Try
            ssh.Connect()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "No Pi for you!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Function ClientConnected() As Boolean
        Try
            Return ssh.IsConnected
        Catch
            Return False
        End Try
    End Function

    Function CreateCommand(ByVal Command As String) As String
        If Command = String.Empty Or Not ClientConnected() Then
            Return "Command did not send"
        End If
        Dim cmd = ssh.CreateCommand(Command)
        Dim print As String() = cmd.Execute().Split(New Char() {vbCr, vbLf})
        Dim Output As String = String.Empty
        Using result As StreamReader = New StreamReader(cmd.ExtendedOutputStream)
            Dim errors As String() = result.ReadToEnd().Split(New Char() {vbCr, vbLf})
            For Each s As String In IIf(print.Length > 1, print, errors)
                Output += (s + Environment.NewLine)
            Next
        End Using

        Return IIf(cmd.ExitStatus = 0, IIf(Output.Trim() = String.Empty, "Command Sent", Output), Output)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(CreateCommand("python /home/pi/Desktop/Scripts/test.py"))
    End Sub

End Class

Return to “Beginners”