使用Java管理千台规模Linux服务器_入门
上一篇 /
下一篇 2012-07-20 09:36:26
/ 个人分类:杂谈
前东家是一家游戏公司,老板很好,当时工作也留下了很多自己原创的管理脚本。现在分享一下在办公环境使用Java、Jsch登录VPN管理Linux的脚本(此处实现JAVA调用Linux上备份Mysql的shell作为示例),希望对运维的朋友有帮助,尽快从繁杂的服务器管理工作中脱离出来。/A\xXlx0 主要的实现思路:51Testing软件测试网6{CXN,I%ps&I;i
51Testing软件测试网*es7XgFHX0GCX 如果需要先登录VPN才能连接游戏服务器,需要将游戏服务器的ssh端口(一般是22)映射到本地办公电脑的端口上(如5555),然后ssh连接本地办公电脑的5555端口,这样就可以连接到游戏服务器,并可以管理游戏服务器了。
lTE2?&bz$\rEal0$t+h1L]
bG'y0 当您学会通过VPN连接Linux服务器后,如果只在内网环境,不使用VPN,就更简单了,此外不再详述。Jsch的example里也有介绍。51Testing软件测试网4dqJ Q#i];j
WRD(U.h5^;o$@i'z0 代码:使用Jsch透过VPN51Testing软件测试网.~|x7Rt!N$o1U
o
- 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();
r&W}7jVOE0- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- int rowcount = 0;
- String sql = "select * from servers_maint_wednesday";
&M1hw-T(j#F+n@1G0- 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();
- }
-
- String privateIpaddress[] = new String[rowcount];
- String remark[] = new String[rowcount];
- String programPath[] = new String[rowcount];
- String backMysqlShellPath[] = new String[rowcount];
^%R/M"i7NAO0- 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();
- }
- }
K"[Z ITD9N0-
- for (int n = 0; n < privateIpaddress.length; n++) {
- try {
- try {
- backUpMysql(privateIpaddress[n], backMysqlShellPath[n],remark[n]);
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- } catch (JSchException e) {
-
- e.printStackTrace();
- }
- }
- 51Testing软件测试网t+yJ)Y0Y6Fc
- }
-
-
-
-
-
-
-
- public static void backUpMysql(String privateip, String backMysqlShellPath, String remark)
- throws JSchException, IOException {
-
- int rport;
- JSch jsch = new JSch();
- String host = "dl.dengdie.com";
- String user = "admin";
- Session sessionForBack = jsch.getSession(user, host, 22);
- rport = 22;
- sessionForBack.setPassword("&*&&&&lalaflls");
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- sessionForBack.setConfig(config);
- sessionForBack.connect();
-
-
- sessionForBack.setPortForwardingL(53238, privateip, rport);
T*k-xK+nA5en0- try {
- JSch jschToBack = new JSch();
}e ZL3rN.zE)T(pq0- Session sessionToBack = jschToBack.getSession(user, "127.0.0.1",
- 53238);
- sessionToBack.setPassword("&*&&&&lalaflls");
- sessionToBack.setConfig(config);
- sessionToBack.connect();
-
- 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();
- 51Testing软件测试网 h,_Q\O,??K
- 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);
- }
- }
- }
收藏
举报
TAG: