Ubuntu / start_v5.sh
privateone's picture
Rename start.sh to start_v5.sh
4a0ce83 verified
#!/bin/bash
# Function to print the current hostname and user details
print_host_details() {
#echo "0.0.0.0 abc" | sudo tee -a /etc/hosts
echo "* The hostname of this container is: $(cat /etc/hostname). PWD $(pwd)"
echo "* The host File of this container is: $(cat /etc/hosts)"
echo "* The Sudoers of this container is: $(cat /etc/sudoers)"
echo "* ID of the user running the script:($whoami) *ID : $(id -u) * Group: $(id -g)"
echo "* Changing User to Admin :$(echo "password" || su - admin)"
echo "* Current User WHO AM I $(whoami)"
echo "* Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"
}
# Function to generate SSH host keys if missing
generate_ssh_keys() {
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
echo "* Generating SSH host keys *"
yes y | ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
yes y | ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
yes y | ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
#ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ""
#ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N ""
#ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
fi
}
# Function to start SSH service if not running
start_ssh_service() {
if ! pgrep -x "sshd" >/dev/null; then
echo "* Starting SSH server on port 2222 *"
/usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
#/usr/sbin/sshd -D || { echo "Failed to start SSH server"; exit 1; }
else
echo "* SSH server is already running *"
fi
}
# Function to create 'admin' user if missing
create_admin_user() {
echo "* Creating 'admin' user *"
#useradd -m admin || { echo "Failed to create admin user"; exit 1; }
echo "admin:password" | chpasswd || { echo "Failed to set admin password"; exit 1; }
#echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers || { echo "Failed to update sudoers"; exit 1; }
}
# Function to fix permissions for 'admin' user
fix_admin_permissions() {
echo "* Fixing permissions for 'admin' user *"
#chown -R admin:admin /home/admin || { echo "Failed to change ownership"; exit 1; }
chmod -R 777 /home/* || { echo "Failed to set permissions"; exit 1; }
}
# Function to generate SSH keys for 'admin' user if missing
generate_admin_ssh_keys() {
if [ ! -f /home/admin/.ssh/id_rsa ]; then
echo "* Generating SSH keys for 'admin' user *"
mkdir -p /home/admin/.ssh || { echo "Failed to create .ssh directory"; exit 1; }
#ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N ""
#yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N ""
ssh-keyscan -p 2222 0.0.0.0 >> /home/admin/.ssh/known_hosts
cp /home/admin/.ssh/known_hosts /home/admin/.ssh/ssh_known_hosts
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 admin@localhost
#chown -R admin:admin /home/admin/.ssh
chmod -R 777 /home/admin/.ssh
#chmod 700 /home/admin/.ssh || { echo "Failed to set .ssh directory permissions"; exit 1; }
#chmod 600 /home/admin/.ssh/id_rsa || { echo "Failed to set private key permissions"; exit 1; }
#chmod 644 /home/admin/.ssh/id_rsa.pub || { echo "Failed to set public key permissions"; exit 1; }
fi
}
# Function to add public key to authorized_keys for 'admin' user
add_admin_authorized_keys() {
echo "* Adding public key to authorized_keys for 'admin' user *"
echo "* Public Key :$(cat /home/admin/.ssh/id_rsa.pub)"
cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys || { echo "Failed to add public key to authorized_keys"; exit 1; }
echo "* Authorised Keys: $(cat /home/admin/.ssh/authorized_keys)"
echo "* Known HOST File :$(cat /home/admin/.ssh/known_hosts)"
#chmod 600 /home/admin/.ssh/authorized_keys || { echo "Failed to set authorized_keys permissions"; exit 1; }
#echo "password" || su - admin
#echo "I am $(whoami)"
}
# Function to add SSH private key to the SSH agent
add_ssh_key_to_agent() {
ssh-add -D # Remove all existing keys from the SSH agent
if ! ssh-add -l | grep -q '/home/admin/.ssh/id_rsa'; then
echo "* Adding SSH private key to the SSH agent *"
ssh-add /home/admin/.ssh/id_rsa
fi
}
# Function to activate virtual environment
activate_virtual_env() {
if [ -d "/app/WebSSHEnv" ]; then
echo "* Activating virtual environment *"
source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
else
echo "* Virtual environment not found, please check setup *"
fi
}
# Function to set working directory
set_working_directory() {
cd /app || { echo "Failed to change directory to /app"; exit 1; }
}
# Function to print the contents of the working directory
print_working_directory_contents() {
echo "* Contents of /app directory: *"
ls -la /app
}
# Function to run the WebSSH application
run_webssh_application() {
echo "* Starting WebSSH application *"
python3 -u -m WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
}
# Function to keep the container running
keep_container_running() {
tail -f /var/log/auth.log
}
# Main script execution
print_host_details
start_ssh_service
generate_ssh_keys
#create_admin_user
fix_admin_permissions
generate_admin_ssh_keys
add_admin_authorized_keys
#lsl -l /home/admin/.ssh/
if [ $? -eq 0 ]; then
echo "* Admin credentials are valid."
else
echo "* Admin login failed! Check the password for 'admin' user." >&2
fi
# Ensure SSH agent is running
#echo "* Starting SSH agent *"
#eval $(ssh-agent -s) || { echo "Failed to start SSH agent"; exit 1; }
# Ensure SSHD config is correctly set up
#echo "* Configuring SSHD *"
#echo "UseKeychain yes" >> /home/admin/.ssh/config
#echo "AddKeysToAgent yes" >> /home/admin/.ssh/config
# Restart SSH service
#service ssh restart || { echo "Failed to restart SSH service"; exit 1; }
#add_ssh_key_to_agent
echo "* Contents of id_rsa of Admin:"
cat /home/admin/.ssh/id_rsa
echo "* Contents of config of Admin:"
cat /home/admin/.ssh/config
echo "* Contents of known hosts of Admin:"
cat /app/ssh/ssh_known_hosts
echo "* Setting Permissions /home/admin/.ssh/ -777 :$(chmod -R 777 /home/admin/.ssh/)"
echo "* Contents of /home/admin/.ssh/ : $(ls -la /home/admin/.ssh/)"
echo "* Contents of .ssh/ of Admin: $(ls -la .ssh/)"
echo "* Contents of Home: $(ls -la /home/)"
#sshd -T | grep -i 'known hosts'
echo "* Status of SSH service: *"
netstat -tuln
echo "* Testing admin's SSH login locally *"
sshpass -p "password" ssh -v -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@0.0.0.0 -p 2222
echo "* Testing admin's SSH login locally Method 2 *"
#ssh -v -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@r-privateone-ubuntu-sypaevhc-acfbc-8nywf -p 2222
#echo "* Testing admin's SSH login locally Method 3 $(ssh -fnNT -R 0.0.0.0:2222:0.0.0.0:7860 remote-host )*"
activate_virtual_env
set_working_directory
print_working_directory_contents
run_webssh_application
keep_container_running