-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from stefopl/noobs-190
fix #190 chce_ssh_config.sh - uzupełnienie danych
- Loading branch information
Showing
1 changed file
with
83 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
#!/bin/bash | ||
# Create ssh config for easy connection with mikr.us | ||
# Autor: Radoslaw Karasinski | ||
# Autor: Radoslaw Karasinski, Artur 'stefopl' Stefanski | ||
# Usage: you can pass following arguments: | ||
# --mikrus MIKRUS_NAME (i.e. 'X123') | ||
# --user USERNAME (i.e. 'root') | ||
# --port PORT_NUMBER (to configure connection for non mikr.us host) | ||
# --host HOSTNAME (to configure connection for non mikr.us host) | ||
# [email protected] (without param in front) | ||
|
||
# some bash magic: https://brianchildress.co/named-parameters-in-bash/ | ||
while [ $# -gt 0 ]; do | ||
if [[ $1 == *"--"* ]]; then | ||
|
@@ -35,24 +34,79 @@ fi | |
port="${port:-22}" | ||
user="${user:-root}" | ||
|
||
declare -A hosts=( | ||
["e"]="srv07" | ||
["f"]="srv08" | ||
["g"]="srv09" | ||
["h"]="srv10" | ||
["i"]="srv11" | ||
["j"]="srv12" | ||
["k"]="srv14" | ||
["l"]="srv15" | ||
["m"]="srv16" | ||
["n"]="srv17" | ||
["o"]="srv18" | ||
["p"]="srv19" | ||
["r"]="srv20" | ||
["s"]="srv21" | ||
["t"]="srv22" | ||
["u"]="srv23" | ||
["w"]="srv24" | ||
["z"]="srv25" | ||
["x"]="maluch" | ||
["y"]="maluch2" | ||
["v"]="maluch3" | ||
["a"]="srv26" | ||
["b"]="srv27" | ||
["c"]="srv29" | ||
["d"]="srv28" | ||
) | ||
|
||
if [ -n "$mikrus" ]; then | ||
if ! [[ "$mikrus" =~ [a-q][0-9]{3}$ ]]; then | ||
if ! [[ "$mikrus" =~ [a-z][0-9]{3}$ ]]; then | ||
echo "ERROR: --mikrus parameter is not valid!" | ||
exit 3 | ||
fi | ||
|
||
port="$(( 10000 + $(echo $mikrus | grep -o '[0-9]\+') ))" | ||
|
||
key="$(echo $mikrus | grep -o '[^0-9]\+' )" | ||
declare -A hosts | ||
hosts["a"]="srv03" | ||
hosts["b"]="srv04" | ||
hosts["e"]="srv07" | ||
hosts["f"]="srv08" | ||
hosts["g"]="srv09" | ||
hosts["h"]="srv10" | ||
hosts["q"]="mini01" | ||
hosts["x"]="maluch" | ||
|
||
url="https://mikr.us/serwery.txt" | ||
|
||
servers="" | ||
|
||
if command -v curl &> /dev/null; then | ||
http_code=$(curl -s -o /dev/null -w "%{http_code}" "$url") | ||
content_type=$(curl -sI "$url" | grep -i "Content-Type" | cut -d ' ' -f2) | ||
|
||
if [ "$http_code" == "200" ] && [[ "$content_type" == "text/"* ]]; then | ||
servers=$(curl -s "$url") | ||
fi | ||
elif command -v wget &> /dev/null; then | ||
http_code=$(wget --spider --server-response "$url" 2>&1 | grep "HTTP/" | awk '{print $2}') | ||
content_type=$(wget --spider --server-response "$url" 2>&1 | grep -i "Content-Type" | awk '{print $2}') | ||
|
||
if [ "$http_code" == "200" ] && [[ "$content_type" == "text/"* ]]; then | ||
servers=$(wget -q -O - "$url") | ||
fi | ||
else | ||
echo "ERROR: Neither 'curl' nor 'wget' were found." | ||
exit 1 | ||
fi | ||
|
||
if [ -n "$servers" ]; then | ||
unset hosts | ||
declare -A hosts | ||
while IFS='=' read -r server value; do | ||
hosts["$server"]="$value" | ||
done <<< "$servers" | ||
else | ||
echo "Failed to download server list. Using hardcoded list." | ||
fi | ||
|
||
echo $hosts | ||
|
||
host="${hosts[$key]}" | ||
if [ -z "$host" ]; then | ||
echo "ERROR: Server hostname not known for key '$key'." | ||
|
@@ -66,10 +120,15 @@ if [ -n "$possible_ssh_param" ]; then | |
host="${possible_ssh_param#*@}" | ||
fi | ||
|
||
|
||
|
||
if [ -z "$host" ]; then | ||
echo "ERROR: Host was not recognized by any known method (--mikrus or --host or by specifying [email protected])." | ||
echo "" | ||
echo "Usage: you can pass following arguments:" | ||
echo " --mikrus MIKRUS_NAME (i.e. 'X123')" | ||
echo " --user USERNAME (i.e. 'root')" | ||
echo " --port PORT_NUMBER (to configure connection for non mikr.us host)" | ||
echo " --host HOSTNAME (to configure connection for non mikr.us host)" | ||
echo " [email protected] (without param in front)" | ||
exit 5 | ||
fi | ||
|
||
|
@@ -81,11 +140,16 @@ if [ -n "$decision" ]; then | |
exit 0 | ||
fi | ||
|
||
if [ -n "$mikrus" ]; then | ||
ssh_key_file="$HOME/.ssh/mikrus-$mikrus-$user-$host-port-$port-rsa" | ||
header="mikrus-$mikrus-$user-$host-$port" | ||
else | ||
ssh_key_file="$HOME/.ssh/$user-$host-port-$port-rsa" | ||
header="$user-$host-$port" | ||
fi | ||
|
||
ssh_key_file="$HOME/.ssh/$user-$host-port-$port-rsa" | ||
ssh-keygen -t rsa -b 4096 -f "$ssh_key_file" -C "$user@$host:$port" | ||
ssh-keygen -t rsa -b 4096 -f "$ssh_key_file" -C "$USER@$HOSTNAME" | ||
|
||
header="$user-$host-$port" | ||
touch ~/.ssh/config # just in case if file was not created in past | ||
if ! grep -q "$header" ~/.ssh/config ; then | ||
echo "" >> ~/.ssh/config | ||
|
@@ -99,8 +163,8 @@ else | |
exit 6 | ||
fi | ||
|
||
ssh-copy-id -i $ssh_key_file $header | ||
ssh-copy-id -i "$ssh_key_file" $header | ||
|
||
echo "" | ||
echo "ssh was properly configured!" | ||
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." | ||
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." |