[SOLVED] Using gb.util prompt/expect with ssh client

Post your Gambas programming questions here.
Post Reply
User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

[SOLVED] Using gb.util prompt/expect with ssh client

Post by zagirovaa » Friday 11th June 2021 8:52am

Hi, everyone!

I would like to know whether anyone had success with connecting to ssh server (or telnet) using expect/prompt from Process class that is in gb.util component (beginning with Gambas 3.15)? I myself tried but with no success. Any working example would be appriciated!

Thanks in advance!
Last edited by zagirovaa on Sunday 04th July 2021 11:40am, edited 1 time in total.

User avatar
cogier
Site Admin
Posts: 739
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Using gb.util prompt/expect with ssh client

Post by cogier » Saturday 12th June 2021 3:50pm

Welcome to the forum.

This is a puzzle. Two of us have been trying to get this to work for the last 3 hours with very little progress. We haven't given up just yet, so watch this space. Can you tell us what you are trying to achieve, there maybe another way to do this?

User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

Re: Using gb.util prompt/expect with ssh client

Post by zagirovaa » Sunday 13th June 2021 12:24pm

Hi!

I would like to develop an application which would periodically save configurations of active network devices like switches, routers, etc. I could implement that using python and qt (in python there are different libraries like paramiko) but it is much more easier to develop GUI applications in Gambas than in any other language.

Looking forward to your help!

User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

Re: Using gb.util prompt/expect with ssh client

Post by zagirovaa » Sunday 13th June 2021 12:29pm

Please, have a look at this project: https://cbackup.me/en/
It works great for me (written in java and php) but it is no longer supported and i would like to expand the functionality it gives in my application.

User avatar
cogier
Site Admin
Posts: 739
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Using gb.util prompt/expect with ssh client

Post by cogier » Sunday 13th June 2021 2:22pm

This is going over my head. However, if you can do all this in Java and PHP then why not use that code and pull it in to Gambas to display.

Here is a Gambas program in which I created some simple Python, ran the Python and displayed the result in Gambas.
TestNew-0.0.11.tar.gz
(12.21 KiB) Downloaded 14 times

User avatar
BruceSteers
Posts: 590
Joined: Thursday 23rd July 2020 5:20pm

Re: Using gb.util prompt/expect with ssh client

Post by BruceSteers » Sunday 13th June 2021 4:47pm

Maybe ask on the gambas mailing list?
I read in the wiki the bit from Process.Expect()
http://gambaswiki.org/wiki/comp/gb.util/process/expect

Read some bit (cannot remember where) that said no more but had (Think ssh/ssl) appended to it.
but it had no further explanation/example.

I'd try the mailing list and see if whoever wrote the command has an explanation.

Does this not help?...
Dim hProcess As Process
Dim sPassword As String = "BigSecret"
  
hProcess = Shell "scp /all/that/stuff/* login@server:~/here" For Input Output As "Process"
' Handle the message emitted when the server is unknown
hProcess.Expect("(yes/no*)?", "yes")
' Any emitted string ending with a colon is supposed to be the password prompt
hProcess.Expect(":", sPassword)
' Wait for the process to terminate
hProcess.Wait
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

Re: Using gb.util prompt/expect with ssh client

Post by zagirovaa » Sunday 13th June 2021 5:12pm

cogier wrote:
Sunday 13th June 2021 2:22pm
This is going over my head. However, if you can do all this in Java and PHP then why not use that code and pull it in to Gambas to display.

Here is a Gambas program in which I created some simple Python, ran the Python and displayed the result in Gambas.

TestNew-0.0.11.tar.gz
Hi!

I didn't say that I know Java or Php. I meant that CBackup project is written in those languages. I gave you the link to the project as an example what I would like to implement. And I would like to write the whole code in pure gambas. According to Gambas wiki there is an appropriate functionality but, unfortunately, it doesn't work as I expect it to.

User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

Re: Using gb.util prompt/expect with ssh client

Post by zagirovaa » Sunday 13th June 2021 5:15pm

BruceSteers wrote:
Sunday 13th June 2021 4:47pm
Maybe ask on the gambas mailing list?
I read in the wiki the bit from Process.Expect()
http://gambaswiki.org/wiki/comp/gb.util/process/expect

Read some bit (cannot remember where) that said no more but had (Think ssh/ssl) appended to it.
but it had no further explanation/example.

I'd try the mailing list and see if whoever wrote the command has an explanation.

Does this not help?...
Dim hProcess As Process
Dim sPassword As String = "BigSecret"
  
hProcess = Shell "scp /all/that/stuff/* login@server:~/here" For Input Output As "Process"
' Handle the message emitted when the server is unknown
hProcess.Expect("(yes/no*)?", "yes")
' Any emitted string ending with a colon is supposed to be the password prompt
hProcess.Expect(":", sPassword)
' Wait for the process to terminate
hProcess.Wait
I already read everything related to expect/prompt on Gambas wiki but that doesn't work when it comes to ssh and/or telnet. I asked the same question in Gambas official IRC channel. No answer. I will try mailing list as you advised.

User avatar
zagirovaa
Posts: 8
Joined: Tuesday 27th September 2016 5:48am

Re: Using gb.util prompt/expect with ssh client

Post by zagirovaa » Sunday 04th July 2021 11:37am

Finally found a solution to this problem on Gambas BugTracker. Thanks for Brian G.
Here is a working example of ssh connection from Gambas project:
' Gambas module file

Public myprocess As Process
Public GotPrompt As Boolean = False
Public logonComplete As Boolean = False
Public tableOfCommand As String[] = ["ls -l",
  "cd /",
  "df -h",
  "exit"]
Public CurrentCmd As Integer = 0
Public ConnectionOpen As Boolean = True

Public Sub Main()
  ' if you change this to use a shell it does not work at all

  MyProcess = Exec ["ssh", "gshdemo@localhost"] For Input Output As "TheProcess"
  'MyProcess = Shell "ssh gshdemo@localhost" For Input Output As "TheProcess"
  Wait

  If MyProcess.Running Then
    Print "set prompt"
    myprocess.expect(":", "password")
    While myprocess.Running
      Wait
      If logonComplete Then Break
    Wend
    myprocess.expect("*:?$") ' wait for prompt

    While myprocess.Running And ConnectionOpen
      Wait
      If GotPrompt Then
        If CurrentCmd < tableOfCommand.Count Then
          Print #myprocess, tableOfCommand[CurrentCmd]
          If tableOfCommand[CurrentCmd] = "exit" Then
            myprocess.expect("*closed.") ' wait for prompt
          Endif
          Inc CurrentCmd
          GotPrompt = False
        Endif
      Endif
    Wend
    myprocess.Kill
  Endif

End

Public Sub TheProcess_prompt(theprompt As String, theanswer As String)

  Print "Got prompt", thePrompt, theanswer
  Select Case ThePrompt
    Case "*:"
      logonComplete = True
    Case "*:?$"
      GotPrompt = True
    Case "*closed."
      ConnectionOpen = False
  End Select

End

Public Sub TheProcess_read()

  Dim Buffer As String
  Dim ReadLen As Long = Lof(MyProcess)

  While readLen > 0
    buffer &= Read #MyProcess, readlen
    ReadLen = Lof(MyProcess)
  Wend
  Print buffer
  'Flush

End

Post Reply