-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncatesyslog.py
113 lines (90 loc) · 3.35 KB
/
truncatesyslog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Check if the script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Update package list
sudo apt-get update
# Install MySQL server
sudo apt-get install -y mysql-server
# Automatically secure MySQL installation
# Set the root password and apply security settings
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password sigma'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password sigma'
# Run the secure installation steps with automatic responses
sudo mysql -u root -p'sigma' -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'sigma';"
sudo mysql -u root -p'sigma' -e "DELETE FROM mysql.user WHERE User='';"
sudo mysql -u root -p'sigma' -e "DROP DATABASE IF EXISTS test;"
sudo mysql -u root -p'sigma' -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
sudo mysql -u root -p'sigma' -e "FLUSH PRIVILEGES;"
# Create the sigma_db database and sigma user
sudo mysql -u root -p'sigma' -e "CREATE DATABASE IF NOT EXISTS sigma_db;"
sudo mysql -u root -p'sigma' -e "CREATE USER IF NOT EXISTS 'sigma'@'localhost' IDENTIFIED BY 'sigma';"
sudo mysql -u root -p'sigma' -e "GRANT ALL PRIVILEGES ON sigma_db.* TO 'sigma'@'localhost';"
sudo mysql -u root -p'sigma' -e "FLUSH PRIVILEGES;"
# Install Python3 and pip
sudo apt-get install -y python3 python3-pip
# Install required Python packages
pip3 install -r requirements.txt
# Run the Initializer_DB.py script to initialize the SQL tables
python3 Initializer_DB.py
# Get the current directory
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# Create the sql.service file
cat <<EOL | sudo tee /etc/systemd/system/sql.service
[Unit]
Description=SQL Service
After=network.target
[Service]
User=root
WorkingDirectory=$SCRIPT_DIR
ExecStart=/usr/bin/python3 $SCRIPT_DIR/SQL.py
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Create the dbscan.service file
cat <<EOL | sudo tee /etc/systemd/system/dbscan.service
[Unit]
Description=DBSCAN Service
After=sql.service
Requires=sql.service
[Service]
User=root
WorkingDirectory=$SCRIPT_DIR
ExecStart=/usr/bin/python3 $SCRIPT_DIR/dbscan.py
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Create the logger.service file
cat <<EOL | sudo tee /etc/systemd/system/logger.service
[Unit]
Description=Logger Service
After=network.target
After=dbscan.service
Requires=dbscan.service
[Service]
User=root
WorkingDirectory=$SCRIPT_DIR
ExecStart=/usr/bin/python3 $SCRIPT_DIR/logger.py
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Ensure truncatesyslog.py is executable
chmod +x $SCRIPT_DIR/truncatesyslog.py
# Create a cron job to run the truncatesyslog.py script every hour
(crontab -l 2>/dev/null; echo "0 * * * * /usr/bin/python3 $SCRIPT_DIR/truncatesyslog.py") | crontab -
# Reload systemd, enable and start the services
sudo systemctl daemon-reload
sudo systemctl enable sql.service
sudo systemctl start sql.service
sleep 5 # Wait for 5 seconds before starting the dbscan service
sudo systemctl enable dbscan.service
sudo systemctl start dbscan.service
sleep 5 # Wait for 5 seconds before starting the logger service
sudo systemctl enable logger.service
sudo systemctl start logger.service
echo "Setup complete. MySQL server and all required Python packages have been installed. Services have been created and started. Cron job for truncatesyslog.py has been added."