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

Post

Posted
Rating:
#1 (In Topic #687)
Avatar
Trainee
 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!
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
 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?
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Trainee
 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!
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Trainee
Please, have a look at this project: cbackup.me
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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
Maybe ask on the gambas mailing list?
I read in the wiki the bit from Process.Expect()
/comp/gb.util/process/expect - Gambas Documentation

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?…

Code (gambas)

  1. Dim hProcess As Process
  2. Dim sPassword As String = "BigSecret"
  3.  
  4. hProcess = Shell "scp /all/that/stuff/* login@server:~/here" For Input Output As "Process"
  5. ' Handle the message emitted when the server is unknown
  6. hProcess.Expect("(yes/no*)?", "yes")
  7. ' Any emitted string ending with a colon is supposed to be the password prompt
  8. hProcess.Expect(":", sPassword)
  9. ' Wait for the process to terminate
  10. hProcess.Wait
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Trainee

cogier said

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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Trainee

BruceSteers said

Maybe ask on the gambas mailing list?
I read in the wiki the bit from Process.Expect()
/comp/gb.util/process/expect - Gambas Documentation

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?…

Code (gambas)

  1. Dim hProcess As Process
  2. Dim sPassword As String = "BigSecret"
  3.  
  4. hProcess = Shell "scp /all/that/stuff/* login@server:~/here" For Input Output As "Process"
  5. ' Handle the message emitted when the server is unknown
  6. hProcess.Expect("(yes/no*)?", "yes")
  7. ' Any emitted string ending with a colon is supposed to be the password prompt
  8. hProcess.Expect(":", sPassword)
  9. ' Wait for the process to terminate
  10. 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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Trainee
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:

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public myprocess As Process
  4. Public GotPrompt As Boolean = False
  5. Public logonComplete As Boolean = False
  6. Public tableOfCommand As String[] = ["ls -l",
  7.   "cd /",
  8.   "df -h",
  9.   "exit"]
  10. Public CurrentCmd As Integer = 0
  11. Public ConnectionOpen As Boolean = True
  12.  
  13. Public Sub Main()
  14.   ' if you change this to use a shell it does not work at all
  15.  
  16.   MyProcess = Exec ["ssh", "gshdemo@localhost"] For Input Output As "TheProcess"
  17.   'MyProcess = Shell "ssh gshdemo@localhost" For Input Output As "TheProcess"
  18.   Wait
  19.  
  20.   If MyProcess.Running Then
  21.     Print "set prompt"
  22.     myprocess.expect(":", "password")
  23.     While myprocess.Running
  24.       Wait
  25.       If logonComplete Then Break
  26.     Wend
  27.     myprocess.expect("*:?$") ' wait for prompt
  28.  
  29.     While myprocess.Running And ConnectionOpen
  30.       Wait
  31.       If GotPrompt Then
  32.         If CurrentCmd < tableOfCommand.Count Then
  33.           Print #myprocess, tableOfCommand[CurrentCmd]
  34.           If tableOfCommand[CurrentCmd] = "exit" Then
  35.             myprocess.expect("*closed.") ' wait for prompt
  36.           Endif
  37.           Inc CurrentCmd
  38.           GotPrompt = False
  39.         Endif
  40.       Endif
  41.     Wend
  42.     myprocess.Kill
  43.  
  44.  
  45. Public Sub TheProcess_prompt(theprompt As String, theanswer As String)
  46.  
  47.   Print "Got prompt", thePrompt, theanswer
  48.   Select Case ThePrompt
  49.     Case "*:"
  50.       logonComplete = True
  51.     Case "*:?$"
  52.       GotPrompt = True
  53.     Case "*closed."
  54.       ConnectionOpen = False
  55.  
  56.  
  57. Public Sub TheProcess_read()
  58.  
  59.   Dim Buffer As String
  60.   Dim ReadLen As Long = Lof(MyProcess)
  61.  
  62.   While readLen > 0
  63.     buffer &= Read #MyProcess, readlen
  64.     ReadLen = Lof(MyProcess)
  65.   Wend
  66.   Print buffer
  67.   'Flush
  68.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.