11#! /bin/bash
22# Create ssh config for easy connection with mikr.us
3- # Autor: Radoslaw Karasinski
3+ # Autor: Radoslaw Karasinski, Artur 'stefopl' Stefanski
44# Usage: you can pass following arguments:
55# --mikrus MIKRUS_NAME (i.e. 'X123')
66# --user USERNAME (i.e. 'root')
77# --port PORT_NUMBER (to configure connection for non mikr.us host)
88# --host HOSTNAME (to configure connection for non mikr.us host)
99# [email protected] (without param in front)10-
1110# some bash magic: https://brianchildress.co/named-parameters-in-bash/
1211while [ $# -gt 0 ]; do
1312 if [[ $1 == * " --" * ]]; then
3534port=" ${port:- 22} "
3635user=" ${user:- root} "
3736
37+ declare -A hosts=(
38+ [" e" ]=" srv07"
39+ [" f" ]=" srv08"
40+ [" g" ]=" srv09"
41+ [" h" ]=" srv10"
42+ [" i" ]=" srv11"
43+ [" j" ]=" srv12"
44+ [" k" ]=" srv14"
45+ [" l" ]=" srv15"
46+ [" m" ]=" srv16"
47+ [" n" ]=" srv17"
48+ [" o" ]=" srv18"
49+ [" p" ]=" srv19"
50+ [" r" ]=" srv20"
51+ [" s" ]=" srv21"
52+ [" t" ]=" srv22"
53+ [" u" ]=" srv23"
54+ [" w" ]=" srv24"
55+ [" z" ]=" srv25"
56+ [" x" ]=" maluch"
57+ [" y" ]=" maluch2"
58+ [" v" ]=" maluch3"
59+ [" a" ]=" srv26"
60+ [" b" ]=" srv27"
61+ [" c" ]=" srv29"
62+ [" d" ]=" srv28"
63+ )
64+
3865if [ -n " $mikrus " ]; then
39- if ! [[ " $mikrus " =~ [a-q ][0-9]{3}$ ]]; then
66+ if ! [[ " $mikrus " =~ [a-z ][0-9]{3}$ ]]; then
4067 echo " ERROR: --mikrus parameter is not valid!"
4168 exit 3
4269 fi
4370
4471 port=" $(( 10000 + $(echo $mikrus | grep - o '[0 - 9 ]\+ ') )) "
4572
4673 key=" $( echo $mikrus | grep -o ' [^0-9]\+' ) "
47- declare -A hosts
48- hosts[" a" ]=" srv03"
49- hosts[" b" ]=" srv04"
50- hosts[" e" ]=" srv07"
51- hosts[" f" ]=" srv08"
52- hosts[" g" ]=" srv09"
53- hosts[" h" ]=" srv10"
54- hosts[" q" ]=" mini01"
55- hosts[" x" ]=" maluch"
74+
75+ url=" https://mikr.us/serwery.txt"
76+
77+ servers=" "
78+
79+ if command -v curl & > /dev/null; then
80+ http_code=$( curl -s -o /dev/null -w " %{http_code}" " $url " )
81+ content_type=$( curl -sI " $url " | grep -i " Content-Type" | cut -d ' ' -f2)
82+
83+ if [ " $http_code " == " 200" ] && [[ " $content_type " == " text/" * ]]; then
84+ servers=$( curl -s " $url " )
85+ fi
86+ elif command -v wget & > /dev/null; then
87+ http_code=$( wget --spider --server-response " $url " 2>&1 | grep " HTTP/" | awk ' {print $2}' )
88+ content_type=$( wget --spider --server-response " $url " 2>&1 | grep -i " Content-Type" | awk ' {print $2}' )
89+
90+ if [ " $http_code " == " 200" ] && [[ " $content_type " == " text/" * ]]; then
91+ servers=$( wget -q -O - " $url " )
92+ fi
93+ else
94+ echo " ERROR: Neither 'curl' nor 'wget' were found."
95+ exit 1
96+ fi
97+
98+ if [ -n " $servers " ]; then
99+ unset hosts
100+ declare -A hosts
101+ while IFS=' =' read -r server value; do
102+ hosts[" $server " ]=" $value "
103+ done <<< " $servers"
104+ else
105+ echo " Failed to download server list. Using hardcoded list."
106+ fi
107+
108+ echo $hosts
109+
56110 host=" ${hosts[$key]} "
57111 if [ -z " $host " ]; then
58112 echo " ERROR: Server hostname not known for key '$key '."
@@ -66,10 +120,15 @@ if [ -n "$possible_ssh_param" ]; then
66120 host=" ${possible_ssh_param#*@ } "
67121fi
68122
69-
70-
71123if [ -z " $host " ]; then
72124 echo " ERROR: Host was not recognized by any known method (--mikrus or --host or by specifying [email protected] )." 125+ echo " "
126+ echo " Usage: you can pass following arguments:"
127+ echo " --mikrus MIKRUS_NAME (i.e. 'X123')"
128+ echo " --user USERNAME (i.e. 'root')"
129+ echo " --port PORT_NUMBER (to configure connection for non mikr.us host)"
130+ echo " --host HOSTNAME (to configure connection for non mikr.us host)"
131+ echo " [email protected] (without param in front)" 73132 exit 5
74133fi
75134
@@ -81,11 +140,16 @@ if [ -n "$decision" ]; then
81140 exit 0
82141fi
83142
143+ if [ -n " $mikrus " ]; then
144+ ssh_key_file=" $HOME /.ssh/mikrus-$mikrus -$user -$host -port-$port -rsa"
145+ header=" mikrus-$mikrus -$user -$host -$port "
146+ else
147+ ssh_key_file=" $HOME /.ssh/$user -$host -port-$port -rsa"
148+ header=" $user -$host -$port "
149+ fi
84150
85- ssh_key_file=" $HOME /.ssh/$user -$host -port-$port -rsa"
86- ssh-keygen -t rsa -b 4096 -f " $ssh_key_file " -C " $user @$host :$port "
151+ ssh-keygen -t rsa -b 4096 -f " $ssh_key_file " -C " $USER @$HOSTNAME "
87152
88- header=" $user -$host -$port "
89153touch ~ /.ssh/config # just in case if file was not created in past
90154if ! grep -q " $header " ~ /.ssh/config ; then
91155 echo " " >> ~ /.ssh/config
99163 exit 6
100164fi
101165
102- ssh-copy-id -i $ssh_key_file $header
166+ ssh-copy-id -i " $ssh_key_file " $header
103167
104168echo " "
105169echo " ssh was properly configured!"
106- echo " Remmber , that you can use tab to use autofill to type connection string faster - type few first chars of Host (i.e. 'ssh ${header: 0: 8} ', or even less) , then press tab."
170+ echo " Remember , that you can use tab to use autofill to type connection string faster - type few first chars of Host (i.e. 'ssh ${header: 0: 8} ', or even less), then press tab."
0 commit comments