Skip to content

Commit d27bb72

Browse files
chore: ✨ more script added for automation
1 parent e649806 commit d27bb72

8 files changed

+350
-8
lines changed

README.md

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
# HackScripts
1+
# HackScripts?
22

3-
| Script | Description |
4-
| --- | --- |
5-
| [notification.sh](/other/notification.sh) | Send a notification to the machine. |
6-
| [setup_ssh.sh](/setup/setup_ssh.sh) | Setup SSH on your machine and get the ip address of the machine. |
7-
| [install_miniconda.sh](/setup/install_miniconda.sh) | Install Miniconda on your machine. |
8-
| [install_docker.sh](/setup/install_docker.sh) | Install Docker on your machine. | [install_docker.sh](/setup/install_docker.sh) |
9-
| [create_conda_env.sh](/setup/create_conda_env.sh) | Create a conda environment with latest python version. |
3+
## What is HackScripts?
104

5+
HackScripts is a collection of scripts that I use to automate the setup of my servers and development environments. The scripts are written in bash and are tested on Ubuntu 22.04 LTS.
6+
7+
## How to use HackScripts?
8+
9+
To use HackScripts, you can clone the repository and run the scripts that you need. The scripts are organized into folders based on their purpose. The `setup` folder contains scripts that set up the development environment, the `server` folder contains scripts that set up the server environment, and the `security` folder contains scripts that set up security features.
10+
11+
12+
## Scripts List
13+
14+
| | Script | Description | Status |
15+
| --- | --- | --- |
16+
| [setup_time_sync.sh](server/setup_time_sync.sh) | Description not set | Tested |
17+
| [notification.sh](other/notification.sh) | Description not set | Tested |
18+
| [create_conda_env.sh](setup/create_conda_env.sh) | Description not set | Tested |
19+
| [setup_vscode.sh](setup/setup_vscode.sh) | Description not set | Tested |
20+
| [setup_gh_ssh.sh](setup/setup_gh_ssh.sh) | Description not set | Tested |
21+
| [basic_setup.sh](setup/basic_setup.sh) | Description not set | Tested |
22+
| [install_miniconda.sh](setup/install_miniconda.sh) | Description not set | Tested |
23+
| [setup_docker.sh](setup/setup_docker.sh) | Description not set | Tested |
24+
| [setup_ssh.sh](setup/setup_ssh.sh) | Description not set | Tested |
25+
| [install_fail2ban.sh](security/install_fail2ban.sh) | Description not set | Tested |
26+
| [table_generator.sh](table_generator.sh) | Description not set | Tested |
27+
| [setup_redis_server.sh](database/setup_redis_server.sh) | Description not set | Tested |
28+
| [setup_postgres.sh](database/setup_postgres.sh) | Description not set | Tested |
29+
30+
31+
## Contributing
32+
33+
If you have a script that you would like to add to HackScripts, feel free to open a pull request. Please make sure that the script is well-documented and tested before submitting it.

database/setup_postgres.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Update package lists
7+
echo "Updating package lists..."
8+
sudo apt-get update
9+
10+
# Install prerequisites
11+
echo "Installing prerequisites..."
12+
sudo apt-get install -y wget ca-certificates
13+
14+
# Import PostgreSQL signing key and add the PostgreSQL APT repository
15+
echo "Adding PostgreSQL APT repository..."
16+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
17+
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
18+
19+
# Update package lists again with the new repository
20+
echo "Updating package lists with PostgreSQL repository..."
21+
sudo apt-get update
22+
23+
# Install the latest PostgreSQL version
24+
echo "Installing PostgreSQL..."
25+
sudo apt-get install -y postgresql postgresql-contrib
26+
27+
# Install the latest version of pgAdmin4
28+
echo "Installing pgAdmin4..."
29+
sudo apt-get install -y curl
30+
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
31+
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
32+
sudo apt-get install -y pgadmin4
33+
34+
# Optionally, run the web version setup for pgAdmin
35+
echo "Setting up pgAdmin4 web..."
36+
sudo /usr/pgadmin4/bin/setup-web.sh
37+
38+
# Enable and start PostgreSQL service
39+
echo "Enabling and starting PostgreSQL service..."
40+
sudo systemctl enable postgresql
41+
sudo systemctl start postgresql
42+
43+
# Create a new PostgreSQL user
44+
read -p "Enter the new PostgreSQL username: " pg_username
45+
read -s -p "Enter the password for the new PostgreSQL user: " pg_password
46+
echo
47+
48+
echo "Creating PostgreSQL user $pg_username..."
49+
sudo -u postgres psql -c "CREATE USER $pg_username WITH PASSWORD '$pg_password';"
50+
51+
# Optionally, create a new database owned by the new user
52+
read -p "Would you like to create a new database for this user? (y/n): " create_db
53+
54+
if [[ $create_db == "y" || $create_db == "Y" ]]; then
55+
read -p "Enter the new database name: " db_name
56+
sudo -u postgres psql -c "CREATE DATABASE $db_name OWNER $pg_username;"
57+
echo "Database $db_name created and owned by $pg_username."
58+
fi
59+
60+
echo "PostgreSQL and pgAdmin installation and user setup completed."

