-
Notifications
You must be signed in to change notification settings - Fork 13
/
rconsole
executable file
·99 lines (85 loc) · 2.72 KB
/
rconsole
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
#!/usr/bin/env bash
# rconsole -- attach to a server's remote console via IPMI SOL
. lib.bash || exit
usage() {
echo "Usage: $progname <host> [-p] [-P <state>] [-v]"
echo
echo_opt "-p" "show system power status"
echo_opt "-P state" "switch system power on/off/reset/cycle"
echo_opt "-v" "be verbose"
}
case $1 in
--help) usage; exit;;
esac
case ${0##*/} in
*-console) host=${0##*/}; host=${host%-console};;
*) host=${1?missing hostname}; shift;;
esac
verbose=0
opt_getpower=0
opt_setpower=""
while getopts ":P:pv" OPT; do
case $OPT in
P) opt_setpower=$OPTARG;;
p) opt_getpower=1;;
v) verbose=1;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
case $host in
*@*) bhost=${host##*@}; host=${host%@*};;
*.*) bhost=$host;;
*) bhost="bmc.$host.nullroute.lt";;
esac
# Look up operator password in ~/.netrc
creds=$(getnetrc -sdf %a:%u:%p "ipmi/$bhost" ||
getnetrc -sdf %a:%u:%p "ipmi/$host" ||
getnetrc -sdf %a:%u:%p "ipmi/*")
priv=${creds%%:*}; creds=${creds#*:}
user=${creds%%:*}; creds=${creds#*:}
pass=$creds
if [[ ! $user || ! $pass ]]; then
die "Credentials for 'ipmi/$bhost' not found in ~/.netrc"
fi
if [[ ! $priv ]]; then
# Guess privilege level from username (e.g. Operator => priv OPERATOR)
priv=${user^^}
fi
if (( verbose )); then
echo "Connecting to '$bhost' as '$user', privilege level '$priv'" >&2
fi
# New ipmitool defaults to ciphersuite 17 (AES-CBC-128 with HMAC-SHA256), which
# iLO4 does not support. Fall back to the HMAC-SHA1 variant.
readonly AESCBC128_HMACSHA1=3
cipher=$AESCBC128_HMACSHA1
if have ipmipower && have ipmiconsole && [[ ! $USE_IPMITOOL ]]; then
args=(-h "$bhost" -u "$user" -p "$pass" -l "$priv" -I "$cipher")
bmc_console() { ipmiconsole "${args[@]}"; }
bmc_get_power() { ipmipower -D LAN_2_0 "${args[@]}" --stat; }
bmc_set_power() { ipmipower -D LAN_2_0 "${args[@]}" --${1?}; }
elif have ipmitool; then
args=(-H "$bhost" -U "$user" -P "$pass" -L "$priv" -C "$cipher")
# Use an escape character different from the SSH one
args+=(-e '&')
bmc_console() { ipmitool -I lanplus "${args[@]}" sol activate; }
bmc_get_power() { ipmitool -I lanplus "${args[@]}" power status; }
bmc_set_power() { ipmitool -I lanplus "${args[@]}" power ${1?}; }
else
die "No IPMI client tool (freeipmi nor ipmitool) found"
fi
if (( opt_getpower )); then
echo "[Connecting to '$bhost']"
bmc_get_power
elif [[ $opt_setpower ]]; then
if [[ $opt_setpower != @(on|off|cycle|reset) ]]; then
die "Unknown power command '$opt_setpower'"
fi
confirm "Send 'power $opt_setpower' command to '$bhost'?" || exit
echo "[Connecting to '$bhost']"
bmc_set_power "$opt_setpower"
elif [[ ! $1 ]]; then
echo "[Connecting to '$bhost' ... escape sequence is '&.']"
bmc_console
else
die "Unrecognized arguments: '$*'"
fi