First functional seq for ssh management

- Installing, Authorization key generation, Authorization key sending, Sending command list

Keys are generated without user input (4096,rsa,comment: hostname,no passphrase,default path)
This commit is contained in:
2019-11-21 23:43:15 +01:00
parent c7de26455b
commit aaec21ae93

View File

@@ -1,28 +1,54 @@
#!/bin/bash #!/bin/bash
toolName="ssh" toolName="ssh"
toolIdentity="~/.ssh/id_rsa" toolIdentity=~/.ssh/id_rsa
aList="" aList=""
aHost="" aHost=""
# default ssh port # default ssh port
aPort="22" aPort="22"
step_1_info() { echo "Install $toolName"; }
step_1_alias() { ALIAS="install"; }
step_1() {
exe apt update
exe apt install ssh
}
step_3_info() { echo "Create $toolName authentication keys"; } step_3_info() { echo "Create $toolName authentication keys"; }
step_3_alias() { ALIAS="create"; } step_3_alias() { ALIAS="create"; }
step_3() { step_3() {
exep "ssh-keygen -l -f $toolIdentity 2>>/dev/null" exep "ssh-keygen -l -f $toolIdentity 2>>/dev/null"
if [ $? -ne 0 ]; then if [ $? -eq 0 ]; then
saveReturn 1 echo " [I] Using key found at $toolIdentity."
else return 0
saveReturn 1
fi fi
endReturn -f "Identity found. Skipping creation" exe ssh-keygen -b 4096 -t rsa -C "$(hostname)" -N "" -f $(realpath $toolIdentity)
echo "Creating..."
} }
step_10_info() { echo "Update remote(s) [CMDLIST] <USER:HOST>"; } step_5_info() { echo "Send key to remote host <USER@HOST> [PORT]"; }
step_10_alias() { ALIAS="send"; } step_5_alias() { ALIAS="sendkey"; }
step_5() {
local sshPort=22
if [ -z "$2" ] || [ "$2" == "" ] ; then
echo " [E] Host not provided."
return 1
fi
exep "ssh-keygen -l -f $toolIdentity 2>>/dev/null"
saveReturn $?
endReturn -f "Key $(realpath $toolIdentity) not found.\n Create one first."
if [ ! -z "$3" ] && [ "$3" != "" ] ; then
sshPort=$3
fi
exe ssh-copy-id -p $sshPort $2
}
step_10_info() { echo "Send command(ssh)/file(scp) list to remote(s) <CMDLIST> [USER:HOST]"; }
step_10_alias() { ALIAS="sendlist"; }
step_10() { step_10() {
aList=$2 aList=$2
aHost=$3 aHost=$3
@@ -32,14 +58,14 @@ step_10() {
fi fi
parseList $aList parseList $aList
saveReturn $? saveReturn $?
endReturn endReturn -f "Parsing error"
} }
# parseList [CMDFILE] # parseList [CMDFILE]
# Parse a list to transfer files and/or execute commands on one or different hosts # Parse a list to transfer files and/or execute commands on one or different hosts
# #
# List format # List format
# [COMMAND h,f,u]|[STRING]|[STRING2] # <COMMAND h,f,u>|<STRING>|[STRING2]
# COMMAND h|H|host - Change user@host:port for the following ssh/scp commands # COMMAND h|H|host - Change user@host:port for the following ssh/scp commands
# f|F|file - Source- and destination file path combination # f|F|file - Source- and destination file path combination
# u|U|update - Execute command on the remote host # u|U|update - Execute command on the remote host
@@ -53,6 +79,8 @@ step_10() {
# u - not used # u - not used
# #
parseList() { parseList() {
local errorMsg=""
if [ -z "$1" ] || [ ! -f "$1" ]; then if [ -z "$1" ] || [ ! -f "$1" ]; then
if [ -z "$1" ] || [ "$1" == "" ] ; then if [ -z "$1" ] || [ "$1" == "" ] ; then
echo " [E] No Command list found" echo " [E] No Command list found"
@@ -71,50 +99,79 @@ parseList() {
fi fi
return 1 return 1
fi fi
echo " [I] Parsing $1 ..."
while IFS='|' read -r lcmd lsrc ldst; do echo " [I] Parsing $(realpath $1) ..."
local line=1
# Working loop without ssh "stealing standard input" by
# https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while
while IFS='|' read -r lcmd lsrc ldst <&3; do
case "$lcmd" in case "$lcmd" in
h|H|host) h|H|host)
aHost="$lsrc" if [ -z "$lsrc" ] || [ "$lsrc" == "" ]; then
if [ ! -z "$ldst" ] || [ "$ldst" != "" ]; then errorMsg="No host found"
aPort=$ldst #Prevent unwanted actions if parsing is continued
aHost=""
saveReturn 1
else else
# Set port (back) to default in case no port is given aHost="$lsrc"
# after previous change if [ ! -z "$ldst" ] || [ "$ldst" != "" ]; then
aPort=22 aPort=$ldst
else
# Set port (back) to default in case no port is given
# after previous change
aPort=22
fi
echo Target host: ${aHost}:$aPort
fi fi
echo Host update: ${aHost}:$aPort
;; ;;
f|F|file) f|F|file)
if [ -z "$aHost" ] || [ "$aHost" == "" ]; then if [ -z "$aHost" ] || [ "$aHost" == "" ]; then
echo " [E] No host found" errorMsg="No host found"
return 1 saveReturn 1
else
exe scp -p $aPort $lsrc ${aHost}:$ldst
saveReturn $?
errorMsg="scp to $aHost failed with $?"
fi fi
echo "scp -p $aPort $lsrc ${aHost}:$ldst"
;; ;;
u|U|update) u|U|update)
if [ -z "$aHost" ] || [ "$aHost" == "" ]; then if [ -z "$aHost" ] || [ "$aHost" == "" ]; then
echo " [E] No host found" errorMsg="No host found"
return 1 saveReturn 1
else
exe ssh -p $aPort $aHost $lsrc
saveReturn $?
errorMsg="ssh to $aHost failed with $?"
fi fi
echo "ssh -p $aPort $aHost $lsrc"
;; ;;
*) *)
# Making comments possible
((line++))
continue continue
;; ;;
esac esac
done < "$1" getReturn
if [ $? -ne 0 ] ; then
echo -e " [E] $line:$errorMsg"
if [ $QUIET -eq 0 ] ; then
endReturn -f "Stop on first error"
fi
fi
((line++))
done 3<"$1"
echo " [I] Parsed $((--line)) lines"
} }
listFileTemplate="# following files are send to host given on command line listFileTemplate="# following files are send to host given on command line
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile
# host and port are changed for the following files # host and port are changed for the following lines
h|user@host2|port2 h|user@host2|port2
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile
u|/destdir/updatescript.sh u|/destdir/updatescript.sh
# host is changed and port set to default 22 for the following files # host is changed and port set to default 22 for the following lines
h|user@host h|user@host
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile
f|/sourcedir/sourcefile|/destdir/destfile f|/sourcedir/sourcefile|/destdir/destfile