Introduction
This tutorial will help you to create servers with an API key via one click on Windows OS and without the need of logging into the Hetzner Cloud Console.
Prerequisites
-
Basic knowledge about batch scripting
You may need to get some basic information about batch scripting, but you can safely continue the tutorial without reading any resources.
-
curl
has to be installedBefore starting this tutorial, you have to check if your system runs cURL.
The formal way to check it, is to run the command
curl --version
in a terminal session. It should return something like:curl 7.83.1 (Windows) libcurl/7.83.1 Schannel Release-Date: 2022-05-13 Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp Features: AsynchDNS HSTS IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI UnixSockets
If you don't have it, you should see:
'curl' is not recognized as an internal or external command, operable program or batch file.
If you don't have cURL installed on your system, you can download it here: https://curl.se/download.html
Step 1 - Open PowerShell / Terminal
Right click to an empty space in a folder while pressing the shift key. Then, select the option "Open PowerShell..." or "Open Terminal here".
Create a new .bat
file and open it with notepad:
start cmd
echo.>create-server.bat
notepad create-server.bat
We are starting by disabling verbose mode and text color:
@echo off
title Creating...
SETLOCAL ENABLEDELAYEDEXPANSION
color D
Below this, add the commands from step 2.
Step 2 - Set your variables
Go to Cloud Console > Your Project > Security. Create an API token with "Read & Write" permissions. Then, add your SSH key and choose a name for it.
In the commands below, replace your-api-token
with the API token you created, and your-ssh-key
with the name you chose for your SSH key.
set APITOKEN=your-api-token
set SSHkey=your-ssh-key
set ServerName=myserver
set start_after_create=true
set server_type=cx21
set datacenter=nbg1-dc3
Below this, add the commands from step 3.
Step 3 - Check for your snapshots and images
"%SystemDrive%\Windows\System32\curl.exe" -H "Authorization: Bearer %APITOKEN%" "https://api.hetzner.cloud/v1/images" | findstr /r /c:"description"
"%SystemDrive%\Windows\System32\curl.exe" -H "Authorization: Bearer %APITOKEN%" "https://api.hetzner.cloud/v1/images" | findstr /r /c:"""id"""
echo.
set snapshotIDstatic=111
set /P snapshotIDstatic=Select snapshot ID:
cls
- The first command prints the names of your snapshots and images.
- The second command prints the IDs of those snapshots and images.
When you create your server in step 7, the snapshotIDstatic
variable will prompt you to enter the ID of the snapshot or image you want to use.
Now, add the commands from step 4.
Step 4 - Check for internet connectivity
Check if Internet access is available, otherwise the script may output false info. If no internet access is detected, exit the script immediately.
set internet=0
ping 8.8.8.8 -n 1 -w 3000|findstr /V /i "pinging statistics approx minimum packets"
if errorlevel 1 (set internet=0) else (set internet=1)
if %internet%==0 (echo No Internet Connection!) else (echo Internet connection established.&&goto resume)
echo.
pause
exit
:resume
cls
echo.
echo.
echo.
Below this, add the commands from step 5.
Step 5 - Check if the snapshot or image is available on your account
If the snapshot ID doesn't exist on your account, inform the user and exit.
set abc1=0
set abc2=0
set abctemp=0
set snapshotID=0
set staticFound=0
if %snapshotIDstatic% LSS 102 (set snapshotIDstatic=11111111)
for /f "delims=" %%a in ('%SystemDrive%\Windows\System32\curl.exe -H "Authorization: Bearer %APITOKEN%" "https://api.hetzner.cloud/v1/images" ^| findstr /r /c:"""id"""') do (
set abc2=!abc1!
set abctemp=%%a
if "!abctemp:~12,1!" NEQ ":" (set abc1=%%a)
if !abc1:~12^,8!==!snapshotIDstatic! (set staticFound=1)
)
set snapshotID=!abc2:~12,8!
if %staticFound%==1 (set snapshotID=%snapshotIDstatic%)
if %snapshotID%==0 (echo Snapshot id couldnt get...&&pause&&exit)
echo.
cls
"%SystemDrive%\Windows\System32\curl.exe" -H "Authorization: Bearer %APITOKEN%" -H "Content-Type: application/json" -d "{ \"name\": \"%ServerName%\", \"datacenter\":\"%datacenter%\", \"server_type\": \"%server_type%\", \"start_after_create\":%start_after_create%, \"image\": \"%snapshotID%\", \"ssh_keys\":[\"%SSHkey%\"]}" -X POST "https://api.hetzner.cloud/v1/servers"|findstr ip
echo.
echo.
echo.
Below this, add the commands from step 6.
Step 6 - Wait for user activity
Inform the user if the creation of the server was successful or not, and wait for input from the user.
echo.
echo Snapshot id %snapshotID% created at: %time:~0,5%
echo.
echo SSH username: root
echo.
echo SSH Password: example
echo.
set /p DUMMY=Press enter key to exit...
You can now save and close the file.
Step 7 - Create the server
After you saved the file, run this command:
create-server.bat
You will be asked to select an image and the new server will be created with the name you specified in step 2.
Conclusion
In this tutorial you learned how to create a server from a snapshot or image by simply clicking a desktop shortcut. You may want to dive deeper into "batch scripting" to develop custom scripts.
Hope it helps.