Reputation: 53
I want to:
df -kh ps -ef|grep www
How can I do it in JSCH?
JSch jsch=new JSch();
Session session=jsch.getSession(remoteHostUserName, RemoteHostName, remoteHostPortNo);
session.setPassword(remoteHostpassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Please wait...");
session.connect();
System.out.println("Connected "+remoteHostUserName+"@"+RemoteHostName);
ChannelExec channel=(ChannelExec) session.openChannel("shell");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("df -kh");
channel.setCommand("pwd");
channel.connect();
Upvotes: 2
Views: 3779
Reputation: 1378
You can run multiple commands by using below approach
put all the commands in a string separated by ;
"command1;command2...."
Upvotes: 0
Reputation: 990
In order to create an interactive session, you can refer the Example class provided jsch developers.
http://www.jcraft.com/jsch/examples/UserAuthKI.java
Create the Channel object as an instance of Shell ie
Channel channel=session.openChannel("shell");
And then set the Input and Output Streams for that Channel object.
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
And then connect the channel.
This way, after each comand execution, the channel won't be closed and the state of the previous command can be persisted.
Using the above code, you can create an interactive session in your console
Upvotes: 0
Reputation: 1121
Try ChannelShell channel = (ChannelShell) session.openChannel("shell");
setup inputStream
and outputStream
and subsequently perform the following loop:
This way you can even construct your second commands based on the outcome of the first one.
Upvotes: 1