<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    基于開源ssh Ganymed 的ssh遠(yuǎn)程連接工具(共同討論版)

    本工具模仿putty 實(shí)現(xiàn)了簡單的ssh 遠(yuǎn)程交互功能。
    本工具可嵌入到集群管理工具
    目前尚在探索,錯(cuò)誤之處歡迎指正

    主干代碼如下 轉(zhuǎn)載注明:http://www.tkk7.com/roymoro/archive/2012/08/12/385318.html
      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==nullreturn 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                closeShell();
    106                }

    107            
    108                   // System.out.println(session.getExitStatus());
    109                   
    110    }

    111    private static void closeShell(){
    112        session.close();
    113        session.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 30000);
    114        //StringBuilder sb=new StringBuilder();\
    115        System.out.println("byebye");
    116    }

    117    /**
    118     * test
    119     * @param args
    120     * @throws IOException 
    121     */

    122    public static void main(String[] args) throws IOException {
    123        Scanner scan=new Scanner(System.in);
    124        while(scan.hasNext()){
    125        LinuxFSTool.executeCommand(scan.nextLine(), System.out,null);
    126        }

    127        
    128    }

    129}

    130
    131class ReadThread extends Thread{
    132    private InputStream in;//輸入流
    133    private PrintStream out;
    134    private String charset;
    135    private JTextArea jta;
    136    private boolean flag=true;
    137    public void stopThread(){
    138        flag=false;
    139    }

    140    public ReadThread(InputStream in,PrintStream out,String charset) {
    141        this.out=out;
    142        this.in = in;
    143        this.charset=charset;
    144    }

    145    public ReadThread(InputStream in,PrintStream out,JTextArea jta,String charset) {
    146        this.out=out;
    147        this.jta=jta;
    148        this.in = in;
    149        this.charset=charset;
    150    }

    151    public ReadThread(InputStream in,PrintStream out,JTextArea jta) {
    152        this(in, out, jta, "utf-8");
    153    }

    154    public ReadThread(InputStream in,PrintStream out) {
    155    this(in,out,"utf-8");
    156        
    157    }

    158    public ReadThread(InputStream in,String charset) {
    159        this(in,System.out,charset);
    160    
    161    }

    162    
    163    public ReadThread(InputStream in){
    164        this(in, "utf-8");
    165    }

    166    
    167    @Override
    168    public void run() {
    169        BufferedReader br;
    170        try {
    171            br = new BufferedReader(new InputStreamReader(in,charset));
    172        
    173         String temp;
    174           
    175                while((temp=br.readLine())!=null&&flag==true){
    176        // while(br.ready()&&flag==true){//
    177            // temp=br.readLine();
    178                    if(out!=null)
    179                    {    out.println(temp);
    180                    out.flush();}

    181                    if(jta!=null){
    182                        jta.setText(jta.getText()+temp+"\n");
    183                    }

    184                }

    185                
    186    }
     catch (UnsupportedEncodingException e1) {
    187        // TODO Auto-generated catch block
    188        e1.printStackTrace();
    189    }
     catch (IOException e) {
    190        // TODO Auto-generated catch block
    191        e.printStackTrace();
    192    }
    }

    193    
    194}

    195

    目前本工具存在如下問題:
    當(dāng)輸入例如 rm wenjian時(shí) ,由于正常的ssh 會(huì)返回是否刪除確認(rèn),初步分析 由于Ganymed 對(duì)返回消息的阻塞,本程序 是否刪除某文件顯示不出來。只有在下一次命令輸入時(shí)才返回 是否刪除某文件 ?新打入的命令。eclipse項(xiàng)目包,見附件。望大神,大叔,大嬸指導(dǎo)。
    其余bug 也歡迎指導(dǎo),本文僅供參考,不足之處多有諒解,轉(zhuǎn)帖請(qǐng)注明地址。
     


    附件下載地址

    posted on 2012-08-12 18:34 scorpio小蝎 閱讀(5493) 評(píng)論(4)  編輯  收藏 所屬分類: java

    評(píng)論

    # re: 基于開源ssh Ganymed 的ssh遠(yuǎn)程連接工具(共同討論版) 2012-08-12 19:15 scorpio小蝎

    連續(xù)使用命令 為command1 && command2
    但是rm filename && rm:
    輸出結(jié)果為:
    [root@hadoop01 ~]# rm wangmian && y

    再次輸入 ls后,輸出結(jié)果為:

    是否刪除普通文件 "filename"?ls
    說明結(jié)果是被阻塞  回復(fù)  更多評(píng)論   

    # re: 基于開源ssh Ganymed 的ssh遠(yuǎn)程連接工具(共同討論版) 2012-10-11 15:21 qq304863386

    可以直接執(zhí)行 su - 切換用戶命令嗎?發(fā)現(xiàn)切換用戶用ganymed有問題,老是切換不了  回復(fù)  更多評(píng)論   

    # re: 基于開源ssh Ganymed 的ssh遠(yuǎn)程連接工具(共同討論版) 2015-07-09 14:53 taisenki

    返回結(jié)果被阻塞是由于while((temp=br.readLine())!=null&&flag==true)這里的br.readLine(),由于需要輸入時(shí)是不存在換行的,所以無法讀到完整的一行,一直阻塞  回復(fù)  更多評(píng)論   

    <2012年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    友情鏈接

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲a∨无码精品色午夜| 亚洲婷婷在线视频| 亚洲AV无码久久精品色欲| 亚洲国产成人久久精品动漫| 亚洲大香人伊一本线| 亚洲欧美自偷自拍另类视| 无码AV动漫精品一区二区免费| 中文字幕一区二区三区免费视频| 日本中文字幕免费高清视频| 一二三四免费观看在线电影| 国产免费人视频在线观看免费 | 青青视频观看免费99| 国产黄色片在线免费观看| 久久久久一级精品亚洲国产成人综合AV区| 亚洲国产精华液网站w| 亚洲日韩乱码中文无码蜜桃臀| 激情无码亚洲一区二区三区| 免费看少妇高潮成人片| 亚洲日本在线免费观看| 日本a级片免费看| 国产精品国产亚洲精品看不卡| 国产成人精品日本亚洲网址| 一级a性色生活片久久无少妇一级婬片免费放 | 免费精品国自产拍在线播放| 国产亚洲免费的视频看| 毛片免费观看的视频| 国产亚洲AV手机在线观看| 国产精品亚洲专区在线观看| eeuss免费天堂影院| 69式国产真人免费视频| 亚洲一级片内射网站在线观看| 亚洲图片中文字幕| 羞羞视频免费观看| 91免费在线播放| 亚洲午夜av影院| 亚洲依依成人亚洲社区| 久久精品免费观看| 免费大学生国产在线观看p| 亚洲天天做日日做天天欢毛片| 美女黄频a美女大全免费皮| 182tv免费视视频线路一二三|