如何在Java中设置SSH连接密码

4

我想用Java代码连接我的Unix服务器。首先它将连接到我的服务器位置,然后连接到我的服务器名称。但是在这时,它会要求确认(是/否)和密码,我不知道该怎么做? 以下是我已经完成的代码,请帮我解决这个问题

public static void main (String args[]) {
    String user = "user";
    String password = "password";
    String host = "hostName";
    int port=22;

    //String remoteFile="/home/john/test.txt";
    //String yes="yes";

    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
        Channel channel = session.openChannel("exec");

        // After this it'll ask for confirmation and password
        ((ChannelExec)channel).setCommand("ssh myServerName");
        channel.connect();

        InputStream output = channel.getInputStream();

        System.out.println("aafter stream");
        int readByte = output.read();
        StringBuilder outputBuffer = new StringBuilder();
        while (readByte != 0xffffffff) {
            //System.out.println("read byte" + readByte);
            outputBuffer.append((char)readByte);
            readByte = output.read();
        }
        System.out.println(outputBuffer.toString());
        channel.disconnect();
    } catch (Exception e){
        System.err.print("error message" + e);
    }
}

已经连接上服务器,你为什么要再次连接呢? - Naveen Ramawat
1
不对,我连接到了我的服务器位置。里面有不同的服务器。我尝试连接其中一个。我尝试直接连接服务器,但它显示“未知主机异常”。 - LowCool
2个回答

2
您需要获取JSch安全通道的OutputStream并将密码插入其中。
Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
        channel.setInputStream(null);
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream in = channel.getInputStream();
        ((ChannelExec) channel).setPty(true);
        channel.connect();
        out.write((password + "\n").getBytes());
        out.flush();

完整的源代码在这里:http://codeflex.co/java-run-sudo-command-on-remote-linux-host/

该源代码涉及到在远程Linux主机上运行sudo命令的Java实现。您可以通过访问以上链接获取更多信息。

0
    try {
    session = jsch.getSession(user, host, port);
}
catch (JSchException e) {
    throw new TransferException("Failed to open session - " + params, e);
}

session.setPassword(password);

//  Create UserInfo instance in order to support SFTP connection to any machine 
//  without a key username and password will be given via UserInfo interface.
UserInfo userInfo = new SftpUserInfo();
session.setUserInfo(userInfo);

try {
    session.connect(connectTimeout);
}
catch (JSchException e) {
    throw new TransferException("Failed to connect to session - " + params, e);
}

boolean isSessionConnected = session.isConnected();

像上面所示,通过编程方式设置密码。


请问您能详细说明一下我的SftpUserInfo类中可以包含哪些内容吗? - LowCool
因为这里显示没有名为SftpUserInfo的类。 - LowCool
最后一个问题,我能否有任何选项可以隐式地给出这个东西,而不需要用户交互? - LowCool
还有一个问题,我在哪里提供SFTP文件的位置?这里显示会话已连接。 - LowCool
我认为你没有理解我的意思。我有一个连接,需要连接到我的服务器。 - LowCool

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接