Below is a script I use for my kali VM on my laptop. The laptop does not have a ton of power, so I use a headless VM and `ssh -X` into it.
```bash
#!/bin/bash
echo " "
# IP address to ping
ip_address="192.168.100.186"
echo " "
echo "---> Checking if VM is up <---"
# Function to check if the IP is reachable
check_ip_reachability() {
if ping -c 1 -W 1 "$ip_address" > /dev/null 2>&1; then
return 0 # IP is reachable
else
return 1 # IP is not reachable
fi
}
# Function to start the VM with virsh
start_vm() {
sudo virsh --connect qemu:///system start "KALI"
}
countdown () {
local secs="$1"
while [ "$secs" -gt 0 ]; do
local pad=""
if [ "$secs" -lt 10 ]; then
pad=" "
fi
echo -ne "Connecting in.... $pad$secs\r"
sleep 1
secs=$((secs -1))
done
echo "Connecting in .... 0"
}
# Main script
if check_ip_reachability; then
echo " "
echo "---> KALI is already up, SSHing now <---"
echo " "
ssh -X
[email protected]
else
echo " "
echo "---> Starting KALI <---"
start_vm
countdown 15
echo " "
echo "SSHing to kilo... Happy Hacking!"
echo " "
ssh -X
[email protected]
fi
```