Advertisement

08.18.2008 at 08:22AM PDT, ID: 23656662
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.8

How can I get command output from j2ssh client ?

Asked by leydent in SSH / Telnet Software, Miscellaneous Programming, Miscellaneous Security

Tags: ,

Hello,

I am writing a j2ssh client which reads scripts to be run via ssh for health checks etc.  I have an SSHWrapper class which looks after connetion, authetication etc and also have an execute_command function which is to be user to pass commands to the ssh server and cache the response.  The problem is the authentication works fine,  however when I pass a simple command such as ls -ltr to server,  I get ni response.  Please see all code and output below.  Any help greatly appreciated.  ThanksStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
package com;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
 
public class SshExample {
	
 
	
	  public static void main(String args[]) {
 
		  
		  		SSHWrapper ssh = new SSHWrapper();
		  		ssh.hostprompt="";
		  		ssh.hostname="iechbe15";
		  		ssh.username="tibco";
		  		ssh.password="eaiprd00";
		  		ssh.connect();
		  		ssh.execute_command("ls -ltr\n");
		  		
		  		ssh.disconnect();
		  		
		  
		  		
		    	
				return;
				
	    	
	  }	
 
}
 
 
 
package com;
 
import com.sshtools.j2ssh.SshClient; 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;   
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;  
import com.sshtools.j2ssh.session.SessionChannelClient;
 
public class SSHWrapper {
	
 
	  String hostname;
	  String hostprompt;
	  String username;
	  String password;
	  String response;
   	  SshClient ssh;
   	  SessionChannelClient session;
   	  
   	// A buffered reader so we can request information from the user
	  private static BufferedReader reader =
	        new BufferedReader(new InputStreamReader(System.in));
 
	SSHWrapper(){
		hostname="";
		username="";
		password="";
		
	}
 
	
	  public String  connect() {
 
		  String return_value="";
		  String command = "ls -ltr\n";
		  int result=0;
		  boolean prompt_returned=false;
		  
		  try {
 
	    	// Create the new  client 
			ssh = new SshClient();
			return_value="created connection successfuly...";
 
			//connect to the host
	    	ssh.connect(hostname);	   
			return_value="Connected to " + hostname + "successfully...";
 
			/** 
	    	  * Create a PasswordAuthenticationClient instance, set the properties
	    	  * and pass to the SessionClient to authenticate
	    	  */
	    	  PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
	    	  
	    	  pwd.setUsername(this.username);
	    	  pwd.setPassword(this.password);
	    	
	    	  result = ssh.authenticate(pwd);
	    	  return_value = "Login result = " + result;
	    	  
	    	  if(result==AuthenticationProtocolState.FAILED)
	    		     return_value = "The authentication failed";
 
    		  if(result==AuthenticationProtocolState.PARTIAL)
    			  return_value = "The authentication succeeded but another"
	    			                   + "authentication is required";
 
    		  if(result==AuthenticationProtocolState.COMPLETE)
    			  return_value = "The authentication is complete";
    		  
    		  session = ssh.openSessionChannel();
    		  return_value = "session is open successfully...";
    		  
    		  session.requestPseudoTerminal("vt100", 80, 25, 0, 0 , "");
    		  session.startShell();
 
    		  
    		  return_value = "shell started successfully...";
 
    		  
    		  return return_value;
	    	
		} catch(Exception e) {
	      return_value = return_value + "   " + e.getStackTrace();
	      return return_value;
	    }
	  }	
 
	  
	  
	  public String  disconnect() {
 
		  String return_value="";
		  
		  try {
 
	    	// Create the new  client 
    		session.close();
    		reader.close();
    		return_value = "Session closed successfully...";
    		  
    		  return return_value;
	    	
		} catch(Exception e) {
			return_value = return_value + "   " + e.getStackTrace();
			return return_value;
	    }
	  }	
	  
 
	  public String  execute_command(String command ) {
 
		  String return_value="";
		  boolean prompt_returned=false;
		  
		  try {
 
			  response = "";
			  return_value = "Executing command : " + command;
			  System.out.println(return_value);
			  /** Writing to the session OutputStream */
    		  OutputStream out = session.getOutputStream();
    		  out.write(command.getBytes());
    		  
    		  /**
    		  * Reading from the session InputStream
    		  */
    		  InputStream in = session.getInputStream();
    		  byte buffer[] = new byte[255];
    		  int read;
    		  return_value += " : Response=";
    		  while( prompt_returned==false && (read = in.read(buffer))>0 ) {
    			  System.out.println("prompt_returned="+prompt_returned + " / read=" + read);
    			  prompt_returned=false;
    		     response = new String(buffer, 0, read);
    		     return_value += response.toString();
    		     System.out.println(response);
    		     
    		     if ( response.indexOf(this.hostprompt) >= 0 )
    		    	 prompt_returned=true;
    		     
    		  }
    		  return return_value;
	    	
		} catch(Exception e) {
			return_value = return_value + "   " + e.getStackTrace();
			return return_value;
	    }
	  }	
 
	  
	public String getpassword() {
		return this.password;
	}
 
