-
Notifications
You must be signed in to change notification settings - Fork 1
/
jump
executable file
·151 lines (134 loc) · 3.42 KB
/
jump
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
# Runs a jumpbox for retro Linux distros to talk to
set -euo pipefail
SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
JUMPHOME=${JUMPHOME:-$SCRIPTDIR/.retrojump}
CLOUDIMG=jammy-server-cloudimg-amd64.img
JUMPIMG=$JUMPHOME/retrojump.img
# temp directory automatically cleaned up on exit
TEMPDIR=$(mktemp -d)
trap 'rm -rf "$TEMPDIR"' EXIT
jump_init() {
mkdir -p $JUMPHOME
if [[ ! -f $JUMPHOME/$CLOUDIMG ]]; then
wget -O $JUMPHOME/$CLOUDIMG "https://cloud-images.ubuntu.com/jammy/current/$CLOUDIMG"
fi
cp $JUMPHOME/$CLOUDIMG $JUMPIMG
qemu-img resize $JUMPIMG 20G
# remove any old keys
ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[localhost]:2222"
if [[ ! -f $JUMPHOME/id_rsa ]]; then
ssh-keygen -q -C retro@retrojump -f $JUMPHOME/id_rsa -N ""
fi
local pubkey=$(cat $JUMPHOME/id_rsa.pub)
cat > $TEMPDIR/metadata <<EOF
instance-id: retrojump-1
local-hostname: retrojump
EOF
cat > $TEMPDIR/userdata <<EOF
#cloud-config
user: retro
password: retro
chpasswd: { expire: False }
ssh_pwauth: True
ssh_authorized_keys:
- $pubkey
packages:
- vsftpd
write_files:
- path: /etc/vsftpd.conf
content: |
listen_address=10.0.2.1
listen=NO
listen_ipv6=YES
anonymous_enable=YES
local_enable=YES
write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
EOF
cat > $TEMPDIR/networkconfig <<EOF
#cloud-config
network:
version: 1
config:
- type: physical
name: enp0s2
subnets:
- type: dhcp
routes:
- network: 10.0.1.0/24
gateway: 10.0.1.2
- type: physical
name: enp0s3
subnets:
- type: static
address: 10.0.2.1/24
routes:
- network: 10.0.2.0/24
gateway: 10.0.2.2
EOF
cloud-localds $TEMPDIR/seed.img $TEMPDIR/userdata $TEMPDIR/metadata -N $TEMPDIR/networkconfig
}
jump_run() {
local seed=""
if [[ ! -f $JUMPIMG ]]; then
jump_init
seed="-drive if=virtio,format=raw,file=$TEMPDIR/seed.img"
fi
qemu-system-x86_64 \
-nographic \
-machine accel=kvm,type=q35 \
-cpu host \
-m 2G \
-netdev user,id=internet,net=10.0.1.0/24,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=internet \
-netdev socket,id=retronet,listen=:1234 \
-device virtio-net-pci,netdev=retronet \
-drive if=virtio,format=qcow2,file=$JUMPIMG \
$seed \
$@
}
jump_ssh() {
ssh -i $JUMPHOME/id_rsa -p 2222 retro@localhost $@
}
jump_sftp() {
sftp -i $JUMPHOME/id_rsa -P 2222 retro@localhost $@
}
jump_scp() {
local params=$(echo $@ | sed 's/\bretro:/retro@localhost:/g')
scp -i $JUMPHOME/id_rsa -P 2222 $params
}
jump_usage() {
cat <<EOF
Usage: $(basename $0) COMMAND ...
Commands:
run start the jumpbox with a serial console
ssh ssh into jumpbox
sftp sftp into jumpbox
scp scp file into into jumpbox
Additional parameters are passed verbatim to ssh/sftp/scp.
For scp, 'retro:*' is expanded to 'retro@localhost:*'.
EOF
exit 1
}
if [[ $# -ge 1 ]]; then
COMMAND=$1
shift
case $COMMAND in
run) jump_run $@;;
ssh) jump_ssh $@;;
sftp) jump_sftp $@;;
scp) jump_scp $@;;
*) jump_usage;;
esac
else
jump_usage
fi