File size: 3,559 Bytes
81f5c26
 
 
 
 
 
 
 
d23feb0
31103ff
81f5c26
 
 
31103ff
 
10ae734
f0e44d4
 
 
 
d64ca78
10ae734
 
 
 
 
f0e44d4
 
10ae734
 
 
d64ca78
 
 
 
 
 
10ae734
31103ff
81f5c26
 
 
31103ff
7ff0d99
d23feb0
81f5c26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdba4f7
81f5c26
 
 
 
 
 
ff06411
d23feb0
81f5c26
 
 
 
 
 
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
#!/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 "toor" || 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() {
  
    rm -rf /etc/ssh/ssh_host_rsa_* 
    rm -rf /etc/ssh/ssh_host_ecdsa_*
    rm -rf /etc/ssh/ssh_host_ed25519_* 
    rm -rf /etc/ssh/ssh_known_* 
    
           
    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-keyscan -p 2222 127.0.0.1 >> /etc/ssh/ssh_known_hosts
    cp /etc/ssh/ssh_known_hosts /etc/ssh/known_host
    #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 "" 
    echo " Public Key:"
    cat /etc/ssh/ssh_host_rsa_key.pub
    echo " Private Key :"
    cat /etc/ssh/ssh_host_rsa_key
    echo " Host Files :"
    cat /etc/ssh/ssh_known_host
 
}
# Function to start SSH service if not running
start_ssh_service() {
    if ! pgrep -x "sshd" >/dev/null; then
        echo "* Starting SSH server at port 2222 *"
        /usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
        #/usr/sbin/sshd -D -e -ddd|| { echo "Failed to start SSH server"; exit 1; }
    else
        echo "* SSH server is already running *"
    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
generate_ssh_keys 
start_ssh_service
#sshd -T | grep -i 'known hosts'
echo "* Status of SSH service: *"
netstat -tuln 

echo "* Testing admin's SSH login locally *"
echo "password" | ssh -v -o /etc/ssh/ssh_host_rsa_key.pub admin@localhost -p 2222 


activate_virtual_env
set_working_directory
print_working_directory_contents
run_webssh_application
keep_container_running