jsch


Running a Shell Script using java ( process Builder ) with a specific unix user


I have developed a web application ( deployed on a weblogic server ) , I want to connect to the solaris server and execute a shell script with a specific unix user.
At present , the script runs with a wls user. Here's the portion of my code :
String CLA="-d";
out.println("Stopping ASAP for the changes to reflect ...");
ProcessBuilder processBuilder = new ProcessBuilder("/bin/ksh","/apps/vpn/asap/scripts/stop_asap_sys_tool"+" "+CLA);
process = processBuilder.start();
InputStream is = process.getInputStream();
InputStream isErr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
InputStreamReader isrErr = new InputStreamReader(isErr);
BufferedReader br = new BufferedReader(isr);
BufferedReader brErr = new BufferedReader(isrErr);
String line;
String lineErr;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
while ((lineErr = brErr.readLine()) != null) {
System.out.println(lineErr);
}
My search result suggests to use Jsch. Can some one give me an example with respect to my implementation on using Jsch. Or any other way of doing it ?!
THanks ,
Bhavin
Jsch is a good way to go, here is something to assist you with what you trying to do:
Examples from the Main Site which covers Remote Execution
[click]
Also here is the code already done for you on StackOverflow [click]
A word of advice, when you execute scripts, and you have written then on Windows or opened them there, you will need to run a dos2unix on the file (if you executing on Linux); otherwise your remote execution is going to fail horribly.
I think this can help you
/**
* This method allows you to send and execute *nix command through SSH
* to specified removed host and returns command output, in case incorrect
* command will return command output error
*
* #param user - ssh user name for login
* #param password - ssh password for login
* #param host - ip or domain with ssh server
* #param command - command to execute
* #return string with command output or output error
* #author Roman Kukharuk
*/
public String execNixComAndGetRez(String user, String password, String host,
String command) {
int port = 22;
String rez = "+!";
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
// System.out.println("Establishing Connection...");
session.connect();
// System.out.println("Connection established.");
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command); //setting command
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
// System.out.print(new String(tmp, 0, i));
rez = new String(tmp, 0, i);
}
if (channel.isClosed()) {
// System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
rez = e.toString();
}
}
channel.disconnect();
session.disconnect();
}
catch (Exception e) {
rez = e.toString();
}
return rez;
}

Related Links

What the situation will make channel.getExitStatus() return 1
JSch read local log file
JSCH setCommand is not working
com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 11 No appropriate prime between 1024 and 1024 is available. en
I'm trying to use Jsch to establish an SSH connection in Java.I got the Exception com.jcraft.jsch.jschexception unknownhostkey
com.jcraft.jsch.JSchException: failed to send sftp request
JSch for pbrun not working
JSCH read from input stream hangs from time to time
Hundreds of Apache SSHD clients leave hundreds of OpenSSH daemon processes running
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

Categories

HOME
ms-access
sendgrid
hive
testng
cookies
json.net
plot
onedrive
relayjs
octobercms
gis
pivotal-cloud-foundry
onelogin
virtualization
dacpac
wheelnav.js
windows-azure-storage
orchardcms
quartz-scheduler
pc
ef-migrations
beyondcompare
scichart
zapier
physics-engine
orleans
functional-testing
mps
core-text
format-specifiers
claims-based-identity
hammerspoon
smb
one-to-many
traffic
wtx
dbclient
primitive
quote
file-rename
delicious-api
skia
javascriptcore
pdb
webtest
qwerty
xcode-extension
hot-module-replacement
swift3.0.2
c11
jna
unobtrusive-validation
webdriver-manager
particles.js
mapzen
unixodbc
netcdf4
reactive-cocoa-5
auto-update
jsch
avconv
taffy
darcs
jxcore
media-player
pearson
account-kit
objective-c-swift-bridge
word-vba-mac
url-pattern
color-picker
instant
vmware-tools
firebaseui
gridview-sorting
actionbardrawertoggle
rhino-servicebus
pyke
cloudbees
wordml
security-testing
embedded-code
ionic
tableau-online
microbenchmark
appfabric-cache
streambase
jsapi
kineticjs
nstableviewcell
codeigniter-routing
app42
mbr
imdbpy
terminal-services
floating-point-precision
mysqltuner
access-rights
gridfs
back-stack
android-hardware
rdoc
google-email-migration
cascalog
tridion-worldserver
ticoredatasync
amazon-appstore
boost-filesystem
window-management
data-loss
cinema-4d
gdlib
android-sdk-2.1
mozilla-prism
xsdobjectgen
nhibernate.search
ncqrs
libs
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