这是我的代码,该代码登录sftp服务器并执行命令“ dzdo su-jhon”命令,并更改文件夹的路径并在该特定用户内执行ls命令。这段代码在john用户内部执行了如何使用citrus测试用例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();
}
}