Sample code for 30+ languages & platforms
DataFlex

Notify the SSH Server of a Terminal Resize

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqWindowChange method, which tells the server that the terminal dimensions changed. The arguments are the channel number, the new character width and height, and the pixel width and height. It may be sent any time after the PTY is allocated.

Background: When a user resizes a terminal window, full-screen remote programs such as vi, top, or less need to know the new size or they will keep drawing at the old dimensions. This request delivers that update, causing the server to signal the remote process (SIGWINCH on Unix) so it can redraw correctly.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannelNum
    Integer iNewWidthInChars
    Integer iNewHeightInRows
    Integer iPixWidth
    Integer iPixHeight
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Ssh.SendReqWindowChange method, which notifies the server that the terminal
    //  dimensions changed.  The 1st argument is the channel number, the 2nd and 3rd are the new
    //  character width and height, and the 4th and 5th are the pixel width and height.

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move 22 To iSshPort
    Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Open a session channel.  A negative return value indicates failure.
    Get ComOpenSessionChannel Of hoSsh To iChannelNum
    If (iChannelNum < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Allocate a PTY and start a shell.
    Get ComSendReqPty Of hoSsh iChannelNum "xterm" 80 24 0 0 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSendReqShell Of hoSsh iChannelNum To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The user resized the terminal window -- tell the server the new dimensions.
    Move 132 To iNewWidthInChars
    Move 43 To iNewHeightInRows
    Move 0 To iPixWidth
    Move 0 To iPixHeight
    Get ComSendReqWindowChange Of hoSsh iChannelNum iNewWidthInChars iNewHeightInRows iPixWidth iPixHeight To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Window change sent."

    Send ComDisconnect To hoSsh


End_Procedure