jsch


How to add timeout when use JSch to execute a shell command which need user input later


There is one scenario in my case:
1.Execute a command : e.g. -bash-4.1$ testBackup
2.This command need to export some database name/password
3.But if I forget to export database name/password this command will be hang there.
4.I'm not quite sure how to use the JSch timeout logic when a shell command is hang
5.I use the JSch(exec) to execute shell command, it think it quite same with JSch(shell)
Here is the JSch code:
public String runCommand(String command) throws JSchException, IOException {
channel = sesConnection.openChannel("exec");
((ChannelExec) channel).setCommand(command);
BufferedReader br = new BufferedReader(
new InputStreamReader(channel.getInputStream()));
InputStream error = ((ChannelExec) channel).getErrStream();
channel.connect(60000);
outputstream = new StringBuffer();
String msg = null;
while ((msg = br.readLine()) != null) {
outputstream.append(msg).append("\n");
}
LOG.info("The output stream's length is:" + outputstream.length());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int code = channel.getExitStatus();
LOG.info("Channel exit status:" + channel.getExitStatus());
LOG.debug(
"Output of command successful execution: " + outputstream.toString());
String errormsg = null;
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(error));
errorstream = new StringBuffer();
while ((errormsg = errorReader.readLine()) != null) {
LOG.error("Terminal error output is: " + errormsg);
errorstream.append(errormsg).append("\n");
}
channel.disconnect();
if (errorstream.length() > 0) {
return errorstream + outputstream.toString() + "ExitCode" + code;
} else {
return outputstream.toString() + "ExitCode" + code;
}
}

Related Links

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
Java jsch and resuming file upload after interruption?
How to pass a variable through channel in Jsch?
How to handle passwords prompts in expectj

Categories

HOME
isabelle
google-play
hp-exstream
grep
getelementsbytagname
sqlite-net-extensions
packages
webpack-2
pivotal-cloud-foundry
swagger-ui
android-youtube-api
cross-validation
primary-key
communication
ssl-client-authentication
vault
wheelnav.js
imacros
u-sql
google-cloud-spanner
apache-cayenne
size
visual-studio-2005
invantive-sql
interop
scichart
autocad-plugin
tokenize
functional-testing
arabic
hammerspoon
smb
poltergeist
fluentvalidation
buck
preg-match-all
scorm2004
gtrendsr
http-get
commit
rotational-matrices
wpf-controls
disassembling
espeak
libvpx
android-browser
vapor
hilbert-curve
tasker
flashair
promela
drupal-6
boost-preprocessor
jspdf-autotable
redundancy
blacklist
azure-sdk
vmware-tools
intrusion-detection
pcf
disque
nessus
hsv
iad
pervasive-sql
rtbkit
ios8-today-widget
msys
sysinternals
angular-local-storage
jsapi
lustre
nsmutabledictionary
applescript-objc
xcode-6.2
ceil
typeof
jboss-weld
mbr
farseer
ruby-datamapper
ril
selected
first-responder
pysimplesoap
amazon-appstore
blackberry-playbook
paster
mozilla-prism
database-management
multi-tier
brewmp
text-coloring
nerddinner
defensive-programming

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