当前位置:编程学习 > 网站相关 >>

编程小记

本工具模仿putty 实现了简单的ssh 远程交互功能。
本工具可嵌入到集群管理工具
目前尚在探索,错误之处欢迎指正

  1package cn.edu.neu.neuInfo.until.shell;
  2
  3import java.io.BufferedReader;
  4import java.io.IOException;
  5import java.io.InputStream;
  6import java.io.InputStreamReader;
  7import java.io.OutputStream;
  8import java.io.OutputStreamWriter;
  9import java.io.PrintStream;
 10import java.io.PrintWriter;
 11import java.io.UnsupportedEncodingException;
 12import java.util.Scanner;
 13
 14import javax.swing.JTextArea;
 15
 16
 17
 18import ch.ethz.ssh2.ChannelCondition;
 19import ch.ethz.ssh2.Connection;
 20import ch.ethz.ssh2.ConnectionInfo;
 21import ch.ethz.ssh2.Session;
 22
 23public class LinuxFSTool {
 24     private static Connection con;
 25     private static Session session;
 26    private static PrintWriter out;
 27     static ReadThread rt;
 28    static ReadThread rt2;
 29    static OutputStream outStream;
 30    private static String address,username,password;
 31     private static NeuInfoConfigration neuInfoConfig=NeuInfoConfigration.getInstance("linuxTool.properties");
 32   
 33     public static boolean setConnect(String username,String password,String address) throws IOException{
 34         LinuxFSTool.address=address;
 35         LinuxFSTool.username=username;
 36         LinuxFSTool.password=password;
 37        return connectFS();
 38     }
 39     public static boolean isconnected()
 40     {
 41         if(con==null) return false;
 42         return true;
 43     }
 44     public static boolean connectFS() throws IOException{
 45        if(address==null||address.equals(""))
 46         address=neuInfoConfig.getvalue(Parameters.Linux_ADDRESS.name);
 47        if(username==null||username.equals(""))
 48         username=neuInfoConfig.getvalue(Parameters.Linux_USERNAME.name);
 49        if(password==null||password.equals(""))
 50        password=neuInfoConfig.getvalue(Parameters.Linux_PASSWORD.name);
 51        System.out.println(address+":"+username+":"+password);
 52          con = new Connection(address);
 53         ConnectionInfo info = con.connect();
 54         //System.out.println(info.serverHostKey);
 55         boolean result = con.authenticateWithPassword(username, password);
 56         if(result==false){ con.close();con=null;}
 57         return result;
 58    }
 59    public static void disconnectFS() throws IOException{
 60        if(rt.isAlive()){
 61            rt.stopThread();
 62        }
 63        if(rt2.isAlive()){
 64            rt2.stopThread();
 65        }
 66        session.close();
 67        con.close();
 68        session=null;
 69        con=null;
 70    }
 71    private static void shellRun(OutputStream outStream,JTextArea jta){
 72        LinuxFSTool.outStream=outStream;
 73        try{
 74            if(con==null) connectFS();//如果未连接 则连接
 75         session = con.openSession();
 76        session.requestPTY("bash");
 77        session.startShell();
 78        InputStream in=session.getStdout();
 79         rt=new ReadThread(in,new PrintStream(outStream),jta);
 80        rt2=new ReadThread(session.getStderr(),new PrintStream(outStream),jta);
 81        rt.start(); 
 82        rt2.start();
 83        out = new PrintWriter(new OutputStreamWriter(session.getStdin(),"UTF-8"));
 84    } catch (Exception ex) {
 85        System.out.println(ex.getLocalizedMessage());
 86        ex.printStackTrace();
 87    }
 88    }
 89    public static void executeCommand(String command){
 90        executeCommand(command,System.out,null);
 91    }
 92    /**
 93     *  use these instead of {@link LinuxFSTool#executeCommand(String)}
 94     * @param command
 95     * @param outStream
 96     */
 97    public static void executeCommand(String command,OutputStream outStream,JTextArea jta){ 
 98               
 99            if(session==null)
100                shellRun(outStream,jta);
101            if(!command.equals("")) {
102            out.println(command);}
103            out.flush();
104            if(command.endsWith("bye")){
105          

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,