有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用JSch在SSH服务器上搜索模式(使用较少的命令)的java文件

我目前正在尝试访问Linux服务器并运行一些命令。 总体思路是搜索包含特定文本的日志文件,然后访问该日志,搜索必要的文本,并以字符串形式存储屏幕上当前显示的所有数据

这种方法适用于腻子,但现在的要求不同了

我已经在其他命令上完成了这个任务,比如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命令获得了必要的输出,但其他任何命令都没有


共 (1) 个答案

  1. # 1 楼答案

    首先,代码在其各自的shell中执行每个命令。因此cd命令对less命令没有影响。Send,/*searchtext*根本不是命令。注意setCommand并不是在SSH控制台窗口中键入内容的等价物。这是完全不同的界面

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

    尽管它对/*searchtext*没有帮助。你不应该尝试使用“人工”技术来实现自动化

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

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