-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-run
executable file
·101 lines (77 loc) · 1.69 KB
/
ssh-run
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
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly SELF=$(basename "${BASH_SOURCE[0]}")
readonly OS=$(uname)
readonly VERSION=0.0.1
OPT_HELP=
OPT_DEBUG=
OPT_VERBOSE=
function error {
if [[ "${OS}" == "Darwin" ]]; then
echo "error: ${@}" >&2
else
echo -e "\e[0;31m\e[1merror: \e[0;0m${@}" >&2
fi
exit 1
}
function warn {
if [[ "${OS}" == "Darwin" ]]; then
echo "warning: ${@}" >&2
else
echo -e "\e[0;33mwarning: \e[0;0m${@}" >&2
fi
}
function debug {
if [[ -n "${OPT_DEBUG}" ]]; then
echo '**'
echo \${@}: ${@}
echo \$OPT_HELP: "${OPT_HELP}"
echo \$OPT_DEBUG: "${OPT_DEBUG}"
echo \$OPT_VERBOSE: "${OPT_VERBOSE}"
fi
}
function parse_arguments {
debug ${FUNCNAME[0]} "$@"
local opts=$(getopt -n "${SELF}" --options hdv --longoptions help,debug,verbose -- "$@")
if [[ $? != 0 ]]; then error "failed to parsing arguments."; fi
eval set -- "${opts}"
while true; do
case "$1" in
(-h|--help) OPT_HELP=true; shift ;;
(-d|--debug) OPT_DEBUG=true; shift ;;
(-v|--verbose) OPT_VERBOSE=true; shift ;;
(--) shift; break ;;
(*) break ;;
esac
done
}
function process_arguments {
debug ${FUNCNAME[0]} "$@"
if [[ -n "${OPT_HELP}" || "${#}" -lt 1 ]]; then
display_usage
else
return 0
fi
}
function display_usage {
debug ${FUNCNAME[0]} "$@"
cat << EOF
${SELF} [OPTIONS]... <COMMAND> [OPTIONS]...
OPTIONS:
-h, --help Show this help
COMMANDS:
EOF
exit 0
}
function launch_agent {
debug ${FUNCNAME[0]} "$@"
ssh-agent bash -c "ssh-add /root/.ssh/id_rsa; $@"
}
function main {
debug ${FUNCNAME[0]} "$@"
parse_arguments "$@"
process_arguments "$@"
launch_agent "$@"
}
main "$@"