Вот мой код, который входит на сервер sftp и выполняет команду "dzdo su - jhon", изменяет путь к папке и выполняет команду ls внутри этого конкретного пользователя.Этот код выполняет команды внутри пользователя john, как этого добиться, используя контрольный пример цитрусового xml.
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");
channelExec.connect();
InputStream in = channelExec.getInputStream();
channelExec.setCommand("dzdo su - john");// changing the user
OutputStream out = channelExec.getOutputStream();
out.write(("cd /xx.yy/zz \n").getBytes());// executing commands inside user
out.write(("ls \n").getBytes());
out.flush();
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();
}
}