如何使用Java管理千台規模Linux伺服器

來源:互聯網
上載者:User

前東家是一家遊戲公司,老闆很好,當時工作也留下了很多自己原創的管理指令碼。現在分享一下在辦公環境使用Java、Jsch登入VPN管理Linux的指令碼(此處實現JAVA調用Linux上備份Mysql的shell作為樣本),希望對營運的朋友有協助,儘快從繁雜的伺服器管理工作中脫離出來。

主要的實現思路:

如果需要先登入VPN才能串連遊戲伺服器,需要將遊戲伺服器的ssh連接埠(一般是22)映射到本地辦公電腦的連接埠上(如5555),然後ssh串連本地辦公電腦的5555連接埠,這樣就可以串連到遊戲伺服器,並可以管理遊戲伺服器了。

當您學會通過VPN串連Linux伺服器後,如果只在內網環境,不使用VPN,就更簡單了,此外不再詳述。Jsch的example裡也有介紹。

代碼:使用Jsch透過VPN

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. // 讀取資料庫配置

18. DataBaseConnection dataBaseConnection = new DataBaseConnection();

19. String dataBaseConfigForWrite[] = new String[3];

20. dataBaseConfigForWrite = dataBaseConnection.loadDataConfig();

21.22. Connection conn = null;// 資料庫連接

23. Statement stmt = null;// 資料庫運算式

24. ResultSet rs = null; // 結果集

25. int rowcount = 0;// 總記錄數

26. String sql = "select * from servers_maint_wednesday";

27.28. try {

29. conn = DriverManager.getConnection(dataBaseConfigForWrite[0],

30. dataBaseConfigForWrite[1], dataBaseConfigForWrite[2]);

31. stmt = conn.createStatement();

32. rs = stmt.executeQuery(sql);

33. rs.last();

34. rowcount = rs.getRow();// 總記錄數

35. rs = stmt.executeQuery(sql);

36. } catch (SQLException e) {

37. e.printStackTrace();

38. }

39. // 定義遊戲伺服器IP的數組,遊戲伺服器IP存在資料庫中。

40. String privateIpaddress[] = new String[rowcount];

41. String remark[] = new String[rowcount];// 定義遊戲區名稱

42. String programPath[] = new String[rowcount];// 定義程式路徑

43. String backMysqlShellPath[] = new String[rowcount];// 定義mysql備份指令碼路徑

44.45. int j = 0;

46. try {

47. while (rs.next()) {

48. privateIpaddress[j] = rs.getString("privateipaddress");

1

50. programPath[j] = rs.getString("programpath");

51. backMysqlShellPath[j] = rs.getString("backmysqlshellpath");

52. j++;

53. }

54. } catch (Exception e) {

55. e.printStackTrace();

56. } finally {

57. try {

58. if (rs != null) {

59. rs.close();

60. }

61. if (stmt != null) {

62. stmt.close();

63. }

64. if (conn != null) {

65. conn.close();

66. }

67. } catch (Exception e) {

68. e.printStackTrace();

69. }

70. }

71.72. // 調用mysql備份方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.