Here is my code which logins to sftp server and executes command "dzdo su - ibmusr" command and changes the path of and folder and execute ls command. Here is the code
public class Sudo{
public static void main(String[] arg) throws Exception{
int port=22;
String name ="john";
String ip ="xxxx";
String password ="root";
JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("dzdo su - john");
OutputStream out = channelExec.getOutputStream();
out.write(("cd /xx.yy/zz \n").getBytes());
out.write(("ls \n").getBytes());
out.flush();
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
session.disconnect();
}
}
I am getting below exception
Exception in thread "main" java.io.IOException: failed to initialize the channel.
at com.jcraft.jsch.Channel$1.init(Channel.java:242)
at com.jcraft.jsch.Channel$1.write(Channel.java:253)
at java.io.OutputStream.write(OutputStream.java:75)
at com.consol.citrus.samples.todolist.Sudo.main(Sudo.java:43)