Page 1 of 1

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

Posted: Friday 11th June 2021 8:52am
by zagirovaa
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!

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

Posted: Saturday 12th June 2021 3:50pm
by cogier
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?

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

Posted: Sunday 13th June 2021 12:24pm
by zagirovaa
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!

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

Posted: Sunday 13th June 2021 12:29pm
by zagirovaa
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.

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

Posted: Sunday 13th June 2021 2:22pm
by cogier
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 211 times

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

Posted: Sunday 13th June 2021 4:47pm
by BruceSteers
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

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

Posted: Sunday 13th June 2021 5:12pm
by zagirovaa
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.

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

Posted: Sunday 13th June 2021 5:15pm
by zagirovaa
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.

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

Posted: Sunday 4th July 2021 11:37am
by zagirovaa
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