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

时间:2015-06-06 14:24:48

标签: java ssh jsch

我是Java新手(我还在学习过程中),所以如果您的答案提供了我可以遵循的详细步骤,请非常感谢。

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

我能够获得以下Java程序登录并运行一些表扬。但正如您所看到的,UID /密码和一个服务器只存储在程序中,我只是在不将输出写入文件的情况下读取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();
}

}

1 个答案:

答案 0 :(得分:1)

下面是一种方法,但是我没有测试它,但会帮助你理解。

这是我的JSSH课程:

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类:

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;
    }
}

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