Created
July 7, 2023 11:47
-
-
Save pich4ya/e8731efbfef96c6adc28d010aab6b5ff to your computer and use it in GitHub Desktop.
Spawn a Vultr VPS instance with zsh script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# @author Pichaya Morimoto ([email protected]) | |
# Ansible's Vultr module is suck. I have built my own. | |
# Nuke all my Vultr instances | |
ssh_key=$(cat ~/.ssh/id_ed25519_vultr.pub) | |
echo $ssh_key | |
# List Instances | |
curl "https://api.vultr.com/v2/instances" \ | |
-X GET \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" > instances.txt | |
cat instances.txt | jq '.instances[].id'|tr -d '"' > instances_id.txt | |
if [[ -s instances_id.txt ]]; then | |
# Delete (All) Instance | |
for id in `cat instances_id.txt`;do | |
curl "https://api.vultr.com/v2/instances/${id}" \ | |
-X DELETE \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" | |
done | |
fi | |
echo https://my.vultr.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# @author Pichaya Morimoto ([email protected]) | |
# Ansible's Vultr module is suck. I have built my own. | |
# Requirements: Edit paths in this script, and set ${VULTR_API_KEY} from https://my.vultr.com/settings/#settingsapi | |
# Spawn Vultr VPS (Ubuntu 22.04), 6 USD/Month | |
# 1v CPU, RAM 1 GB, SSD 32 GB, Bandwidth 1 TB | |
# curl "https://api.vultr.com/v2/plans" \ | |
# -X GET \ | |
# -H "Authorization: Bearer ${VULTR_API_KEY}" | jq '.plans[] | select(.monthly_cost < 20)' | |
rm ~/.ssh/id_ed25519_vultr* | |
if [ ! -f ~/.ssh/id_ed25519_vultr.pub ]; then | |
echo "[+] Generate SSH Public Key" | |
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519_vultr -C "vultr" -N '' | |
fi | |
ssh_key=$(cat ~/.ssh/id_ed25519_vultr.pub) | |
echo $ssh_key | |
echo "[*] Check SSH Public Key on vultr" | |
curl "https://api.vultr.com/v2/ssh-keys" \ | |
-X GET \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" > ~/Documents/vultr/vultr_ssh-keys.txt | |
cat vultr_ssh-keys.txt | |
if ! grep -q '"total":0' vultr_ssh-keys.txt; then | |
# if [[ $(cat vultr_ssh-keys.txt | jq '.meta.total') -gt 0 ]]; then | |
echo "[*] SSH Public Key already existed" | |
ssh_key_ids=$(cat vultr_ssh-keys.txt |jq '.ssh_keys[0].id' |tr -d '"') | |
for ssh_key_id in $(echo $ssh_key_ids) | |
do | |
echo "[-] Deleting SSH Public Key ${ssh_key_id}" | |
curl "https://api.vultr.com/v2/ssh-keys/${ssh_key_id}" \ | |
-X DELETE \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" | |
done | |
fi | |
echo "[+] Add new SSH Public Key" | |
curl "https://api.vultr.com/v2/ssh-keys" \ | |
-X POST \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" \ | |
-H "Content-Type: application/json" \ | |
--data '{ | |
"name" : "tmp_ssh_key", | |
"ssh_key" : "'${ssh_key}'" | |
}' | |
echo "[+] List SSH Public Key" | |
curl "https://api.vultr.com/v2/ssh-keys" \ | |
-X GET \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" > ~/Documents/vultr/vultr_ssh-keys.txt | |
ssh_key_id=$(cat vultr_ssh-keys.txt |jq '.ssh_keys[0].id' |tr -d '"') | |
echo "[+] List Instances" | |
# List Instances | |
curl "https://api.vultr.com/v2/instances" \ | |
-X GET \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" > ~/Documents/vultr/instances.txt | |
cat instances.txt | jq '.instances[].id'|tr -d '"' > ~/Documents/vultr/instances_id.txt | |
echo "[+] Delete Instances" | |
if [[ -s instances_id.txt ]]; then | |
# Delete (All) Instance | |
for id in `cat instances_id.txt`;do | |
curl "https://api.vultr.com/v2/instances/${id}" \ | |
-X DELETE \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" | |
done | |
fi | |
new_instance=$(curl "https://api.vultr.com/v2/instances" \ | |
-X POST \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" \ | |
-H "Content-Type: application/json" \ | |
--data '{ | |
"region" : "sgp", | |
"plan" : "vhf-1c-1gb", | |
"label" : "ubuntu", | |
"os_id" : 1743, | |
"backups" : "disabled", | |
"sshkey_id": ["'${ssh_key_id}'"], | |
"hostname": "ubuntu", | |
"tags": [ | |
"ubuntu" | |
] | |
}' ) | |
new_instance_id=$(echo $new_instance |jq '.instance.id' |tr -d '"') | |
date | |
echo "[*] Wait until IPv4 is assigned to the VPS" | |
count=0 | |
while true; do | |
main_ip=$(curl "https://api.vultr.com/v2/instances" \ | |
-X GET \ | |
-H "Authorization: Bearer ${VULTR_API_KEY}" |jq '.instances[0].main_ip' |tr -d '"') | |
if [[ $main_ip != *"0.0.0.0"* ]]; then | |
echo "[*] IP address ${main_ip} is assigned to the VPS" | |
break | |
else | |
echo "[*] $count minute(s) is passed..." | |
count=$((count+1)) | |
sleep 60 | |
fi | |
done | |
date | |
echo "[*] Wait until for spwaning the chosen VPS (~5 minutes)" | |
port_number="22" | |
count=0 | |
while ! nc -z $main_ip $port_number | |
do | |
count=$((count+1)) | |
sleep 60 | |
echo "[*] $count minute(s) is passed..." | |
done | |
echo '[*] VPS is spwaned successfully !' | |
date | |
echo "[*] Port ${main_ip}:${port_number} is open" | |
REMOTE_COMMANDS=$(cat <<EOF | |
#!/bin/bash | |
apt update | |
apt upgrade -y | |
apt dist-upgrade -y | |
apt autoremove -y | |
apt autoclean -y | |
apt install net-tools tmux git autojump zsh build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev libpcap-dev whois ncat nmap -y | |
sh -c "\$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended | |
chsh -s /bin/zsh | |
wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz | |
tar -xvf go1.20.3.linux-amd64.tar.gz | |
rm go1.20.3.linux-amd64.tar.gz | |
mv go /usr/local | |
export GOROOT=/usr/local/go | |
export GOPATH=\$HOME/go/ | |
export PATH=\$GOPATH/bin:\$GOROOT/bin:\$HOME/app/bin:\$PATH | |
( | |
echo 'export GOROOT=/usr/local/go' | |
echo 'export GOPATH=\$HOME/go/' | |
echo 'export PATH=\$GOPATH/bin:\$GOROOT/bin:\$HOME/app/bin:\$PATH' | |
) >> ~/.zshrc | |
curl https://pyenv.run | bash | |
( | |
echo 'export PYENV_ROOT="\$HOME/.pyenv"' | |
echo 'command -v pyenv >/dev/null || export PATH="\$PYENV_ROOT/bin:\$PATH"' | |
echo 'eval "\$(pyenv init -)"' | |
echo 'eval "\$(pyenv virtualenv-init -)"' | |
) >> ~/.zshrc | |
source ~/.zshrc | |
pyenv install 2.7.18 | |
pyenv install 3.11.2 | |
pyenv global 3.11.2 | |
reboot | |
EOF | |
) | |
echo ssh -i ~/.ssh/id_ed25519_vultr root@${main_ip} | |
ssh -i ~/.ssh/id_ed25519_vultr root@${main_ip} "echo ${REMOTE_COMMANDS} | bash -s" | |
echo "[*] Rebooting.." | |
count=0 | |
while ! nc -z $main_ip $port_number | |
do | |
count=$((count+1)) | |
sleep 60 | |
echo "[*] $count minute(s) is passed..." | |
done | |
# scp -i ~/.ssh/id_ed25519_vultr "${HOME}/script.sh" root@${main_ip}:/blah | |
echo https://my.vultr.com/ | |
#mkdir -p ${HOME}/Desktop/vultr_shared | |
#echo sshfs -i ~/.ssh/id_ed25519_vultr root@${main_ip}:/root/ ${HOME}/Desktop/vultr_shared -ovolname=vultr_shared | |
echo ssh -i ~/.ssh/id_ed25519_vultr root@${main_ip} | |
# ssh -i ~/.ssh/id_ed25519_vultr root@${main_ip} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment