Former club is a game company, the boss is very good, then the work has left a lot of their own original management scripts. Now share in the office environment using Java, Jsch login VPN Management Linux script (here to implement Java call backup MySQL shell as an example), hope for the operation of the friend to help, as soon as possible from the complex server management work out.
The main idea of implementation:
If you need to log in to the VPN before you can connect to the game server, you need to map the SSH port of the game server (typically 22) to the port on your local office PC (e.g. 5555), then ssh to the 5555 port of your local office computer, so you can connect to the game server. And can manage the game server.
when you learn to connect to a Linux server through a VPN, it is much easier to do so without using a VPN in the intranet environment, and is no longer detailed. Jsch's example is also introduced.
- 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[]) {
- Read Database configuration
- DatabaseConnection databaseconnection = new DatabaseConnection ();
- String databaseconfigforwrite[] = new STRING[3];
- Databaseconfigforwrite = Databaseconnection.loaddataconfig ();
- Connection conn = null;//database connection
- Statement stmt = null;//Database expression
- ResultSet rs = null; Result set
- int rowcount = 0;//total number of records
- 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 ();//Total Record Count
- rs = stmt.executequery (SQL);
- } catch (SQLException e) {
- E.printstacktrace ();
- }
- Defines an array of game server IPs, the game server IP exists in the database.
- String privateipaddress[] = new String[rowcount];
- String remark[] = new string[rowcount];//defines the game area name
- String programpath[] = new string[rowcount];//defines the program path
- String backmysqlshellpath[] = new string[rowcount];//define MySQL backup script path
- 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 ();
- }
- }
- Call the MySQL backup method
- 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 ();
- }
- }
- }
- /**
- * How to back up MySQL database
- * @param PRIVATEIP
- * @param Backmysqlshellpath
- * @throws jschexception
- * @throws IOException
- */
- public static void Backupmysql (String privateip, String Backmysqlshellpath, string remark)
- Throws Jschexception, IOException {
- Log on to the server
- int rport;
- Jsch Jsch = new Jsch ();
- String host = "dl.dengdie.com"; Here is the VPN server address
- String user = "admin"; VPN User name
- Session Sessionforback = jsch.getsession (user, host, 22);
- Rport = 22;
- Sessionforback.setpassword ("&*&&&&lalaflls"); VPN password
- java.util.Properties config = new java.util.Properties ();
- Config.put ("stricthostkeychecking", "no");
- Sessionforback.setconfig (config);
- Sessionforback.connect ();//Log on to the VPN server
- Establish an SSH forward connection to the game server: 53238 ports of the local office computer that will be mapped to the 22nd SSH Port of the game server.
- Sessionforback.setportforwardingl (53238, Privateip, Rport);
- try {
- Jsch jschtoback = new Jsch ();
- Session Sessiontoback = jschtoback.getsession (user, "127.0.0.1",
- 53238); Connecting to the 53238 port of the local office computer is the equivalent of Port 22nd connected to the game server.
- Sessiontoback.setpassword ("&*&&&&lalaflls");
- Sessiontoback.setconfig (config);
- Sessiontoback.connect ();
- Backmysqlshellpath is actually a script that backs up the MySQL database on the game server, and this script invites you to implement it yourself, many instances on the web.
- String command = Backmysqlshellpath;
- Open the tunnel that executes the command, and execute the command.
- 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 backup complete!");
- 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);
- }
- }
- }
Copy Code |