package postgre_dump; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; public class Test_dump { public static void main(String[] args) { String host = "192.168.5.11"; Connection conn = null; Session session = null; BufferedReader br = null; try { //建立连接 conn = new Connection(host); conn.connect(); //用户名和密码 boolean status = conn.authenticateWithPassword("postgres", "123456"); //链接是否成功 if(status){ session = conn.openSession(); //运行的命令 //pg_dump -U postgres oesv11 -t oes.poi -f /home/share/01.数据备份/poi.sql session.execCommand("pg_dump -U postgres oesv11 -t oes.poi -f /home/share/01.数据备份/poi.sql"); InputStream stdout = new StreamGobbler(session.getStdout()); br = new BufferedReader(new InputStreamReader(stdout)); String line = null; while((line = br.readLine()) != null){ System.out.println(line); System.out.println(); } } } catch (IOException e) { e.printStackTrace(); }finally{ if(br != null){ try { br.close(); session.close(); conn.close(); } catch (IOException e) { e.printStackTrace(); } } } } }