Skip to content

Instantly share code, notes, and snippets.

@damianorenfer
Last active September 10, 2016 08:06
Show Gist options
  • Save damianorenfer/11063417 to your computer and use it in GitHub Desktop.
Save damianorenfer/11063417 to your computer and use it in GitHub Desktop.
Bash shutdown script over SSH (Unix) and/or Telnet (Windows).
#!/bin/bash
# usage : ./rshutdown.sh host user password
host=$1
username=$2
password=$3
#SSH, requires sshpass package installed
sshpass -p "$password" ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $username@$host "echo $password | sudo -S poweroff" > /dev/null 2>&1
ssh_status=$?
#Telnet
if [[ $ssh_status != 0 ]]; then
echo "Shudown via SSH on $1 with user $2 failed."
echo "Trying via Telnet (Windows)..."
(
echo open $host
sleep 2
echo -en "$username\r\n"
sleep 1
echo -en "$password\r\n"
sleep 1
echo -en "shutdown /s /t 0\r\n"
sleep 1
) | telnet > /dev/null 2>&1
else
echo "Shudown via SSH seems to be successful, not sure."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment