Coverage Report - expectj.SshSpawn
 
Classes in this File Line Coverage Branch Coverage Complexity
SshSpawn
53%
28/52
64%
9/14
2,333
 
 1  
 package expectj;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 import java.io.OutputStream;
 6  
 import com.jcraft.jsch.Channel;
 7  
 import com.jcraft.jsch.JSch;
 8  
 import com.jcraft.jsch.JSchException;
 9  
 import com.jcraft.jsch.Session;
 10  
 
 11  
 /**
 12  
  * A Spawnable for controlling an SSH session using ExpectJ.
 13  
  */
 14  
 public class SshSpawn extends AbstractSpawnable implements Spawnable {
 15  
     /**
 16  
      * A reference to the remote host.
 17  
      */
 18  
     private String m_remoteHost;
 19  
 
 20  
     /**
 21  
      * The port we're talking to on the remote host.
 22  
      */
 23  
     private int m_remotePort;
 24  
 
 25  
     /**
 26  
      * Our communications channel to the remote host.
 27  
      */
 28  1
     private Session m_session = null ;
 29  
 
 30  
     /**
 31  
      * Use this to read data from the remote host.
 32  
      */
 33  
     private OutputStream m_fromSocket;
 34  
 
 35  
     /**
 36  
      * Use this to write data to the remote host.
 37  
      */
 38  
     private InputStream m_toSocket;
 39  
 
 40  
     /**
 41  
      * The username with which to authenticate
 42  
      */
 43  1
     private String m_username = null ;
 44  
 
 45  
     /**
 46  
      * The password with which to authenticate
 47  
      */
 48  1
     private String m_password = null ;
 49  
 
 50  
     /**
 51  
      * The JSch Channel of type "shell"
 52  
      */
 53  1
     private Channel m_channel = null ;
 54  
 
 55  
     /**
 56  
      * Construct a new SSH spawn.
 57  
      * @param remoteHostName The remote host to connect to.
 58  
      * @param remotePort The remote port to connect to.
 59  
      * @param username The user name with which to authenticate
 60  
      * @param password The password with which to authenticate
 61  
      */
 62  0
     public SshSpawn(String remoteHostName, int remotePort, String username, String password) {
 63  0
         m_remotePort = remotePort;
 64  0
         m_remoteHost = remoteHostName ;
 65  0
         this.m_username = username ;
 66  0
         this.m_password = password ;
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Takes control over an existing SSH channel.
 71  
      *
 72  
      * @param channel The channel we should control.  If this channel isn't
 73  
      * already connected, {@link Channel#connect()} will be called.
 74  
      *
 75  
      * @throws IOException If connecting the channel fails.
 76  
      */
 77  1
     public SshSpawn(Channel channel) throws IOException {
 78  1
         if (!channel.isConnected()) {
 79  
             try {
 80  1
                 channel.connect();
 81  0
             } catch (JSchException e) {
 82  0
                 throw new IOException("Failed connecting the channel", e) ;
 83  1
             }
 84  
         }
 85  
 
 86  1
         this.m_channel = channel;
 87  1
         m_toSocket = m_channel.getInputStream();
 88  1
         m_fromSocket = m_channel.getOutputStream();
 89  1
     }
 90  
 
 91  
     public void start() throws IOException {
 92  1
         if (m_toSocket != null) {
 93  
             // We've probably been created by the SshSpawn(Channel) constructor,
 94  
             // or start() has already been called.  No need to do anything
 95  
             // anyway.
 96  1
             return;
 97  
         }
 98  
 
 99  
             try {
 100  0
                         m_session = new JSch().getSession(m_username, m_remoteHost, m_remotePort) ;
 101  0
                         m_session.setPassword(m_password) ;
 102  0
                         m_session.setConfig("StrictHostKeyChecking", "no");
 103  0
                         m_session.connect() ;
 104  0
                         m_channel = m_session.openChannel("shell") ;
 105  0
                         m_channel.connect() ;
 106  0
                 } catch (JSchException e) {
 107  0
                         throw new IOException("Unable to establish SSH session/channel", e) ;
 108  0
                 }
 109  0
         m_toSocket = m_channel.getInputStream() ;
 110  0
         m_fromSocket = m_channel.getOutputStream();
 111  0
     }
 112  
 
 113  
     public InputStream getStdout() {
 114  1
         return m_toSocket;
 115  
     }
 116  
 
 117  
     public OutputStream getStdin() {
 118  1
         return m_fromSocket;
 119  
     }
 120  
 
 121  
     public InputStream getStderr() {
 122  1
         return null;
 123  
     }
 124  
 
 125  
     public boolean isClosed() {
 126  2
         if (m_channel != null) {
 127  1
             if (m_channel.isClosed()) {
 128  
                 // We've been disconnected, shut down
 129  0
                 stop();
 130  
             }
 131  
         }
 132  2
         return m_channel == null;
 133  
     }
 134  
 
 135  
     public int getExitValue() {
 136  1
         return 0;
 137  
     }
 138  
 
 139  
     public void stop() {
 140  1
         if (m_channel == null) {
 141  0
             return;
 142  
         }
 143  
 
 144  1
         m_channel.disconnect();
 145  1
                 m_channel = null;
 146  
 
 147  1
                 if (m_session != null) {
 148  0
                     m_session.disconnect();
 149  0
                     m_session = null;
 150  
                 }
 151  1
         m_toSocket = null;
 152  1
         m_fromSocket = null;
 153  1
     }
 154  
 }