JavaでSCP

とりあえずやってみたってだけで、あってるかは不明。
ちゃんと使うには、もっと中身を覗いていかないとだめかも。

ライブラリ

ganymed-ssh-2 というのを使ってみる。
https://code.google.com/p/ganymed-ssh-2/

バージョンは(Mavenであがってた)262というもの。

ドキュメントがあまりなさそう(古いのはリソースがけっこうある?)で、妄想だけで作ってくのは辛いです・・・

サンプル

public static void main(String[] args) {
    
    Connection conn = null;
    try  {
        conn = new Connection("xxx.xxx.xxx.xxx");
        conn.connect();
        
        boolean auth = conn.authenticateWithPassword("hoge", "foo");
        if (!auth) {
            System.err.println("auth error.");
            return;
        }
        
        File file = new File("C:/tmp/sample.txt");
        SCPClient scp = conn.createSCPClient();
        
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            SCPOutputStream out = scp.put(file.getName(), file.length(), "/home/hoge/", null);) {
            IOUtils.copy(in, out); // Commons IOを使う手抜き
        } 
        
    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}

ConnectionクラスはAutoCloseableを実装してない・・・Java7からだしね・・・