使用 JSch Java 通过 ssh 将命令列表发送到远程服务器列表

Sending a list of commands to list of remote server through ssh by Java with JSch

我是 Java 的新手(我仍在学习过程中),所以如果您的回答提供了我可以遵循的详细步骤,我将不胜感激。

这就是我要找的 1) 我在 txt 文件中有一个命令列表。 2) 我在另一个文件中有一个服务器列表。 3) 我希望让 java 程序提示我的 SSH userID/password,加载命令文件和服务器文件。并在有问题的服务器上执行命令。 4) 将输出存储在本地机器上的 txt 文件中,稍后我将对其进行解析。

我能够获得以下 Java 程序登录和 运行 一些推荐。但是正如您所看到的,UID/password 和一个服务器只存储在程序中,我只读取 System.out 而没有将输出写入文件。

求助!!!

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

/** Demonstrates a connection to a remote host via SSH. **/
public class JSSH
{
    private static final String user = "UID"; // TODO: Username of ssh account on remote machine
    private static final String host = "localhost"; // TODO: Hostname of the remote machine (eg: inst.eecs.berkeley.edu)
    private static final String password = "pass"; // TODO: Password associated with your ssh account
    private static final String command = "ls -l\n cd Downloads \n ls -l\n"; // Remote command you want to invoke

public static void main(String args[]) throws JSchException, InterruptedException
{
    JSch jsch = new JSch();

    // TODO: You will probably want to use your client ssl certificate instead of a password
    // jsch.addIdentity(new File(new File(new File(System.getProperty("user.home")), ".ssh"), "id_rsa").getAbsolutePath());

    Session session = jsch.getSession(user, host, 22);

    // TODO: You will probably want to use your client ssl certificate instead of a password
    session.setPassword(password);

    // Not recommended - skips host check
    session.setConfig("StrictHostKeyChecking", "no");

    // session.connect(); - ten second timeout
    session.connect(10*1000);

    Channel channel = session.openChannel("shell");

    // TODO: You will probably want to use your own input stream, instead of just reading a static string.
    InputStream is = new ByteArrayInputStream(command.getBytes());
    channel.setInputStream(is);

    // Set the destination for the data sent back (from the server)
    // TODO: You will probably want to send the response somewhere other than System.out
    channel.setOutputStream(System.out);

    // channel.connect(); - fifteen second timeout
    channel.connect(15 * 1000);

    // Wait three seconds for this demo to complete (ie: output to be streamed to us).
    Thread.sleep(3*1000);

    // Disconnect (close connection, clean up system resources)
    channel.disconnect();
    session.disconnect();
}

}

下面是一种方法,不过我还没有测试过,但可以帮助您理解。

这是我的 JSSH class:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class JSSH {
    private static final String user = "UID"; 
    private static final String password = "pass";

    public static void main(String args[]) throws JSchException,
        InterruptedException, IOException {
    JSSH jssh = new JSSH();
    JSch jsch = new JSch();
    for(String host : jssh.listOfhost()) {
        Session session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(getProperties());
        session.connect(10 * 1000);
        Channel channel = session.openChannel("shell");

        for(String command : jssh.listOfCommand()) {
            channel.setInputStream(new ByteArrayInputStream(command.getBytes()));
            channel.setOutputStream(new FileOutputStream(new File(OUTPUT_FILE)));
            channel.connect(15 * 1000);
            TimeUnit.SECONDS.sleep(3);
        }

        channel.disconnect();
        session.disconnect();
    }
}

private static Properties getProperties() {
    Properties properties = new Properties();
    properties.put("StrictHostKeyChecking", "no");
    return properties;
}


    private List<String> listOfCommand() throws IOException {
        return new LineBuilder("command_file.txt").build();
    }

    private List<String> listOfhost() throws IOException {
        return new LineBuilder("host_file.txt").build();
    }
}  
}

这里是 LineBuilder class:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LineBuilder {

    private String fileName;

    public LineBuilder(String fileName) {
        this.fileName = fileName;
    }

    public List<String> build() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
        List<String> lines = new ArrayList<String>();
        String line = null;
        try {
            while((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch(IOException e) {
            throw e;
        } finally {
            reader.close();
        }
        return lines;
    }
}

注意 - 我删除了注释只是为了让代码更清晰。