Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) SSH -- Running Commands that Prompt for Additional Input, such as "su"

Demonstrates how to run a shell command via SSH where the shell command prompts for additional input from the client. This example demonstrates "su".

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoSsh.h>
#import <NSString.h>

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

CkoSsh *ssh = [[CkoSsh alloc] init];

// Hostname may be an IP address or hostname:
NSString *hostname = @"172.16.16.46";
int port = 22;

BOOL success = [ssh Connect: hostname port: [NSNumber numberWithInt: port]];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// If receiving output from a command stalls for more than 5 seconds, then stop waiting.
ssh.IdleTimeoutMs = [NSNumber numberWithInt:5000];

success = [ssh AuthenticatePw: @"myLogin" password: @"myPassword"];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Open a session channel. 
int channelNum = [[ssh OpenSessionChannel] intValue];
if (channelNum < 0) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Request a pseudo-terminal
NSString *termType = @"dumb";
int widthInChars = 120;
int heightInChars = 40;
int pixWidth = 0;
int pixHeight = 0;
success = [ssh SendReqPty: [NSNumber numberWithInt: channelNum] xTermEnvVar: termType widthInChars: [NSNumber numberWithInt: widthInChars] heightInRows: [NSNumber numberWithInt: heightInChars] pixWidth: [NSNumber numberWithInt: pixWidth] pixHeight: [NSNumber numberWithInt: pixHeight]];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Start a shell on the channel:
success = [ssh SendReqShell: [NSNumber numberWithInt: channelNum]];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Send the su command.
// (The SSH server I'm using for testing is a Linux Ubuntu
// system running OpenSSH.  It is important in this case to send a bare-LF
// and not a CRLF.)
NSString *cmd = @"su\n";
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: cmd charset: @"ansi"];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Read until we get the prompt for the password:
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: @"Password:" charset: @"ansi" caseSensitive: YES];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Display what we've received so far:
NSLog(@"%@",[ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"ansi"]);

// Send the password. (This is the equivalent of typing at the shell prompt.)
// Again, make sure it uses a bare-LF and not a CRLF.
NSString *password = 0;
password = @"myPassword\n";
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: password charset: @"ansi"];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Read the response until we get the shell prompt (assuming it's successful)
// In my case, the shell prompt is: "chilkat@vivoMini64:~$ "
NSString *myShellPrompt = @"chilkat@vivoMini64:~$";
// It will be different in your case.
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: myShellPrompt charset: @"ansi" caseSensitive: YES];
if (success != YES) {
    // Check the last-error information and the session log...
    NSLog(@"%@",ssh.LastErrorText);
    // Check to see what was received.  
    NSLog(@"%@",[ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"ansi"]);
    return;
}

// Display what we've received so far.  This clears
// the internal receive buffer, which is important. 
// After we send the command, we'll be reading until
// the next command prompt.  If the command prompt
// is already in the internal receive buffer, we'll think we're
// already finished...
NSLog(@"%@",[ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"ansi"]);

// Send a command.  In this case, we are sending the "ls" command:
cmd = @"ls\n";
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: cmd charset: @"ansi"];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Read until the next command prompt:
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: myShellPrompt charset: @"ansi" caseSensitive: YES];
if (success != YES) {
    // Check the last-error information and the session log...
    NSLog(@"%@",ssh.LastErrorText);
    // Check to see what was received.  
    NSLog(@"%@",[ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"ansi"]);
    return;
}

// Display the command output:
NSLog(@"%@",[ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"ansi"]);

// You may continue sending additional commands.
// The technique is: send the command, read until the next command prompt,
// and then fetch/clear the internal receive buffer.

// We're done, so shut it down..

// Send an EOF.  This tells the server that no more data will 
// be sent on this channel.  The channel remains open, and
// the SSH client may still receive output on this channel.
success = [ssh ChannelSendEof: [NSNumber numberWithInt: channelNum]];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Close the channel:
success = [ssh ChannelSendClose: [NSNumber numberWithInt: channelNum]];
if (success != YES) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

// Disconnect
[ssh Disconnect];
 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.