Sample code for 30+ languages & platforms
Objective-C

Set a TTY Mode for an SSH PTY Request

See more SSH Examples

Demonstrates the Chilkat Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in a later SendReqPty request. The first argument is the mode name and the second is its integer value. Call it once per mode, before requesting the PTY.

Background: TTY modes are the terminal settings a PTY is created with — the same knobs stty exposes. The classic use is SetTtyMode("ECHO", 0), which stops the terminal from echoing input; that is how a password prompt keeps typed characters off the screen. Modes accumulate on the object and are sent with the next PTY request.

Chilkat Objective-C Downloads

Objective-C
#import <CkoSsh.h>
#import <NSString.h>

BOOL success = NO;

//  Demonstrates the Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in
//  a later SendReqPty request.  The 1st argument is the mode name and the 2nd is its integer
//  value.  Call it once per mode, before SendReqPty.

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;
}

//  Request that the allocated terminal not echo input.
success = [ssh SetTtyMode: @"ECHO" value: [NSNumber numberWithInt: 0]];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

//  The modes set above are included in the PTY request.
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;
}

[ssh Disconnect];