Contact Info

Crumbtrail

ActiveXperts.com » Network Component » Network Component Objects » SSH

ActiveXperts Network Component Ssh Object

SSH Secure Shell allows secure network services over an insecure network, such as the Internet.

The Secure Shell concept originated on UNIX as a replacement for the insecure "Berkeley services", that is, the rsh , rcp, and rlogin commands. SSH Secure Shell replaces other, insecure terminal applications (such as Telnet and FTP). It allows you to securely login to remote host computers, to execute commands safely in a remote computer, to securely copy remote files, to forward X11 sessions (on UNIX), and to provide secure encrypted and authenticated communications between two non-trusted hosts. Also arbitrary TCP/IP ports can be forwarded over the secure channel, enabling secure connection, for example, to an email service.

SSH Secure Shell with its array of unmatched security features is an essential tool in today's network environment. It is a powerful guardian against the numerous security hazards that nowadays threaten network communications.

With the Network Component Ssh object, you can login onto a remote machine running the SSH daemon, and execute a command or shell script. It has no user interface by itself: standard error and standard output are not copied to the console, but are copied to the corresponding object properties (StdErr and StdOut) instead. There are two ways to authenticate: by using a password or by using a 'private key'.

The Network Component Ssh object has the same functionality as the well-known PuTTY/PLink command-line utility, except that it has no user interface by itself: standard error and standard output are not copied to the console, but are copied to the corresponding object properties (StdErr and StdOut) instead.

The Ssh object is part of the Network Component. Overview of all Network Component objects:

DnsServer & DnsRecord - Ftp & FtpFile - Http - Icmp - IPtoCountry - Msn - Ntp - Radius - Rsh - Scp - SFtp - Ssh - SnmpManager - SnmpTrapManager - SnmpMibBrowser - Tcp - Tftp - TraceRoute - Udp - Xen - Wake-on-LAN - Xen (Citrix)


Ssh Sample code

Visual Basic .NET sample: Execute a script on a remote LINUX machine using SSH

Imports AxNetwork

Public Class SshForm
    Inherits System.Windows.Forms.Form

    Private objSsh As Ssh


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents label15 As System.Windows.Forms.Label
    Friend WithEvents label14 As System.Windows.Forms.Label
    Friend WithEvents CheckRequireHostKeyVerification As System.Windows.Forms.CheckBox
    Friend WithEvents label13 As System.Windows.Forms.Label
    Friend WithEvents label12 As System.Windows.Forms.Label
    Friend WithEvents label11 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents label10 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents ctlTimeout As System.Windows.Forms.TextBox
    Friend WithEvents ctlPort As System.Windows.Forms.TextBox
    Friend WithEvents ctlBrowse As System.Windows.Forms.Button
    Friend WithEvents ctlPassword As System.Windows.Forms.TextBox
    Friend WithEvents ctlPrivateKeyFile As System.Windows.Forms.TextBox
    Friend WithEvents ctlRun As System.Windows.Forms.Button
    Friend WithEvents ctlUser As System.Windows.Forms.TextBox
    Friend WithEvents ctlCommand As System.Windows.Forms.TextBox
    Friend WithEvents ctlHost As System.Windows.Forms.TextBox
    Friend WithEvents ctlView As System.Windows.Forms.Button
    Friend WithEvents ctlLogFile As System.Windows.Forms.TextBox
    Friend WithEvents ctlStdErr As System.Windows.Forms.TextBox
    Friend WithEvents ctlStdOut As System.Windows.Forms.TextBox
    Friend WithEvents ctlResult As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    ...
    End Sub

#End Region

    Private Sub SshForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  Handles MyBase.Load
        objSsh = New Ssh
    End Sub

    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  Handles ctlBrowse.Click
        Dim fdlg As System.Windows.Forms.OpenFileDialog

        fdlg = New OpenFileDialog
        fdlg.Title = "Select Attachment"
        fdlg.InitialDirectory = "C:\"
        fdlg.Filter = "PPK files (*.ppk)|*.ppk"
        fdlg.FilterIndex = 1
        fdlg.RestoreDirectory = True
        If (fdlg.ShowDialog() <> DialogResult.OK) Then
            Exit Sub
        End If

        ctlPrivateKeyFile.Text = fdlg.FileName
    End Sub

    Private Sub ButtonRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  Handles ctlRun.Click
        objSsh.Command = ctlCommand.Text
        objSsh.Host = ctlHost.Text
        objSsh.Port = Int16.Parse(ctlPort.Text)
        objSsh.RequireHostVerification = CheckRequireHostKeyVerification.Checked
        objSsh.UserName = ctlUser.Text
        objSsh.Password = ctlPassword.Text
        objSsh.PrivateKeyFile = ctlPrivateKeyFile.Text
        objSsh.ScriptTimeOut = Int32.Parse(ctlTimeout.Text)
        objSsh.LogFile = ctlLogFile.Text

        ' Clear output form-fields, i.e. stdout and stderr
        ctlStdOut.Text = ""
        ctlStdErr.Text = ""

        Cursor.Current = Cursors.WaitCursor

        objSsh.Run()

        Cursor.Current = Cursors.Default

        ctlResult.Text = objSsh.LastError & ": " & objSsh.GetErrorDescription(objSsh.LastError)

        If (objSsh.LastError = 0) Then
            ctlStdOut.Text = objSsh.StdOut.ReplaceEx(vbLf, vbCrLf)
            ctlStdErr.Text = objSsh.StdErr.ReplaceEx(vbLf, vbCrLf)
        End If
    End Sub

    Private Sub buttonView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  Handles ctlView.Click
        If (System.IO.File.Exists(ctlLogFile.Text.ToString())) Then
            System.Diagnostics.Process.Start(ctlLogFile.Text)
        End If
    End Sub

