I want to run a script with ssh using Java but there is always an Exception: java.net.ConnectException: Connection refused: connect
this is my code
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());
}
}
}
这是互联网连接的问题
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.