【问题标题】:Sending a list of commands to list of remote server through ssh by Java with JSch使用 JSch 通过 Java 通过 ssh 将命令列表发送到远程服务器列表
【发布时间】:2015-06-09 12:41:21
【问题描述】:

我是 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();
}

}

【问题讨论】:

    标签: java ssh jsch


    【解决方案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;
        }
    }
    

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

    【讨论】:

    • 谢谢,我会试一试,但我怎样才能提示输入用户 ID/密码而不是留在脚本中?
    • 有人可以帮忙吗?
    • 我运行了上面的程序,它运行良好。我测试了com.jcraft.jsch_0.1.31.jar
    • 我认为命令文件中有多个命令是导致问题的原因。当我只向 txt 文件添加一个命令 "ls" 时,我没有收到任何错误,但我的输出文件日志有...... ..................“上次登录时间:2015 年 6 月 9 日星期二 14:30:11 来自本地主机 lsSamsungATIV700T:~ myaccount$ ls .................................................. .. 如您所见,“ls”命令未正确执行。“ls”出现在主机名前后,没有任何输出。有什么建议吗?
    • 我的想法,我将您的答案标记为最接近我的解决方案。但它确实需要一些 VGR 帮助我解决的调整。再次感谢您对此的帮助。 VGR 如果你看到这个,你摇滚!!!再次感谢您的详细帮助。完整解决方案请见stackoverflow.com/questions/30805400/…
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 2021-01-17
    • 2011-05-10
    • 1970-01-01
    • 2017-02-15
    • 2013-11-22
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多