Visual FoxPro
Visual FoxPro
SSH Commands to a Cisco Switch
See more SSH Examples
Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending commands in a device console session. Each command is sent, and its output is read up to the device's next prompt.
Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.
Background: Network devices differ from general-purpose servers in that they expose a menu- or prompt-driven console rather than a Unix shell, so
exec requests are usually unavailable and driving the interactive console is the only option. That makes prompt matching the synchronization mechanism, and it is why a PTY is appropriate here despite the general advice to avoid one. Retrieving the received text between commands is essential: a prompt left in the buffer would make the next receive return immediately with the wrong output.Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnPort
LOCAL lcPassword
LOCAL lnChannelNum
LOCAL lnCaseSensitive
LOCAL lcCmdOutput
lnSuccess = 0
* This example requires the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.
* Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending
* commands in a device console session.
*
* Note: QuickShell allocates a PTY, which is what a network device console expects. The device
* presents an interactive prompt, and each command's output is read up to the next prompt.
loSsh = CreateObject('Chilkat.Ssh')
lnPort = 22
lnSuccess = loSsh.Connect("172.16.16.100",lnPort)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* Normally you would not hard-code the password in source. You should instead obtain it
* from an interactive prompt, environment variable, or a secrets vault.
lcPassword = "mySshPassword"
lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* Start a shell session.
lnChannelNum = loSsh.QuickShell()
IF (lnChannelNum < 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
* which means no limit -- without it, this call waits forever if the received data never
* contains a match.
loSsh.ReadTimeoutMs = 15000
lnCaseSensitive = 1
* The switch presents a prompt ending in "#". Reading up to it confirms the session is ready.
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"#","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
? lcCmdOutput
* Send a command and read its output up to the next prompt.
lnSuccess = loSsh.ChannelSendString(lnChannelNum,"show clock" + CHR(10),"utf-8")
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"#","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* Retrieving the text also clears the receive buffer, which matters: if the prompt were left
* buffered, the next receive would match it immediately and return the wrong output.
lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
? lcCmdOutput
* Additional commands follow the same send / read-to-prompt / retrieve pattern.
lnSuccess = loSsh.ChannelSendString(lnChannelNum,"show version" + CHR(10),"utf-8")
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"#","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
? lcCmdOutput
loSsh.Disconnect()
RELEASE loSsh