Overview
This article will demonstrate how to execute commands on a server using rexec service using Secure iNet Factory.
Code Example
Download RexecEx.java
01 import com.jscape.inet.bsd.*;
02
03 public class RexecEx extends BsdAdapter
04 {
05 public void connected(BsdConnectedEvent event)
06 {
07 System.out.println("Connected to host: " + event.getHostname());
08 }
09
10 public void disconnected(BsdDisconnectedEvent event)
11 {
12 System.out.println("Disconnected from host: " + event.getHostname());
13 }
14
15 public static void main(String[] args)
16 {
17 try
18 {
19 Rexec rexec = new Rexec("server.com", "jsmith", "secret", "ls -al", false);
20 // add listener
21 rexec.addBsdListener(new RexecEx());
22 // establish connection
23 rexec.connect();
24 // executes ls -al command on server.com authenticating as user jsmith
25 rexec.execute();
26 // disconnect
27 rexec.disconnect();
28
29 }
30 catch (Exception e)
31 {
32 e.printStackTrace();
33 }
34 }
35 }
Line 1 : Necessary import. Lines 5-13 : Methods to handle events from BsdAdapter. Line 19 : Create new Rexec instance.
Line 21 : Add listener to Rexec instance so the Rexec instance can receive events. Line 23 : Connect to the remote server. Line 25 : Execute the command. Line 27 : Disconnect from the remote server.
|
Comments