Last active
September 10, 2016 08:06
-
-
Save damianorenfer/11063417 to your computer and use it in GitHub Desktop.
Bash shutdown script over SSH (Unix) and/or Telnet (Windows).
This file contains hidden or 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
#!/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