privateone commited on
Commit
25d4f1e
·
verified ·
1 Parent(s): cbc337a

Create start.sh

Browse files
Files changed (1) hide show
  1. start.sh +181 -0
start.sh ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Function to print the current hostname and user details
4
+ print_host_details() {
5
+ #echo "0.0.0.0 abc" | sudo tee -a /etc/hosts
6
+ echo "* The hostname of this container is: $(cat /etc/hostname)"
7
+ echo "* The host of this container is: $(cat /etc/hosts)"
8
+ echo "* The Sudoers of this container is: $(cat /etc/sudoers)"
9
+ echo "* ID of the user running the script:($whoami) *ID : $(id -u) * Group: $(id -g)"
10
+ echo "* Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"
11
+ }
12
+
13
+ # Function to generate SSH host keys if missing
14
+ generate_ssh_keys() {
15
+ if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
16
+ echo "* Generating SSH host keys *"
17
+
18
+ yes y | ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
19
+ yes y | ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
20
+ yes y | ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
21
+ #ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ""
22
+ #ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N ""
23
+ #ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
24
+ fi
25
+ }
26
+
27
+ # Function to start SSH service if not running
28
+ start_ssh_service() {
29
+ if ! pgrep -x "sshd" >/dev/null; then
30
+ echo "* Starting SSH server on port 2222 *"
31
+ /usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
32
+ #/usr/sbin/sshd -D || { echo "Failed to start SSH server"; exit 1; }
33
+ else
34
+ echo "* SSH server is already running *"
35
+ fi
36
+ }
37
+
38
+ # Function to create 'admin' user if missing
39
+ create_admin_user() {
40
+ echo "* Creating 'admin' user *"
41
+ useradd -m admin || { echo "Failed to create admin user"; exit 1; }
42
+ echo "admin:password" | chpasswd || { echo "Failed to set admin password"; exit 1; }
43
+ #echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers || { echo "Failed to update sudoers"; exit 1; }
44
+ }
45
+
46
+ # Function to fix permissions for 'admin' user
47
+ fix_admin_permissions() {
48
+ echo "* Fixing permissions for 'admin' user *"
49
+ #chown -R admin:admin /home/admin || { echo "Failed to change ownership"; exit 1; }
50
+ #chmod -R 777 /home/admin || { echo "Failed to set permissions"; exit 1; }
51
+ }
52
+
53
+ # Function to generate SSH keys for 'admin' user if missing
54
+ generate_admin_ssh_keys() {
55
+ if [ ! -f /home/admin/.ssh/id_rsa ]; then
56
+ echo "* Generating SSH keys for 'admin' user *"
57
+ mkdir -p /home/admin/.ssh || { echo "Failed to create .ssh directory"; exit 1; }
58
+ #ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N ""
59
+ #yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
60
+ yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N "password"
61
+ ssh-keyscan -p 2222 0.0.0.0 >> /home/admin/.ssh/known_hosts
62
+ cp /home/admin/.ssh/known_hosts /home/admin/.ssh/ssh_known_hosts
63
+ #chown -R admin:admin /home/admin/.ssh
64
+ chmod -R 777 /home/admin/.ssh
65
+ #chmod 700 /home/admin/.ssh || { echo "Failed to set .ssh directory permissions"; exit 1; }
66
+ #chmod 600 /home/admin/.ssh/id_rsa || { echo "Failed to set private key permissions"; exit 1; }
67
+ #chmod 644 /home/admin/.ssh/id_rsa.pub || { echo "Failed to set public key permissions"; exit 1; }
68
+ fi
69
+ }
70
+
71
+ # Function to add public key to authorized_keys for 'admin' user
72
+ add_admin_authorized_keys() {
73
+ echo "* Adding public key to authorized_keys for 'admin' user *"
74
+ echo "* Public Key :$(cat /home/admin/.ssh/id_rsa.pub)"
75
+ cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys || { echo "Failed to add public key to authorized_keys"; exit 1; }
76
+ echo "* Authorised Keys: $(cat /home/admin/.ssh/authorized_keys)"
77
+ echo "* Known HOST File :$(cat /home/admin/.ssh/known_hosts)"
78
+ #chmod 600 /home/admin/.ssh/authorized_keys || { echo "Failed to set authorized_keys permissions"; exit 1; }
79
+ echo "password" | su -p "admin"
80
+ whoami
81
+ }
82
+
83
+ # Function to add SSH private key to the SSH agent
84
+ add_ssh_key_to_agent() {
85
+ ssh-add -D # Remove all existing keys from the SSH agent
86
+ if ! ssh-add -l | grep -q '/home/admin/.ssh/id_rsa'; then
87
+ echo "* Adding SSH private key to the SSH agent *"
88
+ ssh-add /home/admin/.ssh/id_rsa
89
+ fi
90
+ }
91
+
92
+ # Function to activate virtual environment
93
+ activate_virtual_env() {
94
+ if [ -d "/app/WebSSHEnv" ]; then
95
+ echo "* Activating virtual environment *"
96
+ source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
97
+ else
98
+ echo "* Virtual environment not found, please check setup *"
99
+ fi
100
+ }
101
+
102
+ # Function to set working directory
103
+ set_working_directory() {
104
+ cd /app || { echo "Failed to change directory to /app"; exit 1; }
105
+ }
106
+
107
+ # Function to print the contents of the working directory
108
+ print_working_directory_contents() {
109
+ echo "* Contents of /app directory: *"
110
+ ls -la /app
111
+ }
112
+
113
+ # Function to run the WebSSH application
114
+ run_webssh_application() {
115
+ echo "* Starting WebSSH application *"
116
+ python3 -u -m WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
117
+ }
118
+
119
+ # Function to keep the container running
120
+ keep_container_running() {
121
+ tail -f /var/log/auth.log
122
+ }
123
+
124
+ # Main script execution
125
+ print_host_details
126
+ start_ssh_service
127
+ generate_ssh_keys
128
+ #create_admin_user
129
+ fix_admin_permissions
130
+ generate_admin_ssh_keys
131
+ add_admin_authorized_keys
132
+
133
+ #lsl -l /home/admin/.ssh/
134
+
135
+ if [ $? -eq 0 ]; then
136
+ echo "* Admin credentials are valid."
137
+ else
138
+ echo "* Admin login failed! Check the password for 'admin' user." >&2
139
+ fi
140
+
141
+ # Ensure SSH agent is running
142
+ #echo "* Starting SSH agent *"
143
+ #eval $(ssh-agent -s) || { echo "Failed to start SSH agent"; exit 1; }
144
+
145
+ # Ensure SSHD config is correctly set up
146
+ #echo "* Configuring SSHD *"
147
+ #echo "UseKeychain yes" >> /home/admin/.ssh/config
148
+ #echo "AddKeysToAgent yes" >> /home/admin/.ssh/config
149
+
150
+ # Restart SSH service
151
+ #service ssh restart || { echo "Failed to restart SSH service"; exit 1; }
152
+
153
+
154
+ #add_ssh_key_to_agent
155
+
156
+ echo "* Contents of id_rsa of Admin:"
157
+ cat /home/admin/.ssh/id_rsa
158
+ echo "* Contents of config of Admin:"
159
+ cat /home/admin/.ssh/config
160
+ echo "* Contents of known hosts of Admin:"
161
+ cat /app/ssh/ssh_known_hosts
162
+ echo "* Contents of /.ssh of Admin:"
163
+ chmod -R 777 /home/admin/.ssh/
164
+ ls -l /home/admin/.ssh/
165
+
166
+ #sshd -T | grep -i 'known hosts'
167
+ echo "* Status of SSH service: *"
168
+ netstat -tuln
169
+
170
+ echo "* Testing admin's SSH login locally *"
171
+ sshpass -p "password" ssh -v -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@0.0.0.0 -p 2222 exit
172
+ echo "* Testing admin's SSH login locally Method 2 *"
173
+ #ssh -v -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@r-privateone-ubuntu-sypaevhc-acfbc-8nywf -p 2222
174
+
175
+ echo "* Testing admin's SSH login locally Method 3 $(ssh -fnNT -R 0.0.0.0:2222:0.0.0.0:7860 remote-host )*"
176
+
177
+ activate_virtual_env
178
+ set_working_directory
179
+ print_working_directory_contents
180
+ run_webssh_application
181
+ keep_container_running