サンプルコード
@Grab(group='ch.ethz.ganymed', module='ganymed-ssh2', version='262') import ch.ethz.ssh2.* host = "your-linux-box" username = "user" password = "password" // サーバに接続 conn = new Connection(host) conn.connect() if( !conn.authenticateWithPassword(username, password) ){ throw new Exception("authentication failed.") } sess = conn.openSession() sess.execCommand("""lsb_release -ir""") def result = [:] // 標準出力 stdout = new BufferedReader(new InputStreamReader( new StreamGobbler(sess.getStdout()), "UTF-8")) stdout.withCloseable{ it.eachLine { line -> result.put( line.split(":")[0], line.split(":")[1].trim() ) } } // 標準エラー出力 stderr = new BufferedReader(new InputStreamReader( new StreamGobbler(sess.getStderr()), "UTF-8")) stderr.withCloseable{ it.eachLine { line -> println line } } println result sess.close() conn.close()
○関連項目
0 件のコメント:
コメントを投稿