Sample code for 30+ languages & platforms
Tcl

SSH ChannelReceiveUntilMatchN (Match Any of Several Prompts)

See more SSH Examples

Demonstrates ChannelReceiveUntilMatchN, which receives until any one of several patterns is matched. This is useful when driving an interactive session where the shell prompt differs from device to device.

Note: This example deliberately requests a PTY. Matching a shell prompt only works in an interactive session — without a PTY the shell runs non-interactively and never prints a prompt at all. For scripted use, omit the PTY and match on a marker your own command echoes.

Background: Real sessions have more than one possible next output, so waiting for one specific string risks hanging when something else appears. Matching a set lets the code proceed on whichever actually occurs — a user prompt, a root prompt, or a device-specific one. A "dumb" terminal type is requested so the remote side does not mix ANSI escape sequences into the output, which would otherwise complicate matching. Remember that retrieving text clears the receive buffer; leaving a prompt buffered would make the next receive match immediately and return the wrong output.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  This example requires the Chilkat API to have been previously unlocked.
#  See Global Unlock Sample for sample code.

#  Demonstrates ChannelReceiveUntilMatchN, which receives until ANY one of several patterns is
#  matched.  This is useful when driving an interactive session where the shell prompt varies
#  from device to device.
#  
#  Note: This example deliberately DOES request a PTY.  Matching a shell prompt only works in an
#  interactive session -- without a PTY the shell runs non-interactively and never prints a
#  prompt at all.  For scripted use where no prompt is needed, omit the PTY and match on a
#  marker your own command echoes instead.

set ssh [new_CkSsh]

#  The remote shell prompt may be any of these, depending on the device.
set saPrompts [new_CkStringArray]

CkStringArray_Append $saPrompts "~$"
CkStringArray_Append $saPrompts "mars#"
CkStringArray_Append $saPrompts "jupiter%"
CkStringArray_Append $saPrompts "chilkat$"
CkStringArray_Append $saPrompts "admin>"

set hostname "ssh.example.com"
set port 22
set success [CkSsh_Connect $ssh $hostname $port]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

#  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.
set password "mySshPassword"

set success [CkSsh_AuthenticatePw $ssh "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set channelNum [CkSsh_OpenSessionChannel $ssh]
if {$channelNum < 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

#  Request a pseudo-terminal so the shell runs interactively and prints prompts.  A "dumb"
#  terminal is used so that ANSI escape sequences are not mixed into the output.
set termType "dumb"
set widthInChars 120
set heightInChars 40
set pixWidth 0
set pixHeight 0
set success [CkSsh_SendReqPty $ssh $channelNum $termType $widthInChars $heightInChars $pixWidth $pixHeight]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set success [CkSsh_SendReqShell $ssh $channelNum]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

#  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.
CkSsh_put_ReadTimeoutMs $ssh 15000

set caseSensitive 1

#  Read the initial login text up to the first prompt.
set success [CkSsh_ChannelReceiveUntilMatchN $ssh $channelNum $saPrompts "utf-8" $caseSensitive]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set cmdOutput [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

puts "$cmdOutput"

#  ---- 1st command ----
set success [CkSsh_ChannelSendString $ssh $channelNum "cd /tmp\n" "utf-8"]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set success [CkSsh_ChannelReceiveUntilMatchN $ssh $channelNum $saPrompts "utf-8" $caseSensitive]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

#  Retrieving the text also clears the receive buffer, which matters: if a prompt were left
#  buffered, the next receive would match it immediately and return the wrong output.
set cmdOutput [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

puts "$cmdOutput"

#  ---- 2nd command ----
set success [CkSsh_ChannelSendString $ssh $channelNum "ls\n" "utf-8"]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set success [CkSsh_ChannelReceiveUntilMatchN $ssh $channelNum $saPrompts "utf-8" $caseSensitive]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set cmdOutput [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

puts "$cmdOutput"

#  Finish the session.
set success [CkSsh_ChannelSendEof $ssh $channelNum]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set success [CkSsh_ChannelReceiveToClose $ssh $channelNum]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

set cmdOutput [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    delete_CkStringArray $saPrompts
    exit
}

puts "$cmdOutput"

CkSsh_Disconnect $ssh

delete_CkSsh $ssh
delete_CkStringArray $saPrompts