Skip to content

Instantly share code, notes, and snippets.

@kristopolous
Last active February 20, 2025 07:36
Show Gist options
  • Save kristopolous/0241d74c89c4cc9fa327b2e6f3967e9f to your computer and use it in GitHub Desktop.
Save kristopolous/0241d74c89c4cc9fa327b2e6f3967e9f to your computer and use it in GitHub Desktop.
#!/bin/bash
tool=./vast.py
tmp=$(mktemp -d --suffix .vastai)
tmp_host=$(basename $tmp)
instance=$1
_start=${2:-11000}
read -r port ip <<EOF
$($tool show instance $instance --raw | jq -r '.ports["22/tcp"][0].HostPort,.public_ipaddr' | tr '\n' ' ' )
EOF
ssh_url="ssh://root@$ip:$port"
mykey=$(grep -f <($tool show ssh-keys --raw | awk -F '"' '/public_key/ {print $4}') ~/.ssh/*pub | cut -d':' -f1 | sed 's/\.pub$//' | head -1)
cat <<EOF > $tmp/cmd
cat > /tmp/ns
chmod +x /tmp/ns
/tmp/ns -lntp | grep LISTEN | grep 0.0.0.0 | awk '
NR>2 {
split(\$4, a, ":");
split(\$7, b, "/");
cmd = "ps -p " b[1] " -o comm= 2>/dev/null";
cmd | getline pname;
close(cmd);
print pname " " a[2]
}' | grep -v "^ "
EOF
curl -Ls https://github.com/ab2pentest/linux-static-binaries/raw/refs/heads/main/x64/netstat |\
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -Ti "$mykey" $ssh_url "$(cat $tmp/cmd )" > $tmp/ports
echo $tmp/ports
start_port=$_start
cat <<EOF > $tmp/config
Host $tmp_host
HostName $ip
Port $port
User root
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
IdentityFile $mykey
EOF
cat $tmp/ports | while read -r proc port; do
((start_port++))
echo " LocalForward 127.0.0.1:$start_port 127.0.0.1:$port" >> "$tmp/config"
done
ssh -fNC -F $tmp/config $tmp_host
cat <<EOF > $tmp/page.html
<!DOCTYPE html>
<html>
<head>
<title>Magic ports $instance</title>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css'>
</head>
<body class='bg-gray-100 text-gray-800 p-6'>
<div class='max-w-lg mx-auto bg-white shadow-lg rounded-lg overflow-hidden'>
<header class='bg-blue-600 text-white text-center py-4 text-xl font-semibold'>
Magic Ports for $instance ($ip)
</header>
<table class='w-full text-left border-collapse'>
<thead>
<tr class='bg-gray-200'>
<th class='py-3 px-4 border-b'>Service</th>
</tr>
</thead>
<tbody>
EOF
start_port=$_start
cat $tmp/ports | while read -r proc port; do
((start_port++))
echo "<tr class='hover:bg-gray-100'> <td class='py-3 px-4 border-b'><a class='text-blue-600 hover:underline' href='http://localhost:$start_port'>$proc</a></td></tr>" >> $tmp/page.html
done
echo -e "</tr></table><footer class='text-center p-4 bg-gray-200 text-gray-700'>vastai magic-port</footer>\n</body>\n</html>" >> $tmp/page.html
open "$tmp/page.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment