使用 JSch 在 SSH 服务器上搜索文件以获取模式(使用 less 命令)

Search file on SSH server for a pattern (with less command) using JSch

我目前正在尝试访问 Linux 服务器和 运行 一些命令。 一般的想法是搜索包含特定文本的日志文件,然后访问该日志,搜索必要的文本并将当前显示在屏幕上的所有数据存储在一个字符串中。

这种方法通过 PuTTY 工作正常,但现在要求不同了。

我已经在 ls 等其他命令和提供直接输出的其他命令上完成了此任务。但是,我对 lessmore.

这样的命令没有运气

我当然尝试过将日志的全部内容发送到数组列表,然后搜索我的文本(一旦找到第一行,我就会对数据的位置有一个相对的了解) .但是,这会花费大量时间,因为日志大小从 10 Mb 到 750 Mb 不等。

如果有人能想到另一种方法或提供任何想法来实现这种方法的可行方法,我将不胜感激。

供参考: 我试过的代码

public class check {

private Session session;

private String username = "user";
private String password = "pass";
private String hostname = "host";

public SSHConnectionManager() 
{ }

public SSHConnectionManager(String hostname, String username, String password) 
{
    this.hostname = hostname;
    this.username = username;
    this.password = password;
}

public void open() throws JSchException 
{
    open(this.hostname, this.username, this.password);
}

public void open(String hostname, String username, String password) throws JSchException
{

    JSch jSch = new JSch();

    session = jSch.getSession(username, hostname, 22);
    Properties config = new Properties(); 
    config.put("StrictHostKeyChecking", "no");  // not recommended
    session.setConfig(config);
    session.setPassword(password);

    System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
    session.connect();
    System.out.println("Connected!");
}

public String runCommand(String command) throws JSchException, IOException 
{

    String ret = "";

    if (!session.isConnected())
        throw new RuntimeException("Not connected to an open session.  Call open() first!");

    ChannelExec channel = null;
    channel = (ChannelExec) session.openChannel("exec");

    channel.setCommand(command);
    channel.setInputStream(null);

    PrintStream out = new PrintStream(channel.getOutputStream());
    InputStream in = channel.getInputStream(); // channel.getInputStream();

    channel.connect();

    ret = getChannelOutput(channel, in);

    channel.disconnect();

    System.out.println("Finished sending commands!");

    return ret;
}


private String getChannelOutput(Channel channel, InputStream in) throws IOException
{

    byte[] buffer = new byte[1024];
    StringBuilder strBuilder = new StringBuilder();

    String line = "";
    while (true){
        while (in.available() > 0) {
            int i = in.read(buffer, 0, 1024);
            if (i < 0) {
                break;
            }
            strBuilder.append(new String(buffer, 0, i));
            System.out.println(line);
        }

        if(line.contains("logout")){
            break;
        }

        if (channel.isClosed()){
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee){}
    }

    return strBuilder.toString();   
}

public void close()
{        
    session.disconnect();
    System.out.println("Disconnected channel and session");
}


public static void main(String[] args)
{

    SSHConnectionManager ssh = new SSHConnectionManager();
    try {
        ssh.open();
        String ret = ssh.runCommand("ls -l");
        System.out.println(ret);
        ret=ssh.runCommand("cd *move to path of log*");
        System.out.println(ret);
        ret=ssh.runCommand("less *log name*");
        System.out.println(ret);
        ret=ssh.runCommand("/*searchtext*");
        System.out.println(ret);


        ssh.close();

        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

我得到了我的 ls 命令的必要输出,但没有得到任何其他命令的输出。

首先,您的代码在其自己单独的 shell 中执行每个命令。所以 cd 命令对 less 命令没有影响。 Send, /*searchtext* 根本就不是命令。请注意,setCommand 不等同于在 SSH 控制台 window 中键入内容。完全不同的界面。

第一步是在同一个 shell 中执行所有命令。为此,请使用服务器的 shell 技术。在 Linux 服务器上,您将使用分号或双符号。例如此处所示:Multiple commands using JSch.

尽管它不会帮助您 /*searchtext*。您不应尝试使用 "human" 自动化技术。

最后,更好的解决方案是实际上用一个命令完成所有事情,比如使用 grep:

grep *searchtext* *move to path of log*/*log name*