Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SSH » ColdFusion

ColdFusion SSH Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.



Step 1: Download and install Network Component

Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new ColdFusion document

Create a new blank webdocument with the ".cfm" extention. First of all we are going to build the form whitch commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.

Step 3: Implementation

Before we can use the Network Component to perform lookups, we have to create an instance of the Rsh object. Use the following ColdFusion code to do this:

 <cfobject class="AxNetwork.Ssh" type="com" name="objSocket" Action="Create"> 

Appendix: Full source code

<!--- creating the object --->
<cfobject class="AxNetwork.Ssh" type="com" name="objSsh" Action="Create">
<cfobject class="AxNetwork.NwConstants" type="com" name="objConstants" Action="Create">

<cfoutput>
<html>
<head>
 <title>ActiveXperts Network Component Ssh Sample in ColdFusion</title>
  <style>
  <!--
  body{
    text-align: center;
  }
  h2{
   font-family: verdana;
   color: navy;
   font-weight: bold;
  }
  form{
   margin: 0px;
  }
  .input{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   background-color: white;
   width: 325px;
  }
  
  .button{
   font-family: verdana;
   font-size: 9pt;   
   width: 325px;
  }
  .table{
   font-family: verdana;
   font-size: 9pt;
   color: navy;
   border: 1px solid navy;
   width: 500px;
  }
  div{
    width: 325px; 
    height: 200px; 
    overflow: auto; 
    text-align: left; 
    font-size: xx-small; 
    font-family: verdana;
    border: 1px solid navy;
  }
  a{
    font-family: verdana;
    font-size: 8pt;
    color: red;
  }
  -->
 </style>
</head>
<body>
</cfoutput>

<cfscript>
    strHost = "192.168.31.98";    
    nPort = 22;
    strUser = "root";
    strPassword = "";
    strPrivateKeyFile = "";
    strCommand = "/bin/ls -als $HOME";  
    nTimeout = 10000;

    strStdOut = "";
    strStdErr = "";
    strResult = "";
    
    strLogfile = GetTempDirectory() & "Ssh.log";
    strComponent = "Module [" & objSsh.Module & "]; Build [" & objSsh.Build & "]";
    
    if (isDefined("URL.txtHost")){
      strHost = URL.txtHost;
    }   

    if (isDefined("URL.txtPort")){
      nPort = URL.txtPort;
    }     
    
    if (isDefined("URL.txtUser")){
        strUser = URL.txtUser;
    }
    
    if (isDefined("URL.txtPassword")){
        strPassword = URL.txtPassword;
    }
    
    if (isDefined("URL.txtPrivateKeyFile")){
        strPrivateKeyFile = URL.txtPrivateKeyFile;
    }
    
    if (isDefined("URL.txtCommand")){
      strCommand = URL.txtCommand;
    }     
    
    if (isDefined("URL.txtTimeout")){
      nTimeout = URL.txtTimeout;
    } 
    
    if (isDefined("URL.txtLogfile")){
      strLogfile = URL.txtLogfile;
    }

    if(IsDefined("URL.Submitbutton")){
      objSsh.Logfile = strLogfile;
      objSsh.Host = strHost;
      objSsh.Username = strUser;
      objSsh.Password = strPassword;
      objSsh.PrivateKeyFile = strPrivateKeyFile;
      objSsh.Command = strCommand;
      objSsh.ScriptTimeOut = nTimeout;
      objSsh.Run();
      
      strStdOut = objSsh.StdOut;
      strStdErr = objSsh.StdErr;

      strResult = objSsh.LastError & ": " & objSsh.GetErrorDescription(objSsh.LastError);	
    }
</cfscript>



<cfoutput>

</head>
<body>
<form method=get>

<h2>ActiveXperts Network Component CF Sample - SSH</h2>

<table class=table>
 <tr>
  <td>Component:</td>
  <td>#strComponent#</td>
 </tr>
 <tr>
  <td>Remote Host:</td>
  <td><input class=input type=text name=txtHost value="#strHost#"></td>
 </tr>
 <tr>
  <td>Port:</td>
  <td><input class=input type=text name=txtPort value="#nPort#"></td>
 </tr>
 <tr>
  <td>User:</td>
  <td><input class=input type=text name=txtUser value="#strUser#"></td>
 </tr>
 <tr>
  <td>Password:</td>
  <td><input class=input type=password name=txtPassword value="#strPassword#"></td>
 </tr>
 <tr>
  <td>Private Key File:</td>
  <td><input class=input type=text name=txtPrivateKeyFile value="#strPrivateKeyFile#"></td>
 </tr>
 <tr>
  <td>Run this Command:</td>
  <td><input class=input type=text name=txtCommand value="#strCommand#"></td>
 </tr>
 <tr>
  <td>Command Timeout (msecs):</td>
  <td><input class=input type=text name=txtTimeout value="#nTimeout#"></td>
 </tr>
 <tr>
    <td> </td>
    <td><input class=button type=submit name=submitbutton value="Run Command"></td>
 </tr>
 <tr>
    <td>Logfile:</td>
    <td><input class=input type=text name=txtLogfile value="#strLogfile#"></td>
 </tr>
 <tr>
  <td><b>Result: </b></td>
  <td>#strResult#</td>
 </tr>
<tr>
  <td>StdOut:</td>
  <td><div>#strStdOut#</div></td>
</tr>
<tr>
  <td>StdErr:</td>
  <td><div>#strStdErr#</div></td>
</tr>
</table>

</form>
<br />


<p>This sample is based on ActiveXperts Network Component, an <a target="blank" href="https://www.activexperts.com">ActiveXperts Software</a> product.</p>
<a href="index.cfm">Back to main page</a>

</body>
</html>

</cfoutput>

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.