Sample code for 30+ languages & platforms
PureBasic

Request a Pseudo-Terminal (PTY) over SSH

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqPty method, which requests a pseudo-terminal for a session channel. The arguments are the channel number, the terminal type, the character width and height, and the pixel width and height. It must be sent before the primary exec or shell request.

Background: A PTY makes the remote side behave as though a real terminal is attached, which matters for programs that draw screens, prompt for passwords, or colorize output — many refuse to run interactively without one. The terminal type (such as xterm) tells the remote program what escape sequences it may use. Pixel dimensions are commonly 0, meaning unspecified.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.SendReqPty method, which requests a pseudo-terminal for a session
    ;  channel.  The 1st argument is the channel number, the 2nd is the terminal type, the 3rd and
    ;  4th are the character width and height, and the 5th and 6th are the pixel width and height.
    ;  It must be sent before the primary exec or shell request.

    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sshPort.i = 22
    success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    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.
    password.s = "mySshPassword"

    success = CkSsh::ckAuthenticatePw(ssh,"mySshLogin",password)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Open a session channel.  A negative return value indicates failure.
    channelNum.i = CkSsh::ckOpenSessionChannel(ssh)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Request an 80x24 xterm.  Pixel dimensions of 0 mean unspecified.
    termType.s = "xterm"
    widthInChars.i = 80
    heightInChars.i = 24
    widthInPixels.i = 0
    heightInPixels.i = 0
    success = CkSsh::ckSendReqPty(ssh,channelNum,termType,widthInChars,heightInChars,widthInPixels,heightInPixels)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Start the shell now that the PTY is allocated.
    success = CkSsh::ckSendReqShell(ssh,channelNum)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure