我正在监视一组远程服务器上的错误的日志。为此,我正在监视在一段时间内被修改的日志。通过Java JSch库在远程计算机上执行本地脚本
我使用Jsch Java library
来制作SSH connection
并执行命令。正如我所搜索的,多个命令可以通过将不同命令分开的远程机器通过;
为此我有一个文本文件,其中包含我在其上建立连接的远程主机上的列表。我意识到,我可以通过下面的命令
find . -mmin -60 -type f -exec ls {} +
不,我可以遍历文件和grep for the Error
修改的所有文件的列表。为此,我写了下面的脚本
logTestScript.sh
#!/bin/bash
cd /log
searchString='<Error ErrorCode="java.sql.SQLException"'
files=`find . -mmin -60 -type f -exec ls {} +`
pwd
hostname
echo ${files}
for file in ${files[@]}
do
if grep -Fq "$searchString" $file
then
echo "String found in $file"
fi
done
现在这个脚本会在当前包从我运行一个Java程序,我想运行在每个这个脚本的远程主机。
下面是我当前的代码利用的
private void getSSHConnection() throws IOException, JSchException {
username = props.getProperty("host_username");
password = props.getProperty("host_password");
log_traceback_interval = props.getProperty("fetch_logs_interval_minutes");
errorString = props.getProperty("error_message_log");
System.out.println(username + " " + password + " " + errorString);
// Get the hostnames resource file
String hostnameResourcePath = envObj.getAgentHostnamesConfig(TargetEnvironment, ManagementSystem);
InputStream hostInpStream = this.getClass().getResourceAsStream(hostnameResourcePath);
BufferedReader hostnameReader = new BufferedReader(new InputStreamReader(hostInpStream));
String host = null;
while((host = hostnameReader.readLine()) != null) {
System.out.println(host);
// get a JSch connection object
JSch jschObj = new JSch();
Session session = jschObj.getSession(username, host);
// Disable StrictHost key checking
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
session.connect();
//Execute channel instance
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
//Bash script command with arguments
StringBuilder sb = new StringBuilder();
String command = sb.append("cd /log").append(";")
.append("echo `find . -mmin -60 -type f -exec ls {} +`").append(";")
.toString();
System.out.println(command);
//Run the script on host
channelExec.setCommand(command); //I want to run the script here instead of each command as it uses for loop
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
InputStream monitorLogs = channelExec.getInputStream();
channelExec.connect();
//Read the logs
BufferedReader logReader = new BufferedReader(new InputStreamReader(monitorLogs));
String logLine;
while((logLine = logReader.readLine()) != null) {
System.out.println(logLine);
}
//Close the connections
channelExec.disconnect();
session.disconnect();
}
}
一个解决方案,我可以到远程主机想如果到SCP的脚本,然后执行它。
但是有没有一些简单的解决方案,即使没有编写脚本,只执行命令或在远程服务器上执行脚本,我也可以获得相同的结果。
感谢您的帮助
https://www.ibm.com/developerworks/community/blogs/738b7897-cd38-4f24-9f05-48dd69116837/entry/programmatically_connecting_to_remote_systems2?lang=en – RajatJ