使用xshell的nohup&命令在服务器上运行Java程序 5
在实验室的服务器上用nohup&运行java程序按理不会受网络影响吧,但今早过来看显示的就是connectclosedbyforeignhost,为什么有这个问题呢?求指...
在实验室的服务器上用nohup &运行java程序按理不会受网络影响吧,但今早过来看显示的就是connect closed by foreign host,为什么有这个问题呢?
求指教,谢谢各位 展开
求指教,谢谢各位 展开
展开全部
您好,在网上有挺多版本,C JAVA的都有,我找了一个还可以,有源码,大概试了试效果还不错。需要下载一个ganymed-ssh2-build210 http://www.ganymed.ethz.ch/ssh2/ 代码部分大概是这样的。
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class RmtShellExecutor {
private Connection conn;
private String ip;
private String usr;
private String psword;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
public RmtShellExecutor(ShellParam param) {
this.ip = param.getIp();
this.usr = param.getUsername();
this.psword = param.getPassword();
}
class ShellParam{
String ip;
String usr;
String psword;
String getIp(){
return ip;
};
String getUsername(){
return usr;
}
String getPassword(){
return psword;
}
}
public RmtShellExecutor(String ip, String usr, String ps) {
this.ip = ip;
this.usr = usr;
this.psword =ps;
}
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(usr, psword);
}
public int exec(String cmds) throws Exception {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1;
try {
if (login()) {
// Open a new {@link Session} on this connection
Session session = conn.openSession();
// Execute a command on the remote machine.
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
System.out.println("" + outStr);
ret = session.getExitStatus();
} else {
System.out.println("登录远程机器失败"+ ip);
}
} finally {
if (conn != null) {
conn.close();
}
}
return ret;
}
private String processStream(InputStream in, String charset) throws Exception {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
RmtShellExecutor exe = new RmtShellExecutor("", "", "");
System.out.println(exe.exec("ls"));
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class RmtShellExecutor {
private Connection conn;
private String ip;
private String usr;
private String psword;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
public RmtShellExecutor(ShellParam param) {
this.ip = param.getIp();
this.usr = param.getUsername();
this.psword = param.getPassword();
}
class ShellParam{
String ip;
String usr;
String psword;
String getIp(){
return ip;
};
String getUsername(){
return usr;
}
String getPassword(){
return psword;
}
}
public RmtShellExecutor(String ip, String usr, String ps) {
this.ip = ip;
this.usr = usr;
this.psword =ps;
}
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(usr, psword);
}
public int exec(String cmds) throws Exception {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1;
try {
if (login()) {
// Open a new {@link Session} on this connection
Session session = conn.openSession();
// Execute a command on the remote machine.
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
System.out.println("" + outStr);
ret = session.getExitStatus();
} else {
System.out.println("登录远程机器失败"+ ip);
}
} finally {
if (conn != null) {
conn.close();
}
}
return ret;
}
private String processStream(InputStream in, String charset) throws Exception {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
RmtShellExecutor exe = new RmtShellExecutor("", "", "");
System.out.println(exe.exec("ls"));
}
}
追问
不是代码,,我只想想问在什么情况下会出现我说的那种问题,nohup &指令跑程序是不是可以不受网络掉线的影响?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询