database/setup_redis_server.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Update package lists
7+
echo "Updating package lists..."
8+
sudo apt-get update
9+
10+
# Install prerequisites
11+
echo "Installing prerequisites..."
12+
sudo apt-get install -y wget gnupg
13+
14+
# Add PostgreSQL APT repository
15+
echo "Adding PostgreSQL APT repository..."
16+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/pgdg-archive-keyring.gpg
17+
echo "deb [signed-by=/usr/share/keyrings/pgdg-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
18+
19+
# Update package lists again with the new repository
20+
echo "Updating package lists with PostgreSQL repository..."
21+
sudo apt-get update
22+
23+
# Install PostgreSQL
24+
echo "Installing PostgreSQL..."
25+
sudo apt-get install -y postgresql postgresql-contrib
26+
27+
# Install pgAdmin4
28+
echo "Installing pgAdmin4..."
29+
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/pgadmin-archive-keyring.gpg
30+
echo "deb [signed-by=/usr/share/keyrings/pgadmin-archive-keyring.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/ $(lsb_release -cs) pgadmin4 main" | sudo tee /etc/apt/sources.list.d/pgadmin4.list
31+
sudo apt-get update
32+
sudo apt-get install -y pgadmin4
33+
34+
# Optionally, run the web version setup for pgAdmin
35+
echo "Setting up pgAdmin4 web..."
36+
sudo /usr/pgadmin4/bin/setup-web.sh
37+
38+
# Enable and start PostgreSQL service
39+
echo "Enabling and starting PostgreSQL service..."
40+
sudo systemctl enable postgresql
41+
sudo systemctl start postgresql
42+
43+
# Create a new PostgreSQL user
44+
read -p "Enter the new PostgreSQL username: " pg_username
45+
read -s -p "Enter the password for the new PostgreSQL user: " pg_password
46+
echo
47+
48+
echo "Creating PostgreSQL user $pg_username..."
49+
sudo -u postgres psql -c "CREATE USER $pg_username WITH PASSWORD '$pg_password';"
50+
51+
# Optionally, create a new database owned by the new user
52+
read -p "Would you like to create a new database for this user? (y/n): " create_db
53+
54+
if [[ $create_db == "y" || $create_db == "Y" ]]; then
55+
read -p "Enter the new database name: " db_name
56+
sudo -u postgres psql -c "CREATE DATABASE $db_name OWNER $pg_username;"
57+
echo "Database $db_name created and owned by $pg_username."
58+
fi
59+
60+
echo "PostgreSQL and pgAdmin installation and user setup completed."
File renamed without changes.

setup/basic_setup.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# indian timezone
4+
NEW_USER="admin"
5+
TIMEZONE="Asia/Kolkata"
6+
7+
# Function to update and upgrade the system
8+
update_system() {
9+
echo "Updating and upgrading the system..."
10+
sudo apt-get update && sudo apt-get upgrade -y
11+
}
12+
13+
# Function to install basic packages
14+
install_basic_packages() {
15+
echo "Installing basic packages..."
16+
sudo apt-get install -y curl wget git vim ufw htop unzip nano
17+
}
18+
19+
# Function to set the timezone
20+
set_timezone() {
21+
echo "Setting timezone to $TIMEZONE..."
22+
sudo timedatectl set-timezone $TIMEZONE
23+
}
24+
25+
# Function to add a new user
26+
add_new_user() {
27+
echo "Adding new user $NEW_USER..."
28+
sudo adduser --gecos "" $NEW_USER
29+
sudo usermod -aG sudo $NEW_USER
30+
echo "$NEW_USER ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$NEW_USER
31+
echo "User $NEW_USER has been created and added to the sudo group."
32+
}
33+
34+
# Main function to run all setup steps
35+
main() {
36+
update_system
37+
install_basic_packages
38+
set_timezone
39+
# add_new_user
40+
}
41+
42+
echo "Server setup is complete!"
43+
# Run the main function
44+
main

