-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Now update key when 1 day reimagining 3. New logic code 4. Some fix P. S. This update not tested
- Loading branch information
Showing
7 changed files
with
251 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,16 @@ | ||
import com.jcraft.jsch.Channel; | ||
import com.jcraft.jsch.ChannelExec; | ||
import com.jcraft.jsch.JSch; | ||
import com.jcraft.jsch.Session; | ||
|
||
import java.io.InputStream; | ||
import java.sql.*; | ||
import java.util.HashMap; | ||
import db.ConnectionsData; | ||
import threads.ThreadGetDate; | ||
|
||
public class Main { | ||
|
||
private static final HashMap<Integer, String> keys = new HashMap<>(); | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
if (args.length != 7) { | ||
throw new Exception("Wrong number of arguments"); | ||
} | ||
|
||
final String con = args[0]; | ||
final String user = args[1]; | ||
final String pass = args[2]; | ||
final String sshHost = args[3]; | ||
final String sshLogin = args[4]; | ||
final String sshPass = args[5]; | ||
final String databaseName = args[6]; | ||
|
||
try { | ||
for (; ; ) { | ||
|
||
String query = "SELECT id, text FROM Plesk WHERE id = 0"; | ||
String delete = "DELETE FROM Plesk WHERE id = ?"; | ||
String update = "UPDATE Plesk SET id = id - 1 WHERE id >= ?"; | ||
Connection conn = DriverManager.getConnection( | ||
"jdbc:mysql://" + con + ":3306/" + databaseName | ||
+ "?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", user, pass); | ||
Statement statement = conn.createStatement(); | ||
ResultSet rs = statement.executeQuery(query); | ||
while (rs.next()) { | ||
String id = rs.getString("id"); | ||
String text = rs.getString("text"); | ||
keys.put(Integer.parseInt(id), text); | ||
} | ||
String command = "cd /; usr/sbin/plesk bin license -i " + keys.get(0).trim(); | ||
System.out.println(keys.get(0)); | ||
runCommand(command, sshPass, sshLogin, sshHost); | ||
|
||
//remove from DB and HashMap and close connections | ||
keys.remove(0); | ||
PreparedStatement preparedStmt = conn.prepareStatement(delete); | ||
PreparedStatement preparedStmt2 = conn.prepareStatement(update); | ||
preparedStmt.setInt(1, 0); | ||
preparedStmt2.setInt(1, 0); | ||
preparedStmt.executeUpdate(); | ||
preparedStmt2.executeUpdate(); | ||
rs.close(); | ||
statement.close(); | ||
preparedStmt.close(); | ||
preparedStmt2.close(); | ||
conn.close(); | ||
//sleep 14 days | ||
Thread.sleep(1209600000L); | ||
} | ||
} catch (Exception ex) { | ||
Thread.currentThread().interrupt(); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
protected static void runCommand(String command, String pass, String login, String host) { | ||
java.util.Properties config = new java.util.Properties(); | ||
config.put("StrictHostKeyChecking", "no"); | ||
JSch jsch = new JSch(); | ||
try { | ||
Session session = jsch.getSession(login, host, 22); | ||
session.setPassword(pass); | ||
session.setConfig(config); | ||
session.connect(); | ||
System.out.println("Connected"); | ||
|
||
Channel channel = session.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("exit-status: " + channel.getExitStatus()); | ||
break; | ||
} | ||
try { | ||
Thread.sleep(1000); | ||
} catch (Exception ignored) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
channel.disconnect(); | ||
session.disconnect(); | ||
System.out.println("DONE"); | ||
} catch (Exception exx) { | ||
exx.printStackTrace(); | ||
} | ||
final ConnectionsData connectionsData = new ConnectionsData(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); | ||
Thread thread = new ThreadGetDate(connectionsData); | ||
thread.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package db; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
@AllArgsConstructor | ||
public class ConnectionsData { | ||
|
||
private final String con; | ||
private final String user; | ||
private final String pass; | ||
private final String sshHost; | ||
private final String sshLogin; | ||
private final String sshPass; | ||
private final String databaseName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package db; | ||
|
||
import com.jcraft.jsch.Channel; | ||
import com.jcraft.jsch.ChannelExec; | ||
import com.jcraft.jsch.JSch; | ||
import com.jcraft.jsch.Session; | ||
import lombok.AllArgsConstructor; | ||
import model.Plesk; | ||
|
||
import java.io.InputStream; | ||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
public class DataBase { | ||
|
||
private final ConnectionsData connectionsData; | ||
private static final List<Plesk> keys = new ArrayList<>(); | ||
|
||
public void getRowsInTable() { | ||
try { | ||
final String query = "SELECT id, text FROM Plesk"; | ||
final String delete = "DELETE FROM Plesk WHERE id = "; | ||
|
||
Connection conn = DriverManager.getConnection( | ||
"jdbc:mysql://" + connectionsData.getCon() + ":3306/" + connectionsData.getDatabaseName() | ||
+ "?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", connectionsData.getUser(), connectionsData.getPass()); | ||
|
||
Statement statement = conn.createStatement(); | ||
|
||
ResultSet rs = statement.executeQuery(query); | ||
|
||
while (rs.next()) { | ||
String id = rs.getString("id"); | ||
String text = rs.getString("text"); | ||
keys.add(new Plesk(id, text)); | ||
} | ||
|
||
String command = "cd /; usr/sbin/plesk bin license -i " + keys.get(0).getText().trim(); | ||
|
||
System.out.println(keys.get(0).getText()); | ||
|
||
runCommand(command, connectionsData.getSshHost(), connectionsData.getSshLogin(), connectionsData.getSshPass()); | ||
|
||
PreparedStatement preparedStmt = conn.prepareStatement(delete + keys.get(0).getId()); | ||
preparedStmt.executeQuery(); | ||
|
||
//Close con | ||
preparedStmt.close(); | ||
rs.close(); | ||
statement.close(); | ||
keys.clear(); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void runCommand(String command, String pass, String login, String host) { | ||
java.util.Properties config = new java.util.Properties(); | ||
config.put("StrictHostKeyChecking", "no"); | ||
JSch jsch = new JSch(); | ||
try { | ||
Session session = jsch.getSession(login, host, 22); | ||
session.setPassword(pass); | ||
session.setConfig(config); | ||
session.connect(); | ||
System.out.println("Connected"); | ||
|
||
Channel channel = session.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("exit-status: " + channel.getExitStatus()); | ||
break; | ||
} | ||
try { | ||
Thread.sleep(1000); | ||
} catch (Exception ignored) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
channel.disconnect(); | ||
session.disconnect(); | ||
System.out.println("DONE"); | ||
} catch (Exception exx) { | ||
exx.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Plesk { | ||
|
||
private final String id; | ||
private final String text; | ||
} |
Oops, something went wrong.