Objective-C
Objective-C
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 Objective-C Downloads
#import <CkoSsh.h>
#import <NSString.h>
BOOL success = NO;
// 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.
CkoSsh *ssh = [[CkoSsh alloc] init];
int sshPort = 22;
success = [ssh Connect: @"ssh.example.com" port: [NSNumber numberWithInt: sshPort]];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// 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.
NSString *password = @"mySshPassword";
success = [ssh AuthenticatePw: @"mySshLogin" password: password];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// Open a session channel. A negative return value indicates failure.
int channelNum = [[ssh OpenSessionChannel] intValue];
if (channelNum < 0) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// Allocate a PTY and start a shell.
success = [ssh SendReqPty: [NSNumber numberWithInt: channelNum] xTermEnvVar: @"xterm" widthInChars: [NSNumber numberWithInt: 80] heightInRows: [NSNumber numberWithInt: 24] pixWidth: [NSNumber numberWithInt: 0] pixHeight: [NSNumber numberWithInt: 0]];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
success = [ssh SendReqShell: [NSNumber numberWithInt: channelNum]];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// The user resized the terminal window -- tell the server the new dimensions.
int newWidthInChars = 132;
int newHeightInRows = 43;
int pixWidth = 0;
int pixHeight = 0;
success = [ssh SendReqWindowChange: [NSNumber numberWithInt: channelNum] widthInChars: [NSNumber numberWithInt: newWidthInChars] heightInRows: [NSNumber numberWithInt: newHeightInRows] pixWidth: [NSNumber numberWithInt: pixWidth] pixHeight: [NSNumber numberWithInt: pixHeight]];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
NSLog(@"%@",@"Window change sent.");
[ssh Disconnect];