C# Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

C# Examples

Bounced Mail
Bz2
Character Encoding
CSV
Digital Certificates
Digital Signatures
Email
FTP
HTML to XML
HTTP
IMAP
Encryption
MHT / HTML Email
MIME
POP3
RSA
S/MIME
SMTP
Socket
Spider
SSH
SSH Tunnel
SSH Key
SFTP
Tar Archive
Upload
XML
XMP
Zip Compression


More Examples...
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

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".

Download Chilkat .NET for 4.0 Framework

Download Chilkat .NET for 64-bit 4.0 Framework (x64)

Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 64-bit 2.0 / 3.5 Framework (x64)

Download Chilkat .NET for 1.0 / 1.1 Framework

//  Important: It is helpful to send the contents of the
//  ssh.LastErrorText property when requesting support.

Chilkat.Ssh ssh = new Chilkat.Ssh();

//  Any string automatically begins a fully-functional 30-day trial.
bool success;
success = ssh.UnlockComponent("30-day trial");
if (success != true) {
    MessageBox.Show(ssh.LastErrorText);
    return;
}

string hostname;
int port;

//  Hostname may be an IP address or hostname:
hostname = "192.168.1.117";
port = 22;

//  Keep a session log, which is available via the SessionLog
//  property:
ssh.KeepSessionLog = true;

success = ssh.Connect(hostname,port);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  When reading, if no additional data arrives for more than
//  5 seconds, then abort:
ssh.IdleTimeoutMs = 5000;

//  SSH Server Authentication
//  If there is no login/password required, you must still call
//  AuthenticatePw and use any values for login/password.
success = ssh.AuthenticatePw("chilkat","myPassword");
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Open a session channel.
int channelNum;
channelNum = ssh.OpenSessionChannel();
if (channelNum < 0) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Request a pseudo-terminal
string termType;
termType = "dumb";
int widthInChars;
widthInChars = 120;
int heightInChars;
heightInChars = 40;
int pixWidth;
pixWidth = 0;
int pixHeight;
pixHeight = 0;
success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Start a shell on the channel:
success = ssh.SendReqShell(channelNum);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    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.)
string cmd;
cmd = "su" + "\n";
success = ssh.ChannelSendString(channelNum,cmd,"ansi");
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Read until we get the prompt for the password:
success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","ansi",true);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Display what we've received so far:
textBox1.Text += ssh.GetReceivedText(channelNum,"ansi") + "\r\n";

//  Send the password.
//  Again, make sure it uses a bare-LF and not a CRLF.
string password;
password = "myPassword" + "\n";
success = ssh.ChannelSendString(channelNum,password,"ansi");
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Read the response until we get the shell prompt (assuming it's successful)
//  In my case, the shell prompt is: "root@ubuntu:/home/chilkat# "
//  It will be different in your case.
success = ssh.ChannelReceiveUntilMatch(channelNum,"root@ubuntu:/home/chilkat#","ansi",true);
if (success != true) {
    //  Check the last-error information and the session log...
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    //  Check to see what was received.
    textBox1.Text += ssh.GetReceivedText(channelNum,"ansi") + "\r\n";
    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...
textBox1.Text += ssh.GetReceivedText(channelNum,"ansi") + "\r\n";

//  Send a command.  In this case, we are sending the "ls" command:
cmd = "ls" + "\n";
success = ssh.ChannelSendString(channelNum,cmd,"ansi");
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Read until the next command prompt:
success = ssh.ChannelReceiveUntilMatch(channelNum,"root@ubuntu:/home/chilkat#","ansi",true);
if (success != true) {
    //  Check the last-error information and the session log...
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    //  Check to see what was received.
    textBox1.Text += ssh.GetReceivedText(channelNum,"ansi") + "\r\n";
    return;
}

//  Display the command output:
textBox1.Text += ssh.GetReceivedText(channelNum,"ansi") + "\r\n";

//  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(channelNum);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Close the channel:
success = ssh.ChannelSendClose(channelNum);
if (success != true) {
    textBox1.Text += ssh.LastErrorText + "\r\n";
    textBox1.Text += ssh.SessionLog + "\r\n";
    return;
}

//  Disconnect
ssh.Disconnect();
 

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

Email Component · XML Parser