Overview
The article will demonstrate how to use the ipclient component in Secure iNet Factory
to create TCP/IP client socket connections over SSH.
To demonstrate, consider the POP (Post Office Protocol) service
which allows you to retrieve email from your account mailbox. To
retrieve your email you must submit a username and password. This
username and password is usually sent to the POP service in clear text
making it susceptible to snooping on the network. While this may not
be a concern on your own corporate intranet it becomes an immediate
concern when accessing your account remotely via the public Internet.
To address this issue you can create a secure SSH tunnel that
encrypts data while travelling over the Internet. An SSH tunnel is a
special type of SSH connection where all data sent by the client is
securely routed to a specified destination hostname/port and any data
received from that hostname/port is securely sent back through the SSH
tunnel to the client.
Code Example
Download IPClientSSHEx.java
01 import java.io.*;
02 import com.jscape.inet.ipclient.IpClientConnectedEvent;
03 import com.jscape.inet.ipclient.IpClientDisconnectedEvent;
04 import com.jscape.inet.ipclient.IpClientListener;
05 import com.jscape.inet.ipclientssh.IpClientSsh;
06 import com.jscape.inet.ssh.util.SshParameters;
07
08 public class IPClientSSHEx implements IpClientListener
09 {
10 public static void main(String[] args)
11 {
12 try
13 {
14 // SSH hostname
15 String sshHostname = "myhost.com";
16 // SSH username
17 String sshUsername = "jsmith";
18 // SSH password
19 String sshPassword = "secret";
20 // POP3 service hostname
21 String popHostname = "myhost.com";
22 // POP3 service port
23 int popPort = 110;
24 // create SSH authentication info
25 SshParameters sshParams = new SshParameters(sshHostname,sshUsername,sshPassword);
26
27 // create new tunnel to specified POP3 server hostname and POP3 service port
28 IpClientSsh tunnel = new IpClientSsh(sshParams,popHostname,popPort);
29
30 // establish tunnel
31 tunnel.connect();
32
33 // get input stream for tunnel
34 InputStream in = tunnel.getInputStream();
35
36 // read data from input stream
37 int b = 0;
38 while((b = in.read()) != -1) {
39 // do something with data
40 }
41
42 // get output stream for tunnelte
43 byte[] buffer = new byte[1024];
44 OutputStream out = tunnel.getOutputStream();
45 out.write(buffer);
46
47 // disconnect
48 tunnel.disconnect();
49 }
50 catch (Exception e)
51 {
52 e.printStackTrace();
53 }
54 }
55
56
57 public void connected(IpClientConnectedEvent event)
58 {
59 System.out.println("Connected to host: " + event.getHostname());
60 }
61
62
63 public void disconnected(IpClientDisconnectedEvent event)
64 {
65 System.out.println("Disconnected from host: " + event.getHostname());
66 }
67
68 }
Lines 1-6 : Necessary import statements. Line 25 : Create a SshParameters instance with the hostname, username and password. Line 28 : Create a IpClientSsh instance. Line 31 : Connect to the host. Line 34-45 : Read the input stream Line 48 : Disconnect from the host
|
Comments