Created
April 19, 2018 03:24
-
-
Save dale3h/f69ebd4bf6851ecca0fecc78c416eb64 to your computer and use it in GitHub Desktop.
[Home Assistant] Hass.io - Install Packages and Execute Telnet Command
This file contains 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 telnet-wrapper.sh "uptime" | |
## @usage telnet-wrapper.sh "ping -c1 10.0.0.2" | |
## @description Install Alpine packages and run telnet command | |
## @license MIT | |
## @author Dale Higgs <@dale3h> | |
################################################################ | |
# Set these as needed | |
host="10.0.0.1" | |
username="Admin" | |
password="Admin" | |
cmd="$1" | |
logout="logout" | |
# You shouldn't need to edit anything below this line | |
if [[ -z "$cmd" ]]; then | |
cmd="uptime" | |
fi | |
apk=$(command -v apk) || { echo "missing apk"; exit 0; } | |
expect=$(command -v expect) | |
telnet=$(command -v telnet) | |
if [[ -z "$expect" ]]; then | |
$apk add --quiet --no-cache expect > /dev/null 2>&1 | |
expect=$(command -v expect) | |
[[ -n "$expect" ]] || { echo "missing expect"; exit 0; } | |
fi | |
if [[ -z "$telnet" ]]; then | |
$apk add --quiet --no-cache busybox-extras > /dev/null 2>&1 | |
telnet=$(command -v telnet) | |
[[ -n "$telnet" ]] || { echo "missing telnet"; exit 0; } | |
fi | |
result=$( | |
/usr/bin/expect <<EOD | |
set timeout 1 | |
log_user 0 | |
spawn -noecho telnet ${host} | |
expect -re "login:\r" | |
send "${username}\r" | |
expect -re "Password:\r" | |
send "${password}\r" | |
expect -re "#" | |
log_user 1 | |
send "echo START_OUTPUT; ${cmd}; echo END_OUTPUT\r" | |
set timeout 10 | |
expect -re "#" | |
send "${logout}\r" | |
expect eof | |
EOD | |
) | |
echo "$result" | sed ':a;N;$!ba;s/.*START_OUTPUT\s\+\(.*\)\s\+END_OUTPUT.*/\1/g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment