SSH with plink

Just starting out? Need help? Post your questions and find answers here.
spacewalker
User
User
Posts: 19
Joined: Tue Apr 27, 2010 4:35 pm
Location: Germany

SSH with plink

Post 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
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSH with plink

Post 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
spacewalker
User
User
Posts: 19
Joined: Tue Apr 27, 2010 4:35 pm
Location: Germany

Re: SSH with plink

Post by spacewalker »

OK, thank you for the information... so I'll wait for 5.31
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSH with plink

Post by infratec »

You don't have to wait :!:

5.31b1 is ready :mrgreen:
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSH with plink

Post by infratec »

:cry:

5.31b1 is still not working as expected.
Post Reply