我必须要两个脚本,我要在服务器上执行。问题是,弗里斯特脚本设置一些环境变量,有而第二个可以只通过用JSch执行source命令
script2.ksh
现在,如果我给Jsch下面的字符串来执行执行将通过
. script1.ksh
执行
cd work_dir && . script1.ksh && ./script2.ksh
它告诉我script1.ksh没有找到 得到控制而如果我尝试
cd work_dir && ./script1.ksh && ./script2.ksh
当然,script2中的变量没有定义。
有没有办法在JSch中执行这样的命令?
===========解决方案如下:
在这篇文章的最后答案的伎俩:
Multiple commands through Jsch Shell
JSch jsch = new JSch();
Session session = jsch.getSession(scpInfo.getUsername(), scpInfo.getIP(), scpInfo.getPort());
session.setPassword(scpInfo.getPassword());
setUpHostKey(session);
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
for(String command: commands) {
shellStream.println(command);
shellStream.flush();
}
Thread.sleep(5000);
channel.disconnect();
session.disconnect();
切断睡觉前很重要的