简单地对Unix系统访问

xiaoxiao2026-05-24  15

在Unix端布署一RMI应用,使用Runtime.exec(String command)方法实现 InterfaceFile.java public interface Cmd extends Remote { /** * 执行服务器的命令 * * @param command * @return * @throws RemoteException */ public String executeCommand(String command) throws RemoteException;} Cmd_Impl.java public String executeCommand(String command) throws RemoteException { StringBuffer result = new StringBuffer(); try { Process proc = Runtime.getRuntime().exec(command); InputStream is = proc.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String str; while((str = reader.readLine()) != null){ result.append(str); result.append("\n"); } reader.close(); is.close(); return result.toString(); } catch (IOException e) { log.error("Exception has occoured when executing [ "+command+" ] command...",e); return result.toString(); } } 生成_Stub.class文件 … … 另一实现,通过使用commons-net组件的telnet包来实现 import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.net.SocketException;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.apache.commons.net.telnet.TelnetClient;/** * 访问后台文件服务器类 */ public class AccessRemoteServer { private InputStream in; private PrintStream out; private final TelnetClient tc = new TelnetClient(); private final String osTag = ">"; public static final String CONFIG_FILE_PATH = "/conf/server.properties"; // 配置文件所在路径 /** * 连接服务器 * * @return */ public boolean connect(){ try { Map configs = getConfig(); if(configs != null){ String host = configs.get("host").toString(); int port = Integer.parseInt(configs.get("port").toString()); String user = configs.get("user").toString(); String pwd = configs.get("pwd").toString(); tc.connect(host, port); in = tc.getInputStream(); out = new PrintStream(tc.getOutputStream()); readInfo(in,"login: "); out.println(user); out.flush(); readInfo(in,"Password: "); out.println(pwd); out.flush(); readInfo(in,">"); return true; } else{ return false; } } catch (SocketException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } } /** * 执行命令 * * @param command * @return */ public String executeCmd(String command){ boolean flag = connect(); if(flag){ String result = ""; try { result = executeCommand(command, in, out, osTag); tc.disconnect(); return "执行结果:\n\n"+result; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return result; } } else{ return "连接服务器失败!"; } } private String readInfo(InputStream in,String pattern){ try{ char last = pattern.charAt(pattern.length()-1); StringBuffer rs = new StringBuffer(); char ch = (char)in.read(); while(true){ rs.append(ch); if(ch == last){ if(rs.toString().endsWith(pattern)){ return rs.toString(); } } ch = (char)in.read(); } } catch(IOException e){ e.printStackTrace(); } return null; } private String executeCommand(String command,InputStream in,PrintStream out,String endStr){ out.println(command); out.flush(); String result = readInfo(in,endStr); return result; } /** * 获取文件服务器的相关配置信息 * * @return 配置信息 */ public Map getConfig(){ try{ Map configs = new HashMap(); Properties prop = new Properties(); InputStream in = AccessRemoteServer.class.getResourceAsStream(CONFIG_FILE_PATH); prop.load(in); configs.put("user", prop.getProperty("server.user")); configs.put("pwd", prop.getProperty("server.password")); configs.put("host", prop.getProperty("server.host")); configs.put("port", prop.getProperty("server.port")); return configs; } catch (IOException e){ System.out.println("读取服务器配置信息失败!"); e.printStackTrace(); return null; } } // 测试程序 public static void main(String[] args){ AccessRemoteServer ser = new AccessRemoteServer(); System.out.println(ser.executeCmd("ls -F ./bin")); }
转载请注明原文地址: https://www.6miu.com/read-5049315.html

最新回复(0)