Page 1 of 1

SSH with plink

Posted: Wed Sep 03, 2014 3:51 pm
by spacewalker
Hello,

I need to login to a device (CISCO Switch) using SSH and retrieve the configuration.

I've tried to use plink for this based on the information I've found here :
http://www.purebasic.fr/english/viewtop ... 13&t=53356

So I need to do 4 steps:
1.Connect to the device with username and password (I think that this works...)
2.After connected : Answer a question with "no" in order to skip a "WARNING - POTENTIAL SECURITY BREACH!" warning.
3.send a command ("sh run") to the device that lists the current configuration
4.save the result (output) to a string

So here is what I do manually:

Code: Select all

plink -ssh -l admin -pw 123 10.1.6.100

WARNING - POTENTIAL SECURITY BREACH!
The server's host key does not match the one PuTTY has
cached in the registry....
If you want to carry on connecting but without updating
the cache, enter "n".
Update cached key? (y/n) : n                           <--- how to send this "n" ? (I will add the host key, but for now I want to understand how I could answer the question with "no")     
Using username "admin".
.
**********Logged in: SYSTEM01**************************************
.
IDESWB7b#sh run												<---- how to send this "sh run" command ?
Building configuration...

Current configuration : 20130 bytes							<---- save this output (maybe 100 lines...)
!
! Last configuration change at 16:42:30 CEST Wed Aug 20 2014 by cancom
! NVRAM config last updated at 16:42:32 CEST Wed Aug 20 2014 by cancom
!
version 12.2
vlan 2000
 --More-- ^C
.....

I have the following code that should just send "no" to answer the question, but it fails showing "exit code: 1"
#PLinkPath$ = #DQUOTE$ + "C:\Programme\putty 0.63\plink.exe" + #DQUOTE$

#Server$ = "10.1.6.100"
#User$ = "admin"
#Password$ ="123"


sCommand.s = "-ssh -l " + #User$ + " -pw " + #Password$ + " " + #Server$

PLinkID = RunProgram(#PLinkPath$, sCommand,"", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)


Output$ = ""

If PLinkID

;delay (1000)

WriteProgramStringN (PLinkID,"n") ; Try to send "n"+CR$

While ProgramRunning(PLinkID)
If AvailableProgramOutput(PLinkID)
Debug ReadProgramString(PLinkID) + Chr(13)
Output$ + ReadProgramString(PLinkID) + Chr(13)
EndIf
Wend

Output$ + Chr(13) + Chr(13)
Output$ + "Exitcode: " + Str(ProgramExitCode(PLinkID))

CloseProgram(PLinkID) ; Close the connection to the program

else
MessageRequester("Error", "Erro")

EndIf

MessageRequester("Output", Output$)
thank you

Re: SSH with plink

Posted: Wed Sep 03, 2014 7:19 pm
by infratec
Hi,

you can not send 'blind' any data to the program.
You have to wait until you are prompted for something.

The first messages of plink goes not to stdout, they goes to stderror.
So you need:

Code: Select all

PLinkID = RunProgram(#PLinkPath$, sCommand,"", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error)
Unfortunately, in my short test, ReadProgramError() is blocking.
That's against the help file, where it is written that it is non blocking.

Code: Select all

PLinkID = RunProgram(#PLinkPath$, sCommand,"", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error)

If PLinkID
  *Buffer = AllocateMemory(1024)
  If *Buffer
    Repeat
      
      Debug "A"
      
      Error$ = ReadProgramError(PLinkID)
      If Len(Error$)
        Debug "Error: " + Error$
        ;If Error$ = "
      EndIf
      
      Debug "B"
      
      Size = AvailableProgramOutput(PLinkID)
      Debug Size
      If Size
        Size = ReadProgramData(PLinkID, *Buffer, Size)     
        Output$ + PeekS(*Buffer, Size)
        Debug Output$
        If FindString(Output$, "$")
          Ok = #True
        EndIf
      Else
        Delay(10)
      EndIf
    Until Ok
    FreeMemory(*Buffer)
  EndIf
EndIf
That's a bug in PB 5.30.
http://www.purebasic.fr/english/viewtop ... =4&t=60203
But is already fixed for 5.31 :D

Bernd

Re: SSH with plink

Posted: Thu Sep 04, 2014 2:06 pm
by spacewalker
OK, thank you for the information... so I'll wait for 5.31

Re: SSH with plink

Posted: Thu Sep 04, 2014 2:11 pm
by infratec
You don't have to wait :!:

5.31b1 is ready :mrgreen:

Re: SSH with plink

Posted: Thu Sep 04, 2014 2:30 pm
by infratec
:cry:

5.31b1 is still not working as expected.