This is a multi-part message in MIME format.
--------------060903030909030803070705
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Ravi,
I stole this from StackOverflow, haven't tested it, but it should be
fairly straightforward:
| private String readStream(InputStream iStream) throws IOException {
//build a Stream Reader, it can read char by char
InputStreamReader iStreamReader= new InputStreamReader(iStream);
//build a buffered Reader, so that i can read whole line at once
BufferedReader bReader= new BufferedReader(iStreamReader);
String line= null;
StringBuilder builder= new StringBuilder();
while((line= bReader.readLine()) != null) { //Read till end
builder.append(line);
}
bReader.close(); //close all opened stuff
iStreamReader.close();
iStream.close();
return builder.toString();
}|
// Call the above routine with the error stream
String errorStream = readStream(channel.getErrStream());
// Log it with your preferred logger
logger.error(errorStream);
Many other threads on stack overflow related to this...
http://stackoverflow.com/questions/6902386/how-to-read-jsch-command-output
Stan
Post by Ravi JoshiThanks Viet,
I got your idea and looked over the example code. However I am looking
for any short solution for this. Basically the example code
(http://www.jcraft.com/jsch/examples/Exec.java.html) line no 67 has
following line-
((ChannelExec) channel).setErrStream(System.err);
Now I have a modified class of Exec.java which uses log4j logger like
this-
import org.apache.log4j.Logger;
public class Exec{
private static final Logger logger = Logger.getLogger(Exec.class);
public void execute(){
//All of the JSch errors are now written to System.error stream
((ChannelExec) channel).setErrStream(System.err);
//Instead of above line, how can I write these errors to logger
logger.error(/*set error stream to this*/);
}
}
How can I achieve this implementation?
-
Regards
Ravi
On Monday, 30 December 2013 7:56 AM, Viet H. Phan
Hi Ravi,
1) Create your own Logger class that implements com.jcraft.jsch.Logger
and uses log4j
2) Apply your Logger using
com.jcraft.jsch.JSch.setLogger(com.jcraft.jsch.Logger) method
Check Logger class of JSch example for reference.
Hope this helps.
Regards,
Viet
------------------------------------------------------------------------
*Sent:* Sunday, 29 December 2013 4:47 PM
*Subject:* [JSch-users] How to set ChannelExec error stream from
System.err to log4j
Hi,
I want to execute a command to remote linux machine. I am referring
Exec.java <http://www.jcraft.com/jsch/examples/Exec.java>
(http://www.jcraft.com/jsch/examples/Exec.java.html) example code.
I just wanted to know How to set ChannelExec error stream from
System.err to log4j, so that all of these errors, I can get captured
by log4j under error catagory.
-
Thanks
Ravi
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into
your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of
AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
--------------060903030909030803070705
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Ravi,<br>
<br>
I stole this from StackOverflow, haven't tested it, but it should
be fairly straightforward: <br>
<br>
<pre style="" class="lang-java prettyprint prettyprinted"><code><span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> readStream</span><span class="pun">(</span><span class="typ">InputStream</span><span class="pln"> iStream</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">throws</span><span class="pln"> </span><span class="typ">IOException</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="com">//build a Stream Reader, it can read char by char</span><span class="pln">
</span><span class="typ">InputStreamReader</span><span class="pln"> iStreamReader </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">InputStreamReader</span><span class="pun">(</span><span class="pln">iStream</span><span class="pun">);</span><span class="pln">
</span><span class="com">//build a buffered Reader, so that i can read whole line at once</span><span class="pln">
</span><span class="typ">BufferedReader</span><span class="pln"> bReader </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">BufferedReader</span><span class="pun">(</span><span class="pln">iStreamReader</span><span class="pun">);</span><span class="pln">
</span><span class="typ">String</span><span class="pln"> line </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">
</span><span class="typ">StringBuilder</span><span class="pln"> builder </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringBuilder</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">while</span><span class="pun">((</span><span class="pln">line </span><span class="pun">=</span><span class="pln"> bReader</span><span class="pun">.</span><span class="pln">readLine</span><span class="pun">())</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">//Read till end</span><span class="pln">
builder</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">line</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
bReader</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><span class="pln"> </span><span class="com">//close all opened stuff</span><span class="pln">
iStreamReader</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><span class="pln">
iStream</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> builder</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span></code></pre>
<br>
// Call the above routine with the error stream<br>
String errorStream = readStream(channel.getErrStream()); <br>
// Log it with your preferred logger<br>
logger.error(errorStream);<br>
<br>
Many other threads on stack overflow related to this...<br>
<br>
<a class="moz-txt-link-freetext" href="http://stackoverflow.com/questions/6902386/how-to-read-jsch-command-output">http://stackoverflow.com/questions/6902386/how-to-read-jsch-command-output</a><br>
<br>
Stan<br>
<br>
<br>
On 12/30/2013 8:59 AM, Ravi Joshi wrote:<br>
</div>
<blockquote
cite="mid:***@web163002.mail.bf1.yahoo.com"
type="cite">
<div style="color:#000; background-color:#fff;
font-family:verdana, helvetica, sans-serif;font-size:10pt">
<div><span>Thanks Viet,</span></div>
<div style="color: rgb(0, 0, 0); font-size: 13px; font-family:
verdana, helvetica, sans-serif; background-color: transparent;
font-style: normal;"><span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-size: 13px; font-family:
verdana, helvetica, sans-serif; background-color: transparent;
font-style: normal;"><span>I got your idea and looked over the
example code. However I am looking for any short solution
for this. Basically the example code (</span><a
moz-do-not-send="true"
href="http://www.jcraft.com/jsch/examples/Exec.java.html"
style="font-size: 10pt;">http://www.jcraft.com/jsch/examples/Exec.java.html</a>)
line no 67 has following line-</div>
<div style="color: rgb(0, 0, 0); font-size: 13px; font-family:
verdana, helvetica, sans-serif; background-color: transparent;
font-style: normal;"><br>
</div>
<div style="background-color: transparent;"><span
class="Apple-tab-span" style="white-space: pre;"> </span>((ChannelExec)
channel).setErrStream(System.err);<br>
</div>
<div style="background-color: transparent; color: rgb(0, 0, 0);
font-size: 13px; font-family: verdana, helvetica, sans-serif;
font-style: normal;"><br>
</div>
<div style="background-color: transparent; color: rgb(0, 0, 0);
font-size: 13px; font-family: verdana, helvetica, sans-serif;
font-style: normal;">Now I have a modified class of Exec.java
which uses log4j logger like this-</div>
<div style="background-color: transparent; color: rgb(0, 0, 0);
font-size: 13px; font-family: verdana, helvetica, sans-serif;
font-style: normal;"><br>
</div>
<div style="background-color: transparent;">import
org.apache.log4j.Logger;</div>
<div style="background-color: transparent;"><br>
</div>
<div style="background-color: transparent;">public class Exec{</div>
<div style="background-color: transparent;"> private static
final Logger logger = Logger.getLogger(Exec.class);</div>
<div style="background-color: transparent;"> public void
execute(){</div>
<div style="background-color: transparent;"> //All of the
JSch errors are now written to System.error stream</div>
<div style="background-color: transparent;">
((ChannelExec) channel).setErrStream(System.err);</div>
<div style="background-color: transparent;"><br>
</div>
<div style="background-color: transparent;"> //Instead of
above line, how can I write these errors to logger</div>
<div style="background-color: transparent;">
logger.error(/*set error stream to this*/);</div>
<div style="background-color: transparent;"> }</div>
<div style="background-color: transparent;">}</div>
<div><br>
</div>
<div>How can I achieve this implementation?</div>
<div> </div>
<div>-</div>
<div>Regards</div>
<div>Ravi</div>
<div class="yahoo_quoted" style="display: block;"> <br>
<br>
<div style="font-family: verdana, helvetica, sans-serif;
font-size: 10pt;">
<div style="font-family: 'times new roman', 'new york',
times, serif; font-size: 12pt;">
<div dir="ltr"> <font face="Arial" size="2"> On Monday,
30 December 2013 7:56 AM, Viet H. Phan <a class="moz-txt-link-rfc2396E" href="mailto:***@yahoo.com"><***@yahoo.com></a> wrote:<br>
</font> </div>
<div class="y_msg_container">
<div id="yiv3069917843">
<div>
<div style="color: rgb(0, 0, 0); background-color:
rgb(255, 255, 255); font-family: 'times new
roman', 'new york', times, serif; font-size:
12pt;">
<div><span style="font-size:13px;">Hi Ravi,</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="font-size:small;"><br clear="none">
</span></div>
<div style="background-color:transparent;"><span
style="font-size:small;">1) Create your own
Logger class that
implements com.jcraft.jsch.Logger and uses
log4j</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="font-size:small;"><span>2) Apply your
Logger using com.jcraft.jsch.JSch.setLogger(</span><span>com.jcraft.jsch.Logger</span><span
style="background-color:transparent;">)
method</span></span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;">Check
Logger class of JSch example for reference.</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;"><br
clear="none">
</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;">Hope
this helps.</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;"><br
clear="none">
</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;">Regards,</span></div>
<div style="font-family: 'times new roman', 'new
york', times, serif; background-color:
transparent; font-style: normal;"><span
style="background-color:transparent;font-size:small;">Viet</span></div>
<div style="color: rgb(0, 0, 0); font-size: 16px;
font-family: 'times new roman', 'new york',
times, serif; background-color: transparent;
font-style: normal;"><span><br clear="none">
</span></div>
<div style="color: rgb(0, 0, 0); font-size: 16px;
font-family: 'times new roman', 'new york',
times, serif; background-color: transparent;
font-style: normal;"><span><br clear="none">
</span></div>
<div><br clear="none">
</div>
<div style="font-family: 'times new roman', 'new
york', times, serif; font-size: 12pt;">
<div style="font-family: 'times new roman', 'new
york', times, serif; font-size: 12pt;">
<div class="yiv3069917843yqt3584844522"
id="yiv3069917843yqt49455">
<div dir="ltr">
<hr size="1"> <font face="Arial" size="2">
<b><span style="font-weight:bold;">From:</span></b>
Ravi Joshi <a class="moz-txt-link-rfc2396E" href="mailto:***@yahoo.com"><***@yahoo.com></a><br
clear="none"> <b><span style="font-weight:bold;">To:</span></b> <a class="moz-txt-link-rfc2396E" href="mailto:jsch-***@lists.sourceforge.net">"jsch-***@lists.sourceforge.net"</a> <a class="moz-txt-link-rfc2396E" href="mailto:jsch-***@lists.sourceforge.net"><jsch-***@lists.sourceforge.net></a>
<br clear="none">
<b><span style="font-weight:bold;">Sent:</span></b>
Sunday, 29 December 2013 4:47 PM<br
clear="none">
<b><span style="font-weight:bold;">Subject:</span></b>
[JSch-users] How to set ChannelExec
error stream from System.err to log4j<br
clear="none">
</font> </div>
<div class="yiv3069917843y_msg_container"><br
clear="none">
<div id="yiv3069917843">
<div>
<div style="color: rgb(0, 0, 0);
background-color: rgb(255, 255,
255); font-family: verdana,
helvetica, sans-serif; font-size:
10pt;">
<div><span>Hi,</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span><br
clear="none">
</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span>I want
to execute a command to remote
linux machine. I am referring </span><a
moz-do-not-send="true"
rel="nofollow" shape="rect"
target="_blank"
href="http://www.jcraft.com/jsch/examples/Exec.java"
style="background-color:
rgb(255, 255, 255); color:
rgb(88, 167, 70);
text-decoration: none;
font-weight: bolder;
font-family: arial, helvetica,
sans-serif; font-size: 14px;
line-height: 18px;">Exec.java</a> <span
style="background-color:transparent;">(</span><a moz-do-not-send="true"
rel="nofollow" shape="rect"
target="_blank"
href="http://www.jcraft.com/jsch/examples/Exec.java.html"
style="font-size:10pt;">http://www.jcraft.com/jsch/examples/Exec.java.html</a><span
style="background-color:transparent;">) example code.</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;"><br
clear="none">
</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;">I
just wanted to know How to set
ChannelExec error stream from
System.err to log4j, so that all
of these errors, I can get
captured by log4j under error
catagory.</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;"><br
clear="none">
</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;"><br
clear="none">
</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;">-</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;">Thanks</span></div>
<div style="color: rgb(0, 0, 0);
font-size: 13px; font-family:
verdana, helvetica, sans-serif;
background-color: transparent;
font-style: normal;"><span
style="background-color:transparent;">Ravi</span></div>
<div><br clear="none">
</div>
</div>
</div>
</div>
<br clear="none">
------------------------------------------------------------------------------<br
clear="none">
Rapidly troubleshoot problems before they
affect your business. Most IT <br
clear="none">
organizations don't have a clear picture
of how application performance <br
clear="none">
affects their revenue. With AppDynamics,
you get 100% visibility into your <br
clear="none">
Java,.NET, & PHP application. Start
your 15-day FREE TRIAL of AppDynamics Pro!<br
clear="none">
<a moz-do-not-send="true" rel="nofollow"
shape="rect" target="_blank"
href="http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk">http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk</a><br
clear="none">
_______________________________________________<br clear="none">
JSch-users mailing list<br clear="none">
<a moz-do-not-send="true" rel="nofollow"
shape="rect"
ymailto="mailto:JSch-***@lists.sourceforge.net"
target="_blank"
href="mailto:JSch-***@lists.sourceforge.net">JSch-***@lists.sourceforge.net</a><br
clear="none">
<a moz-do-not-send="true" rel="nofollow"
shape="rect" target="_blank"
href="https://lists.sourceforge.net/lists/listinfo/jsch-users">https://lists.sourceforge.net/lists/listinfo/jsch-users</a><br
clear="none">
<br clear="none">
<br clear="none">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
<a class="moz-txt-link-freetext" href="http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk">http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk</a></pre>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
JSch-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:JSch-***@lists.sourceforge.net">JSch-***@lists.sourceforge.net</a>
<a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/jsch-users">https://lists.sourceforge.net/lists/listinfo/jsch-users</a>
</pre>
</blockquote>
<br>
</body>
</html>
--------------060903030909030803070705--