Contact Info

Crumbtrail

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

ColdFusion Telnet 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.Tcp" type="com" name="objSocket" Action="Create"> 

Appendix: Full source code

<!--- creating the object --->
<cfobject class="AxNetwork.Tcp" type="com" name="objSocket" Action="Create">

<cfoutput>
<html>
<head>
 <title>ActiveXperts Network Component Telnet 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: 500; 
    height: 250px; 
    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>
    strLogfile = GetTempDirectory() & "Telnet.log";
    strComponent = "Module [" & objSocket.Module & "]; Build [" & objSocket.Build & "]";
    
    if (isDefined("URL.logfile")){
      strLogfile = URL.logfile;
    }

</cfscript>
<cfoutput>
<h2>ActiveXperts Network Component CF Sample - Telnet</h2>

<form method=get>

<table class=table>
  <tr>
    <td>Component:</td>
    <td>#strComponent#</td>
  </tr>
  <tr>
    <td>Telnet server:</td>
    <td><input class=input type=text name=host value="telnet.activexperts-labs.com"></td>
  </tr>
  <tr>
    <td>Port No.:</td>
    <td><input class=input type=text name=port value="23"></td>
  </tr>
  <tr>
    <td>Username:</td>
    <td><input class=input type=text name=username value="library"></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input class=input type=password name=password value="library"></td>
  </tr>
  <tr>
    <td>Command:</td>
    <td><input class=input type=text name=command value="X"></td>
  </tr>
  <tr>
    <td>Logfile:</td>
    <td><input class=input type=text name=logfile></td>
  </tr>
  <tr>
    <td> </td>
    <td><input class=button type=submit name=submitbutton value="Connect and send a command"></td>
  </tr>
</table>
<br />
</form>
</cfoutput>
<cfif IsDefined("URL.submitbutton")>

   <div>

   <!--- set the logfile --->
   <cfset objSocket.Logfile = URL.logfile>

   <!--- connect to host --->
   <cfscript>
   objSocket.connect(URL.host, URL.port);
   strResult = objSocket.LastError & ":" & objSocket.GetErrorDescription(objSocket.LastError) & "<br>";	 
   objSocket.Sleep(1000);
   receiveData();
   </cfscript>

   <!--- log in --->
   <cfif objSocket.LastError eq 0>
	   <cfset username = URL.username>

     <cfif username eq "">
		 <cfelse>
       <cfscript>
         objSocket.SendString(URL.username);
         objSocket.SendString(URL.password);
         strResult = objSocket.LastError & ":" & objSocket.GetErrorDescription(objSocket.LastError) & "<br>";
         receiveData();
       </cfscript>
		 </cfif>

   <cfelse>
   </cfif>

   <!-- send command -->
   <cfif objSocket.LastError eq 0>	 
     <cfscript>
       strCommand = URL.command;
       objSocket.SendString(strCommand);
       strResult = objSocket.LastError & ": " & objSocket.GetErrorDescription(objSocket.LastError) & "<br>";
		   WriteOutput(strCommand & ":<br>");		 
       receiveData();
     </cfscript>
   <cfelse>
   </cfif>

   <!--- receive the results --->
   <cfscript>
   function receiveData(){

   do{
      crlf = CHR(13) & CHR(10); 
      strOutput = ReplaceEx(objSocket.ReceiveString(), crlf, "<br>", "ALL"); 

      WriteOutput(strOutput & "<br>");
      objSocket.Sleep(1000)	;
   }
   while(objSocket.HasData() eq -1);

   }
   </cfscript>
   </div>
	 
   <br>
   <table class=table>
      <tr>
	      <td>
		     <b>Results:</b> 
        </td>
        <td>
         <cfif objSocket.LastError eq 0>
           <cfoutput>#strResult#</cfoutput>
         <cfelse>
   		     <cfset strResult = objSocket.LastError & ":"  & objSocket.GetErrorDescription(objSocket.LastError)>
		       <cfoutput>#strResult#</cfoutput>				
    			</cfif>
        </td>
      </tr>
    </table>

    <!--- close the object --->
    <cfset objSocket.Disconnect()>

<cfelse>

</cfif>

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

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