繁体   English   中英

Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接

[英]ssh Exception in Java: java.net.ConnectException: Connection refused: connect

我想使用 Java 用 ssh 运行脚本,但总是有一个异常: java.net.ConnectException: Connection refused: connect

这是我的代码

public class RemoteScriptExec {

    public static void main(String[] args) {

        try {
            JSch jsch = new JSch();

            Session session;  

            // String host=JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + //"@localhost");
            // String user=host.substring(0, host.indexOf('@'));
            //host=host.substring(host.indexOf('@')+1);

            session = jsch.getSession("","197.31.73.196", 22); 
            session.setConfig("StrictHostKeyChecking", "no");

            session.setPassword("");

            session.connect();

            if (session.isConnected()){
                System.out.println("ok");
            } else {
                System.out.println("failed");
            }

            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

            channelExec.setCommand("sh myscript.sh Rajesh");
            channelExec.connect();
            // by the executing command, from the remote side.
            InputStream in = channelExec.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int exitStatus = channelExec.getExitStatus();
            if (exitStatus > 0) {
                System.out.println("Remote script exec error! " + exitStatus);
            }
            //Disconnect the Session
            session.disconnect();
        } catch (JSchException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    } 
}

这是互联网连接的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM