Overview
This article will demonstrate how to execute commands on a server using rsh service using Secure iNet Factory.
Code Example
Download RSHEx.java
01 import com.jscape.inet.bsd.*;
02 import java.io.*;
03
04 public class RSHEx extends BsdAdapter
05 {
06 public void connected(BsdConnectedEvent event)
07 {
08 System.out.println("Connected to host: " + event.getHostname());
09 }
10
11 public void disconnected(BsdDisconnectedEvent event)
12 {
13 System.out.println("Disconnected from host: " + event.getHostname());
14 }
15
16 public static void main(String[] args)
17 {
18 Rsh rsh = new Rsh("remote.host.com", "johndoe", "johndoe","ls -la", false);
19 rsh.addBsdListener(new RSHEx());
20 try
21 {
22 // establish connection
23 rsh.connect();
24 // issue an rsh command
25 String cmd = "someRSHCommand";
26 rsh.setCommand(cmd);
27 // execute command
28 rsh.execute();
29 // get input stream
30 InputStream input = rsh.getInputStream();
31 // read first byte to determine success or failure
32 int in = input.read();
33 StringBuffer buffer = new StringBuffer();
34 if(in == 0) buffer.append("Success! Remote host returned: \n");
35 else buffer.append("Failure! Remote host returned: \n");
36 // read response
37 while((in = input.read()) != -1) buffer.append((char)in);
38 System.out.println(buffer.toString());
39 }
40 catch(Exception e)
41 {
42 System.out.println(e);
43 }
44 }
45 }
46
Lines 1-2 : Necessary import statments.
Lines 6-14 : Events from BsdAdapter
Line 18 : Create an Rsh instance
Line 19 : Add listener to this Rsh instance so this instance can
receive events from BsdAdapter
Lines 25-28 : Set a rsh command and execute it
Lines 30-38 : Read response and print it out to the console.
|
Comments