jsch


How to read JSch command output?


I have the following code:
JSch jsch = new JSch();
jsch.setKnownHosts(dotSshDir + "/known_hosts");
jsch.addIdentity(dotSshDir + "/id_rsa");
Session session = jsch.getSession(userName, hostname, 22);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
Reader reader = new InputStreamReader(channel.getInputStream());
char[] buf = new char[1024];
int numRead;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
result.append(readData);
buf = new char[1024];
}
It's hanging trying to read from the reader. How do I fix this? How do I go about hunting down what's happening?
The hanging was actually due to some unbalanced quotes in the command.
For posterity, the final code (after dealing with some other issues) is:
public String call(String hostname, String[] argv) throws SubprocessException {
StringBuffer result = new StringBuffer();
Session session = null;
ChannelExec channel = null;
try {
// TODO: Emit friendly error if ~/.ssh doesn't exist.
String dotSshDir = System.getProperty("user.home") + "/.ssh";
String userName = System.getProperty("user.name");
// TODO: Emit friendly error if ~/.ssh/known_hosts doesn't exist.
jSch.setKnownHosts(dotSshDir + "/known_hosts");
// TODO: Emit friendly error if ~/.ssh/id_rsa doesn't exist.
jSch.addIdentity(dotSshDir + "/id_rsa");
session = jSch.getSession(userName, hostname, 22);
session.connect();
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(Joiner.on(" ").join(argv));
channel.setInputStream(null);
InputStream stdout = channel.getInputStream();
InputStream stderr = channel.getErrStream();
channel.connect();
waitForChannelClosed(channel);
if (channel.getExitStatus() != 0) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stderr));
readAll(bufferedReader, result);
throw new Exception(result.toString());
} else {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stdout));
readAll(bufferedReader, result);
}
} catch (Exception e) {
throw new SubprocessException(e);
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
return result.toString();
}

Related Links

Jsch - ssh connection through ssh tunnel
Changing password using Jsch and ChannelExec
how to delete local file using jsch after file is sent to remote SFTP server directory
com.jcraft.jsch.JSchException: Auth fail error
Creating a new file in SSH enabled system using JSCH
How to interact with the script after its execution using Jsch
java jsch -executing multiple unix commands
How to get one stream from error stream and input stream when calling a script using JSCH
SFTP using jschexception verify false sometimes fails
how to increase the jsch buffer size?
The cipher 'aes256-cbc' is required, but it is not available
How to pause/resume procedure while/after upload file by ChannelSftp
jsch + running multiple command in shell after SU and then close channal
NGit/Nsch Generating SSH Keypair (c# Jgit)
Extract userInfo from JSCH stream
Running a Shell Script using java ( process Builder ) with a specific unix user

Categories

HOME
ibm-bluemix
wso2
openmp
view
raspberry-pi
react-router
kalman-filter
filtering
ebean
blueprintjs
datastax-java-driver
autotools
basic
imacros
handsontable
u-sql
mapserver
messages
decimal
lombok
workload-scheduler
libtiff
graphlab
nhibernate-envers
openedx
crystal-reports-2008
swiftlint
conemu
reverse-proxy
visjs
cloudhub
tapestry
excel-2007
one-to-many
javacv
kannel
quote
socialengine
gtrendsr
winrt-xaml-toolkit
caret
lto
space-complexity
android-browser
ssjs
grid.mvc
http-live-streaming
glew
bootstrap-dialog
websphere-mq-fte
appcompat
chain-builder
rails-routing
boost-multi-index
veracode
directory-structure
r-forge
account-kit
chord-diagram
eventkit
migradoc
pintos
wdf
vmware-tools
fody
fputcsv
ready-api
pickadate
pagerank
asp.net5
independentsoft
cloudbees
browser-link
marching-cubes
atk4
spidermonkey
citrus-pay
pundit
riak-cs
method-parameters
operation
oxwall
c3
jscript.net
fpml
nsbutton
applescript-objc
issuu
castle
mesa
marmalade
ivyde
ms-project-server-2010
ftps
batterylevel
html4
django-nonrel
gdata-api
ohm
transactionscope
adk
boost-filesystem
stage
subgurim-maps
django-tagging
javax.script
self-extracting
asp.net-mvc-areas
infrastructure
user-friendly
libs
ti-dsp
boost-smart-ptr
anti-piracy
spec#
w3wp
msdev
ugc
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App