Spaces:
Paused
Paused
File size: 7,116 Bytes
1a4023b d89b239 19a162c 158758c 19a162c 158758c 7381827 6059389 19a162c 4f8a8a2 f53692d 4f8a8a2 19a162c a647ec5 19a162c 3db2a68 19a162c 476832a 19a162c 386dc79 f8c35b0 19a162c 157b02d 65715ec b93344d 00f1f49 b84ea66 00f1f49 19a162c 21e483f b84ea66 00f1f49 b84ea66 00f1f49 f19db59 19a162c 96d7379 d8eca31 96d7379 d8eca31 96d7379 6059389 19a162c 4f8a8a2 19a162c 7a26acc 19a162c ecee644 157b02d 19a162c f67cf0f e0755ec a5425b8 f67cf0f 6059389 f4b272b 6059389 e38f7d2 6059389 4aeec3c a5425b8 6059389 e38f7d2 d137eed db49676 157b02d c6ecbc2 cdc4366 c6ecbc2 e0755ec 75785bd 5334b6e d137eed 45c2409 1cf7ad2 45c2409 d137eed 1cf7ad2 f42bd53 19a162c 6059389 |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
#!/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)"
echo "* The host 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 "* 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 "PASSWORD" || { echo "Failed to generate RSA key"; exit 1; }
yes y | ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "PASSWORD" || { echo "Failed to generate ECDSA key"; exit 1; }
yes y | ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "PASSWORD" || { 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/admin || { 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
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
#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 -p "admin"
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 "* Contents of /.ssh of Admin:"
chmod -R 777 /home/admin/.ssh/
ls -l /home/admin/.ssh/
#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 exit
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
|