setup/setup_gh_ssh.sh

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Function to check if a command is available
7+
command_exists() {
8+
command -v "$1" >/dev/null 2>&1
9+
}
10+
11+
# Check if ssh-keygen is available
12+
if ! command_exists ssh-keygen; then
13+
echo "ssh-keygen is not installed. Installing..."
14+
sudo apt-get update
15+
sudo apt-get install -y openssh-client
16+
fi
17+
18+
# Check if ssh-agent is running
19+
if ! pgrep -u "$USER" ssh-agent >/dev/null; then
20+
echo "Starting ssh-agent..."
21+
eval "$(ssh-agent -s)"
22+
fi
23+
24+
# Function to generate SSH key for an account
25+
generate_ssh_key() {
26+
local email=$1
27+
local key_name=$2
28+
local key_file="$HOME/.ssh/id_rsa_$key_name"
29+
30+
echo "Generating SSH key for $email..."
31+
ssh-keygen -t rsa -b 4096 -C "$email" -f "$key_file" -N ""
32+
33+
# Set permissions for private and public keys
34+
chmod 600 "$key_file"
35+
chmod 644 "${key_file}.pub"
36+
37+
ssh-add "$key_file"
38+
echo "SSH key generated and added to the agent for $email."
39+
}
40+
41+
# Add SSH keys for multiple accounts
42+
while true; do
43+
read -p "Enter your GitHub email address (or press Enter to finish): " email
44+
if [ -z "$email" ]; then
45+
break
46+
fi
47+
48+
# Extract the part before the @ symbol
49+
key_name=$(echo "$email" | cut -d '@' -f 1)
50+
generate_ssh_key "$email" "$key_name"
51+
52+
echo "Your SSH public key for $email is:"
53+
cat "$HOME/.ssh/id_rsa_$key_name.pub"
54+
55+
read -p "Open GitHub SSH keys settings page in your default browser? (y/n): " open_browser
56+
if [[ $open_browser == "y" || $open_browser == "Y" ]]; then
57+
xdg-open "https://github.com/settings/keys"
58+
fi
59+
60+
echo "Please add the above SSH public key to your GitHub account."
61+
done
62+
63+
# Create or update SSH config file for multiple accounts
64+
echo "Creating/updating SSH config file..."
65+
ssh_config="$HOME/.ssh/config"
66+
touch "$ssh_config"
67+
chmod 600 "$ssh_config"
68+
69+
for key_file in ~/.ssh/id_rsa_*; do
70+
if [[ -f "$key_file" && "$key_file" != ~/.ssh/id_rsa ]]; then
71+
key_name=$(basename "$key_file" | sed 's/id_rsa_//')
72+
echo "Host github-$key_name" >> "$ssh_config"
73+
echo " HostName github.com" >> "$ssh_config"
74+
echo " User git" >> "$ssh_config"
75+
echo " IdentityFile $key_file" >> "$ssh_config"
76+
echo "" >> "$ssh_config"
77+
fi
78+
done
79+
80+
echo "SSH config file created/updated."
81+
82+
echo "SSH key setup script completed."

setup/setup_vscode.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Update package lists
7+
echo "Updating package lists..."
8+
sudo apt-get update
9+
10+
# Install prerequisites
11+
echo "Installing prerequisites..."
12+
sudo apt-get install -y wget gpg
13+
14+
# Add Microsoft’s GPG key
15+
echo "Adding Microsoft GPG key..."
16+
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor -o /usr/share/keyrings/microsoft-archive-keyring.gpg
17+
18+
# Add VS Code repository
19+
echo "Adding VS Code repository..."
20+
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/repos/vscode stable main" | sudo tee /etc/apt/sources.list.d/vscode.list
21+
22+
# Update package lists again with the new repository
23+
echo "Updating package lists with VS Code repository..."
24+
sudo apt-get update
25+
26+
# Install VS Code
27+
echo "Installing VS Code..."
28+
sudo apt-get install -y code
29+
30+
# Verify installation
31+
echo "Verifying VS Code installation..."
32+
code --version
33+
34+
echo "Visual Studio Code installation completed."

table_generator.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# Directory to start the search
4+
SEARCH_DIR="." # Current directory
5+
6+
# Output file for the Markdown table
7+
OUTPUT_FILE="SCRIPT_TABLE.md"
8+
9+
# Create or clear the output file
10+
> "$OUTPUT_FILE"
11+
12+
# Print the table header
13+
echo "| Script | Description | Status |" >> "$OUTPUT_FILE"
14+
echo "| --- | --- | --- |" >> "$OUTPUT_FILE"
15+
16+
# Find all .sh files in the specified directory and its subdirectories
17+
find "$SEARCH_DIR" -type f -name "*.sh" | while read -r script; do
18+
if [[ -f "$script" ]]; then
19+
script_name=$(basename "$script")
20+
script_path=$(realpath --relative-to="$(pwd)" "$script")
21+
description="Description not set"
22+
status="Tested"
23+
24+
# Check for a description in the script (e.g., comments at the top)
25+
if grep -q "^# Description:" "$script"; then
26+
description=$(grep "^# Description:" "$script" | sed 's/^# Description: //')
27+
fi
28+
29+
# Check for a status in the script (e.g., comments at the top)
30+
if grep -q "^# Status:" "$script"; then
31+
status=$(grep "^# Status:" "$script" | sed 's/^# Status: //')
32+
fi
33+
34+
# Append the script information to the table
35+
echo "| [${script_name}](${script_path}) | ${description} | ${status} |" >> "$OUTPUT_FILE"
36+
fi
37+
done
38+
39+
echo "Markdown table generated in $OUTPUT_FILE."

0 commit comments

Comments
 (0)