Android™
Android™
SSH Commands to a Sophos Router Device Console
See more SSH Examples
Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its device console session. The router presents a numbered main menu; the example selects the Device Console entry and then runs commands at the resulting console> prompt.
Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.
Background: Menu-driven appliances require the client to navigate the interface exactly as a human would — read the menu, send the keystroke that selects an entry, then wait for the next prompt. Because the prompt changes as you move between menu levels, the pattern you match on must change too, which is why this example matches the menu text first and
console> afterward.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its
// device console session.
//
// Note: QuickShell allocates a PTY, which is what a device console expects. Navigating the
// device's menus is a matter of reading up to each prompt and sending the expected keystrokes.
CkSsh ssh = new CkSsh();
int port = 22;
success = ssh.Connect("172.16.16.16",port);
if (success == false) {
Log.i(TAG, 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.
String password = "mySshPassword";
success = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
int channelNum = ssh.QuickShell();
if (channelNum < 0) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
// which means no limit -- without it, this call waits forever if the received data never
// contains a match.
ssh.put_ReadTimeoutMs(15000);
boolean caseSensitive = true;
// The router presents a numbered main menu ending with "Select Menu Number [0-7]: ".
success = ssh.ChannelReceiveUntilMatch(channelNum,"Select Menu Number","utf-8",caseSensitive);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
String cmdOutput = ssh.getReceivedText(channelNum,"utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, cmdOutput);
// Choose menu item 4 (Device Console), exactly as if typing it in a terminal.
success = ssh.ChannelSendString(channelNum,"4\n","utf-8");
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// The device console prompt is "console>".
success = ssh.ChannelReceiveUntilMatch(channelNum,"console>","utf-8",caseSensitive);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
cmdOutput = ssh.getReceivedText(channelNum,"utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, cmdOutput);
// Send a console command and read its output up to the next prompt.
success = ssh.ChannelSendString(channelNum,"dnslookup host google.com\n","utf-8");
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
success = ssh.ChannelReceiveUntilMatch(channelNum,"console>","utf-8",caseSensitive);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
cmdOutput = ssh.getReceivedText(channelNum,"utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, cmdOutput);
// Additional commands follow the same pattern.
success = ssh.ChannelSendString(channelNum,"dnslookup host microsoft.com\n","utf-8");
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
success = ssh.ChannelReceiveUntilMatch(channelNum,"console>","utf-8",caseSensitive);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
cmdOutput = ssh.getReceivedText(channelNum,"utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, cmdOutput);
ssh.Disconnect();
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}