-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpct-enter
More file actions
executable file
·57 lines (51 loc) · 1.64 KB
/
pct-enter
File metadata and controls
executable file
·57 lines (51 loc) · 1.64 KB
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
#!/bin/sh
#
# 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 <http://www.gnu.org/licenses/>.
#
# Copyright 2024 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: remotely connect to a proxmox container by name
#
# Inspired by https://gist.github.com/Xenthys/eac5c4b46e8ad016ab8d6584883c754a
#
# NOTE: the remote user must have passwordless sudo permissions
#
for b in jq \
; do
if ! command -v "$b" >/dev/null 2>&1; then
printf "'%s' not found\n" "$b" >&2
exit 1
fi
done
if [ -z "$1" ]; then
printf '%s\n' 'No container requested' >&2
exit 1
fi
name=$1
if [ -z "$PVE_HOST" ]; then
printf '%s\n' 'PVE_HOST is undefined' >&2
exit 1
fi
# Get CT ID by name or number
id=$(ssh "$PVE_HOST" 'sudo pvesh get /cluster/resources --output-format=json' |
jq -r --arg id "$name" '.[] | select(.name==$id or .vmid==($id|tonumber? // .)) | .vmid')
if [ -z "$id" ]; then
printf "Container '%s' does not exist.\n" "$name" >&2
exit 1
fi
# SSH and enter the CT
#
# The `-i` flag is required so that you land in /root
# on entering the CT
ssh -t "$PVE_HOST" sudo -i pct enter "$id"