	public void setpassword(String input_value) {
		this.password = input_value;
	}
	  
 
	public String gethostname() {
		return this.hostname;
	}
 
	public void sethostname(String input_value) {
		this.hostname = input_value;
	}
 
	public String getusername() {
		return this.username;
	}
 
	public void setusername(String input_value) {
		this.username = input_value;
	}
 
	
	public String gethostprompt() {
		return this.hostprompt;
	}
 
	public void sethostprompt(String input_value) {
		this.hostprompt = input_value;
	}
	
}
 
18-Aug-2008 16:05:23 com.sshtools.j2ssh.transport.publickey.SshKeyPairFactory <clinit>
INFO: Loading public key algorithms
18-Aug-2008 16:05:24 com.sshtools.j2ssh.configuration.ConfigurationLoader initialize
INFO: JAVA version is 1.5.0_06
18-Aug-2008 16:05:24 com.sshtools.j2ssh.configuration.ConfigurationLoader initialize
INFO: Extension C:\Sun\AppServer\jdk\jre\lib\ext\dnsns.jar being added to classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.util.ExtensionClassLoader add
INFO: Adding C:\Sun\AppServer\jdk\jre\lib\ext\dnsns.jar to the extension classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.configuration.ConfigurationLoader initialize
INFO: Extension C:\Sun\AppServer\jdk\jre\lib\ext\localedata.jar being added to classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.util.ExtensionClassLoader add
INFO: Adding C:\Sun\AppServer\jdk\jre\lib\ext\localedata.jar to the extension classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.configuration.ConfigurationLoader initialize
INFO: Extension C:\Sun\AppServer\jdk\jre\lib\ext\sunjce_provider.jar being added to classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.util.ExtensionClassLoader add
INFO: Adding C:\Sun\AppServer\jdk\jre\lib\ext\sunjce_provider.jar to the extension classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.configuration.ConfigurationLoader initialize
INFO: Extension C:\Sun\AppServer\jdk\jre\lib\ext\sunpkcs11.jar being added to classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.util.ExtensionClassLoader add
INFO: Adding C:\Sun\AppServer\jdk\jre\lib\ext\sunpkcs11.jar to the extension classpath
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.cipher.SshCipherFactory <clinit>
INFO: Loading supported cipher algorithms
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.kex.SshKeyExchangeFactory <clinit>
INFO: Loading key exchange methods
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.compression.SshCompressionFactory <clinit>
INFO: Loading compression methods
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.hmac.SshHmacFactory <clinit>
INFO: Loading message authentication methods
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon startTransportProtocol
INFO: Starting transport protocol
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon run
INFO: Registering transport protocol messages with inputstream
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon negotiateVersion
INFO: Negotiating protocol version
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon negotiateVersion
INFO: Protocol negotiation complete
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon beginKeyExchange
INFO: Starting key exchange
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.kex.DhGroup1Sha1 performClientExchange
INFO: Starting client side key exchange.
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification verifyHost
INFO: Verifying iechbe15,10.162.123.114 host key
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolClient verifyHostKey
INFO: The host key signature is  valid
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.TransportProtocolCommon completeKeyExchange
INFO: Completing key exchange
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.cipher.SshCipherFactory newInstance
INFO: Creating new blowfish-cbc cipher instance
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.cipher.SshCipherFactory newInstance
INFO: Creating new blowfish-cbc cipher instance
18-Aug-2008 16:05:24 com.sshtools.j2ssh.connection.ConnectionProtocol onServiceInit
INFO: Registering connection protocol messages
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.Service start
INFO: ssh-connection has been requested
18-Aug-2008 16:05:24 com.sshtools.j2ssh.transport.AsyncService onStart
INFO: Starting ssh-connection service thread
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol openChannel
INFO: Channel 0 is open [session]
18-Aug-2008 16:05:25 com.sshtools.j2ssh.session.SessionChannelClient requestPseudoTerminal
INFO: Requesting pseudo terminal
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Sending pty-req request for the session channel
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Waiting for channel request reply
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Channel request succeeded
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Sending shell request for the session channel
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Waiting for channel request reply
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol sendChannelRequest
INFO: Channel request succeeded
Executing command : ls -ltr
 
prompt_returned=false / read=146
Last   successful login for tibco: Mon Aug 18 16:04:59 GMT0BST 2008  
Last unsuccessful login for tibco: Tue Aug 12 09:23:15 GMT0BST 2008      
 
18-Aug-2008 16:05:25 com.sshtools.j2ssh.connection.ConnectionProtocol closeChannel
INFO: Local computer has closed channel 0[session]
 
Keywords: How can I get command output from j2…
 
Loading Advertisement...
 
[+][-]08.19.2008 at 08:58PM PDT, ID: 22266017

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08.22.2008 at 03:22PM PDT, ID: 22294559

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08.24.2008 at 06:54AM PDT, ID: 22300680

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08.26.2008 at 02:47AM PDT, ID: 22312883

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SSH / Telnet Software, Miscellaneous Programming, Miscellaneous Security
Tags: j2ee, ie 6
Sign Up Now!
Solution Provided By: leydent
Participating Experts: 0
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628