From b79ae416e6e92967546ebc9dd8542f038b20b200 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Wed, 20 Nov 2019 16:39:41 +0000 Subject: [PATCH] WIP for ssh management Function for parsing a command file, which can send files and execute commands on a given remote host --- seqs/ssh.sh | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 seqs/ssh.sh diff --git a/seqs/ssh.sh b/seqs/ssh.sh new file mode 100644 index 0000000..698e037 --- /dev/null +++ b/seqs/ssh.sh @@ -0,0 +1,108 @@ +#!/bin/bash + +aList="" +aHost="" +# default ssh port +aPort="22" + +step_10_info() { echo "Update remote(s) [CMDLIST] "; } +step_10_alias() { ALIAS="send"; } +step_10() { + aList=$2 + aHost=$3 + # Set port only if not empty + if [ ! -z "$4" ] || [ "$4" != "" ]; then + aPort=$4 + fi + parseList $aList + saveReturn $? + endReturn +} + +# parseList [CMDFILE] +# Parse a list to transfer files and/or execute commands on one or different hosts +# +# List format +# [COMMAND h,f,u]|[STRING]|[STRING2] +# COMMAND h|H|host - Change user@host:port for the following ssh/scp commands +# f|F|file - Source- and destination file path combination +# u|U|update - Execute command on the remote host +# +# STRING h - user@host +# f - source file path +# u - shell command string +# +# STRING2 h - port +# f - destination file path +# u - not used +# +parseList() { + if [ -z "$1" ] || [ ! -f "$1" ]; then + if [ -z "$1" ] || [ "$1" == "" ] ; then + echo " [E] No Command list found" + else + echo " [E] Command list not found: $1" + fi + if [ ! -z "$1" ] && [ $DRY == 0 ] && [ $QUIET == 0 ] ; then + read -p " Create template there y/[n]? " answer + case "$answer" in + y|Y) + addConf -s "$listFileTemplate" "$1" + ;; + *) + ;; + esac + fi + return 1 + fi + echo " [I] Parsing $1 ..." + while IFS='|' read -r lcmd lsrc ldst; do + case "$lcmd" in + h|H|host) + aHost="$lsrc" + if [ ! -z "$ldst" ] || [ "$ldst" != "" ]; then + aPort=$ldst + else + # Set port (back) to default in case no port is given + # after previous change + aPort=22 + fi + echo Host update: ${aHost}:$aPort + ;; + f|F|file) + if [ -z "$aHost" ] || [ "$aHost" == "" ]; then + echo " [E] No host found" + return 1 + fi + echo "scp -p $aPort $lsrc ${aHost}:$ldst" + ;; + u|U|update) + if [ -z "$aHost" ] || [ "$aHost" == "" ]; then + echo " [E] No host found" + return 1 + fi + echo "ssh -p $aPort $aHost $lsrc" + ;; + *) + continue + ;; + esac + done < "$1" +} + +listFileTemplate="# following files are send to host given on command line +f|/sourcedir/sourcefile|/destdir/destfile +f|/sourcedir/sourcefile|/destdir/destfile +# host and port are changed for the following files +h|user@host2|port2 +f|/sourcedir/sourcefile|/destdir/destfile +f|/sourcedir/sourcefile|/destdir/destfile +u|/destdir/updatescript.sh +# host is changed and port set to default 22 for the following files +h|user@host +f|/sourcedir/sourcefile|/destdir/destfile +f|/sourcedir/sourcefile|/destdir/destfile +u|/destdir2/update.sh" + +VERSION_SEQREV=5 +. sequencer.sh