End Class

Visual C# .NET sample: Execute a script on a remote LINUX machine using SSH

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using AxNetwork;


namespace Demo
{
  /// <summary>
  /// Summary description for SshForm.
  /// </summary>
  public class SshForm : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.GroupBox GroupBox2;
    private System.Windows.Forms.Label label10;
    internal System.Windows.Forms.Label Label9;
    internal System.Windows.Forms.Label Label8;
    internal System.Windows.Forms.Label Label7;
    internal System.Windows.Forms.GroupBox GroupBox1;
    internal System.Windows.Forms.Label Label6;
    internal System.Windows.Forms.Label Label3;
    internal System.Windows.Forms.Label Label2;
    internal System.Windows.Forms.Label Label1;
    internal System.Windows.Forms.Label label11;
    internal System.Windows.Forms.Label label12;
    internal System.Windows.Forms.Label label13;
    internal System.Windows.Forms.Label label14;
    internal System.Windows.Forms.Label label15;
    private System.Windows.Forms.Button ctlView;
    private System.Windows.Forms.TextBox ctlLogFile;
    internal System.Windows.Forms.TextBox ctlStdErr;
    internal System.Windows.Forms.TextBox ctlStdOut;
    internal System.Windows.Forms.TextBox ctlResult;
    internal System.Windows.Forms.TextBox ctlTimeout;
    private System.Windows.Forms.CheckBox ctlRequireHostKeyVerification;
    internal System.Windows.Forms.TextBox ctlPort;
    private System.Windows.Forms.Button ctlBrowse;
    internal System.Windows.Forms.TextBox ctlPassword;
    internal System.Windows.Forms.TextBox ctlPrivateKeyFile;
    internal System.Windows.Forms.Button ctlRun;
    internal System.Windows.Forms.TextBox ctlUser;
    internal System.Windows.Forms.TextBox ctlCommand;
    internal System.Windows.Forms.TextBox ctlHost;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public SshForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        ...

    }
    #endregion

    private Ssh objSsh;

    private void SshForm_Load(object sender, System.EventArgs e)
    {
      objSsh = new Ssh();
      objSsh.Clear ();
      ctlTimeout.Text = objSsh.ScriptTimeOut.ToString();
      ctlLogFile.Text = System.IO.Path.GetTempPath() + "SshLog.txt";
    }

    private void ctlRun_Click(object sender, System.EventArgs e)
    {
      objSsh.Command      = ctlCommand.Text;
      objSsh.Host        = ctlHost.Text;
      objSsh.Port        = Int16.Parse(ctlPort.Text);
      objSsh.RequireHostVerification = ctlRequireHostKeyVerification.Checked ?-1:0;
      objSsh.UserName      = ctlUser.Text;
      objSsh.Password      = ctlPassword.Text;
      objSsh.PrivateKeyFile  = ctlPrivateKeyFile.Text;
      objSsh.ScriptTimeOut  = Int32.Parse(ctlTimeout.Text);
      objSsh.LogFile      = ctlLogFile.Text;

      // Clear output form-fields, i.e. stdout and stderr
      ctlStdOut.Text = "";
      ctlStdErr.Text = "";

      Cursor.Current = Cursors.WaitCursor;

      objSsh.Run();

      Cursor.Current = Cursors.Default;

      ctlResult.Text = objSsh.LastError 

      if (objSsh.LastError == 0)
      {
        ctlStdOut.Text = objSsh.StdOut.ReplaceEx( "\n", "\r\n" );
        ctlStdErr.Text = objSsh.StdErr.ReplaceEx( "\n", "\r\n" );
      }
    }

    private void ctlBrowse_Click(object sender, System.EventArgs e)
    {
      System.Windows.Forms.OpenFileDialog fdlg;

      fdlg = new OpenFileDialog();
      fdlg.Title = "Select Attachment";
      fdlg.InitialDirectory = "C:\\";
      fdlg.Filter = "PPK files (*.ppk)|*.ppk";
      fdlg.FilterIndex = 1;
      fdlg.RestoreDirectory = true;
      if(fdlg.ShowDialog() != DialogResult.OK )
        return;

      ctlPrivateKeyFile.Text = fdlg.FileName;
    }

    private void ctlView_Click(object sender, System.EventArgs e)
    {
      if (System.IO.File.Exists( ctlLogFile.Text.ToString()))
      {
        System.Diagnostics.Process.Start( ctlLogFile.Text);
      }
    }

  }
}

You can download the full samples here.