5
  1. サーバー(ユーザー名、paw、ホスト)に接続する必要があります--簡単

  2. 3 ~ 10 個のコマンドを入力します -- command="dir;date;cd;dir" 20 行も書かずにもっと簡単な方法はありますか?

  3. ファイルをダウンロード -- 簡単

  4. ダウンロードした別のファイルを同じファイルに書き込みます(owerrideではなく追加します)-方法はありますか?

Jsch (すばらしいドキュメント) をあえて使用すると不可能に思えるかもしれない、これらの信じられないほど簡単なタスクを実行するには、Jsch、sshj、Ganymed の間で選択肢があります。何か提案はありますか?

神秘:

2) 複数コマンド入力

4) 既存の txt ファイルにさらに txt :D を追加する (おそらく組み込みコマンドがある) かどうか。

  /* just for download/owerride : sftpChannel.get("downloadfile.txt", "savefile.txt");*/
4

2 に答える 2

5

ガニメッドは知らん しかし、私はリモート ログインとスクリプトの実行に JSch を広く使用しています。私は Google の Expect4j を Jsch と共に使用して、リモート マシン上でスクリプトを期待モード (送信/待機) で実行しました。JSch/Expect4j/Closures を使用して、コード内で実行されたコマンドまたはスクリプトの出力全体を取得できます。

jsch については、http:
//www.jcraft.com/jsch/ にアクセスしてください 。 Expect4j については、http ://code.google.com/p/expect4j/ にアクセスしてください。

以下は、リモート Java クラスのファイルにログインして実行するための小さなコード サンプルです。

private Expect4j SSH(String hostname, String username,String password, int port) throws Exception {
    JSch jsch = new JSch();
    Session session = jsch.getSession(username, hostname, port);
    if (password != null) {         
        session.setPassword(password);
    }
    Hashtable<String,String> config = new Hashtable<String,String>();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect(60000);
    channel = (ChannelShell) session.openChannel("shell");
    Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
    channel.connect();      
    return expect;
}

このメソッドは、expect4j がコマンドを送信するために使用するリモート サーバーへの SSH ストリームを開きます。

private boolean executeCommands() {
        boolean isSuccess = true;
        Closure closure = new Closure() {
            public void run(ExpectState expectState) throws Exception {
                buffer.append(expectState.getBuffer());//buffer is string buffer for appending output of executed command             
                expectState.exp_continue();
            }
        };
        List<Match> lstPattern =  new ArrayList<Match>();
        String[] regEx = SSHConstants.linuxPromptRegEx;  
        if (regEx != null && regEx.length > 0) {
            synchronized (regEx) {
                for (String regexElement : regEx) {//list of regx like,  :>, /> etc. it is possible command prompts of your remote machine
                    try {
                        RegExpMatch mat = new RegExpMatch(regexElement, closure);
                        lstPattern.add(mat);                        
                    } catch (MalformedPatternException e) {                     
                        return false;
                    } catch(Exception e) {                      
                        return false;
                    }
                }
                lstPattern.add(new EofMatch( new Closure() { // should cause entire page to be collected
                    public void run(ExpectState state) {
                    }
                }));
                lstPattern.add(new TimeoutMatch(defaultTimeOut, new Closure() {
                    public void run(ExpectState state) {
                    }
                }));
            }
        }
        try {
            Expect4j expect = SSH(objConfig.getHostAddress(), objConfig.getUserName(), objConfig.getPassword(), SSHConstants.SSH_PORT);
            expect.setDefaultTimeout(defaultTimeOut);       
            if(isSuccess) {
                for(String strCmd : lstCmds)
                    isSuccess = isSuccess(lstPattern,strCmd);
            }
            boolean isFailed = checkResult(expect.expect(lstPattern));
            return !isFailed;
        } catch (Exception ex) {            
            return false;
        } finally {
            closeConnection();
        }
    }


private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
        try {   
            boolean isFailed = checkResult(expect.expect(objPattern));

            if (!isFailed) {
                expect.send(strCommandPattern);         
                expect.send("\r");              
                return true;
            } 
            return false;
        } catch (MalformedPatternException ex) {    
            return false;
        } catch (Exception ex) {
            return false;
        }
}  
于 2011-02-24T04:30:09.940 に答える
1

他のものについてはコメントできませんが、Ganymed は非常にうまく機能します。

于 2011-02-24T01:26:22.083 に答える