[Rao] using Java to manage thousands of Linux servers _ get Started

Source: Internet
Author: User
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.

  1. Package com.daily.wednesday;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import java.sql.Connection;
  5. Import Java.sql.DriverManager;
  6. Import Java.sql.ResultSet;
  7. Import java.sql.SQLException;
  8. Import java.sql.Statement;
  9. Import com.daily.util.DataBaseConnection;
  10. Import Com.jcraft.jsch.Channel;
  11. Import com.jcraft.jsch.ChannelExec;
  12. Import Com.jcraft.jsch.JSch;
  13. Import com.jcraft.jsch.JSchException;
  14. Import com.jcraft.jsch.Session;
  15. public class BackUpMysql3 {
  16. public static void Main (String args[]) {
  17. Read Database configuration
  18. DatabaseConnection databaseconnection = new DatabaseConnection ();
  19. String databaseconfigforwrite[] = new STRING[3];
  20. Databaseconfigforwrite = Databaseconnection.loaddataconfig ();
  21. Connection conn = null;//database connection
  22. Statement stmt = null;//Database expression
  23. ResultSet rs = null; Result set
  24. int rowcount = 0;//total number of records
  25. String sql = "SELECT * from Servers_maint_wednesday";
  26. try {
  27. conn = Drivermanager.getconnection (Databaseconfigforwrite[0],
  28. DATABASECONFIGFORWRITE[1], databaseconfigforwrite[2]);
  29. stmt = Conn.createstatement ();
  30. rs = stmt.executequery (SQL);
  31. Rs.last ();
  32. RowCount = Rs.getrow ();//Total Record Count
  33. rs = stmt.executequery (SQL);
  34. } catch (SQLException e) {
  35. E.printstacktrace ();
  36. }
  37. Defines an array of game server IPs, the game server IP exists in the database.
  38. String privateipaddress[] = new String[rowcount];
  39. String remark[] = new string[rowcount];//defines the game area name
  40. String programpath[] = new string[rowcount];//defines the program path
  41. String backmysqlshellpath[] = new string[rowcount];//define MySQL backup script path
  42. int j = 0;
  43. try {
  44. while (Rs.next ()) {
  45. PRIVATEIPADDRESS[J] = rs.getstring ("PrivateIPAddress");
  46. REMARK[J] = rs.getstring ("Remarks");
  47. PROGRAMPATH[J] = rs.getstring ("Programpath");
  48. BACKMYSQLSHELLPATH[J] = rs.getstring ("Backmysqlshellpath");
  49. j + +;
  50. }
  51. } catch (Exception e) {
  52. E.printstacktrace ();
  53. } finally {
  54. try {
  55. if (rs! = null) {
  56. Rs.close ();
  57. }
  58. if (stmt! = null) {
  59. Stmt.close ();
  60. }
  61. IF (conn! = null) {
  62. Conn.close ();
  63. }
  64. } catch (Exception e) {
  65. E.printstacktrace ();
  66. }
  67. }
  68. Call the MySQL backup method
  69. for (int n = 0; n < privateipaddress.length; n++) {
  70. try {
  71. try {
  72. Backupmysql (Privateipaddress[n], backmysqlshellpath[n],remark[n]);
  73. } catch (IOException e) {
  74. TODO auto-generated Catch block
  75. E.printstacktrace ();
  76. }
  77. } catch (Jschexception e) {
  78. TODO auto-generated Catch block
  79. E.printstacktrace ();
  80. }
  81. }
  82. }
  83. /**
  84. * How to back up MySQL database
  85. * @param PRIVATEIP
  86. * @param Backmysqlshellpath
  87. * @throws jschexception
  88. * @throws IOException
  89. */
  90. public static void Backupmysql (String privateip, String Backmysqlshellpath, string remark)
  91. Throws Jschexception, IOException {
  92. Log on to the server
  93. int rport;
  94. Jsch Jsch = new Jsch ();
  95. String host = "dl.dengdie.com"; Here is the VPN server address
  96. String user = "admin"; VPN User name
  97. Session Sessionforback = jsch.getsession (user, host, 22);
  98. Rport = 22;
  99. Sessionforback.setpassword ("&*&&&&lalaflls"); VPN password
  100. java.util.Properties config = new java.util.Properties ();
  101. Config.put ("stricthostkeychecking", "no");
  102. Sessionforback.setconfig (config);
  103. Sessionforback.connect ();//Log on to the VPN server
  104. 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.
  105. Sessionforback.setportforwardingl (53238, Privateip, Rport);
  106. try {
  107. Jsch jschtoback = new Jsch ();
  108. Session Sessiontoback = jschtoback.getsession (user, "127.0.0.1",
  109. 53238); Connecting to the 53238 port of the local office computer is the equivalent of Port 22nd connected to the game server.
  110. Sessiontoback.setpassword ("&*&&&&lalaflls");
  111. Sessiontoback.setconfig (config);
  112. Sessiontoback.connect ();
  113. 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.
  114. String command = Backmysqlshellpath;
  115. Open the tunnel that executes the command, and execute the command.
  116. Channel channel = Sessiontoback.openchannel ("exec");
  117. ((channelexec) channel). SetCommand (command);
  118. Channel.setinputstream (NULL);
  119. ((channelexec) channel). Seterrstream (System.err);
  120. InputStream in = Channel.getinputstream ();
  121. Channel.connect ();
  122. byte[] tmp = new byte[1024];
  123. while (true) {
  124. while (in.available () > 0) {
  125. int i = in.read (tmp, 0, 1024);
  126. if (I < 0)
  127. Break
  128. System.out.print (New String (TMP, 0, i));
  129. }
  130. if (channel.isclosed ()) {
  131. SYSTEM.OUT.PRINTLN (remark + "MySQL backup complete!");
  132. System.out.println ("Exit-status:"
  133. + Channel.getexitstatus ());
  134. Break
  135. }
  136. try {
  137. Thread.Sleep (1000);
  138. } catch (Exception ee) {
  139. }
  140. }
  141. Channel.disconnect ();
  142. Sessiontoback.disconnect ();
  143. Sessionforback.disconnect ();
  144. } catch (Exception e) {
  145. System.out.println (e);
  146. }
  147. }
  148. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.