aboutsummaryrefslogtreecommitdiff
path: root/tools/rexec.sh
blob: 388cbc3af45c25a13d97a2bb26c4d96f3b553e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash

#    rexec.sh
#    ltask lightweight remote task execution
#    Copyright (C) 2020 Jonas Gunz <himself@jonasgunz.de>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

ARGC=$#
ARGV=($@)

SSH_IDENTITY=~/.ssh/id_rsa
SSH_PORT=22
SSH_HOST=

SSH_OPTIONS="-q -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no"
SSH="ssh $SSH_OPTIONS"

SCRIPT_FILES=()
SCRIPT_INTERPRETER=/bin/sh

FILES=()

function parse_args() {
	for (( i=0; i < $ARGC;i++ )); do
		local ARGREGEX="^-.*"
		if [[ ! ${ARGV[$i]} =~ $ARGREGEX ]]; then
			[ -z $SSH_HOST ] && SSH_HOST=${ARGV[$i]} && continue
			SCRIPT_FILES+=(${ARGV[$i]})
			continue
		fi

		case ${ARGV[$i]} in
			-p)
				i=$((i+1))
				SSH_PORT=${ARGV[$i]};;
			-s)
				i=$((i+1))
				SSH=${ARGV[$i]} $SSH_OPTIONS;;
			-i)
				i=$((i+1))
				SSH_IDENTITY=${ARGV[$i]};;
			-f)
				i=$((i+1))
				FILES+=(${ARGV[$i]});;
			-h)
				print_help 0;;
			*)
				echo Invalid Argument ${ARGV[$i]}
				echo $0 -h for help
				exit 1;;
		esac
	done
	
	[ -z $SSH_HOST ] && echo No host specified && exit 1
	[ ${#SCRIPT_FILES[@]} -eq 0 ] && echo No script specified && exit 1
}

function print_help() {
	cat << EOF
Execute a script on a remote host via SSH

Usage:
$0 [OPTIONS] [USER@]HOST SCRIPT
	-p <PORT>	SSH port (default: 22)
	-s <SSH>	Custom SSH program
	-i <IDENTITY>	SSH Identity file (default: ~/.ssh/id_rsa.pub)
	-f FILE		Copy FILE to target before execution
	-h		Print this help text
EOF
	exit $1
}

parse_args

[ ${#FILES[@]} -gt 0 ] && scp $SSH_OPTIONS -i $SSH_IDENTITY -P $SSH_PORT ${FILES[@]} $SSH_HOST:

cat ${SCRIPT_FILES[@]} | $SSH -p $SSH_PORT -i $SSH_IDENTITY $SSH_HOST "/bin/bash 2>&1" 2>/dev/null
exit $?