???????????????????????????????????????????????????????????????????????????????Java??Jsch???VPN????Linux????????????JAVA????Linux?????Mysql??shell?????????????????????????а?????????????????????????????????????

?????????????·??

??????????????VPN???????????????????????????????????ssh?????????22??????????????????????5555???????ssh?????????????5555?????????????????????????????????????????????????

??????????????VPN????Linux?????????????????????????????VPN?????????????????????Jsch??example????н????

???????????Jsch???VPN

package com.daily.wednesday; 
import java.io.IOException; 
import java.io.InputStream; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import com.daily.util.DataBaseConnection; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.JSchException; 
import com.jcraft.jsch.Session; 
public class BackUpMysql3 { 
    public static void main(String args[]) { 
        // ????????????
        DataBaseConnection dataBaseConnection = new DataBaseConnection(); 
        String dataBaseConfigForWrite[] = new String[3]; 
        dataBaseConfigForWrite = dataBaseConnection.loadDataConfig(); 

        Connection conn = null;// ?????????
        Statement stmt = null;// ????????
        ResultSet rs = null; // ?????
        int rowcount = 0;// ??????
        String sql = "select * from servers_maint_wednesday"; 

        try { 
            conn = DriverManager.getConnection(dataBaseConfigForWrite[0]?? 
                    dataBaseConfigForWrite[1]?? dataBaseConfigForWrite[2]); 
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql); 
            rs.last(); 
            rowcount = rs.getRow();// ??????
            rs = stmt.executeQuery(sql); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        } 
        // ?????????????IP?????飬?????????IP??????????С?
        String privateIpaddress[] = new String[rowcount]; 
        String remark[] = new String[rowcount];// ?????????????
        String programPath[] = new String[rowcount];// ???????·??
        String backMysqlShellPath[] = new String[rowcount];// ????mysql??????·??

        int j = 0; 
        try { 
            while (rs.next()) { 
                privateIpaddress[j] = rs.getString("privateipaddress"); 
                remark[j] = rs.getString("remarks"); 
                programPath[j] = rs.getString("programpath"); 
                backMysqlShellPath[j] = rs.getString("backmysqlshellpath"); 
                j++; 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (rs != null) { 
                    rs.close(); 
                } 
                if (stmt != null) { 
                    stmt.close(); 
                } 
                if (conn != null) { 
                    conn.close(); 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
        } 

        // ????mysql???????
        for (int n = 0; n < privateIpaddress.length; n++) { 
            try { 
                try { 
                    backUpMysql(privateIpaddress[n]?? backMysqlShellPath[n]??remark[n]); 
                } catch (IOException e) { 
                    // TODO Auto-generated catch block
                    e.printStackTrace(); 
                } 
            } catch (JSchException e) { 
                // TODO Auto-generated catch block
                e.printStackTrace(); 
            } 
        } 

    } 
    /**
     * ????mysql?????????
     * @param privateip
     * @param backMysqlShellPath
     * @throws JSchException
     * @throws IOException
     */
    public static void backUpMysql(String privateip?? String backMysqlShellPath?? String remark) 
            throws JSchException?? IOException { 
        // ???????????
        int rport; 
        JSch jsch = new JSch(); 
        String host = "dl.dengdie.com"; //????VPN?????????
        String user = "admin"; //VPN?????
        Session sessionForBack = jsch.getSession(user?? host?? 22); 
        rport = 22; 
        sessionForBack.setPassword("&*&&&&lalaflls"); //VPN????
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking"?? "no"); 
        sessionForBack.setConfig(config); 
        sessionForBack.connect();//?????VPN??????
         
        // ?????????????????ssh???????:???????????????22??ssh????????????????53238????      
        sessionForBack.setPortForwardingL(53238?? privateip?? rport); 

        try { 
            JSch jschToBack = new JSch(); 

            Session sessionToBack = jschToBack.getSession(user?? "127.0.0.1"?? 
                    53238); //?????????????53238?????????????????????????22?????
            sessionToBack.setPassword("&*&&&&lalaflls"); 
            sessionToBack.setConfig(config); 
            sessionToBack.connect(); 
            //backMysqlShellPath?????????????????????Mysql?????????????????????????????????????????
            String command = backMysqlShellPath; 
            //???????????????????????
            Channel channel = sessionToBack.openChannel("exec"); 
            ((ChannelExec) channel).setCommand(command); 
            channel.setInputStream(null); 
            ((ChannelExec) channel).setErrStream(System.err); 
            InputStream in = channel.getInputStream(); 
            channel.connect(); 

            byte[] tmp = new byte[1024]; 
            while (true) { 
                while (in.available() > 0) { 
                    int i = in.read(tmp?? 0?? 1024); 
                    if (i < 0) 
                        break; 
                    System.out.print(new String(tmp?? 0?? i)); 
                } 
                if (channel.isClosed()) { 
                    System.out.println(remark + "Mysql???????!"); 
                    System.out.println("exit-status: "
                            + channel.getExitStatus()); 
                    break; 
                } 
                try { 
                    Thread.sleep(1000); 
                } catch (Exception ee) { 
                } 
            } 
            channel.disconnect(); 
            sessionToBack.disconnect(); 
            sessionForBack.disconnect(); 
        } catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}