Last active
June 12, 2017 21:14
-
-
Save utdrmac/cc352587b8fa2ef7e2cb52d653542ac5 to your computer and use it in GitHub Desktop.
Managing SSH with pushbullet
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 | |
# Redirect STDOUT/STDERR to journalctl | |
# journalctl -f SYSLOG_IDENTIFIER=pushBulletSSH | |
exec > >(logger -t pushBulletSSH) 2> >(logger -t pushBulletSSH -p user.warn) | |
# Config | |
GASGIANT_ID="XXXXXX" | |
PORTFILE=/tmp/.sshport | |
# Get most recent push | |
tenMinAgo=$(($(date +"%s")-600)) | |
IFS=$'\n' | |
comm=($(curl --header "Access-Token: ${TOKEN}" --data-urlencode active="true" --data-urlencode modified_after="${tenMinAgo}" \ | |
--get https://api.pushbullet.com/v2/pushes 2>/dev/null | /usr/local/bin/jq --raw-output ".pushes[] | select(.target_device_iden == \"${GASGIANT_ID}\").body | ascii_downcase")) | |
if [ "$comm" == "" ]; then | |
echo "No pushes since `date --date=\"@${tenMinAgo}\" +"%r"`" | |
exit | |
fi | |
command=${comm[0]} | |
echo "Latest Command: ${command}" | |
if [ "$command" == "open ssh" ]; then | |
# Generate random port | |
port=$(shuf -i 30000-62000 -n 1) | |
echo "Opening SSH on Port ${port}" | |
echo "${port}" >$PORTFILE | |
# Make call to UPnP | |
echo -n "Response: " | |
curl 'http://10.10.10.1:1980/control?WANIPConnection' \ | |
-X 'POST' \ | |
-H 'Content-Type: text/xml; charset="utf-8"' \ | |
-H 'Connection: close' \ | |
-H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"' \ | |
-d "<?xml version=\"1.0\"?> | |
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> | |
<s:Body> | |
<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\"> | |
<NewRemoteHost></NewRemoteHost> | |
<NewExternalPort>${port}</NewExternalPort> | |
<NewProtocol>TCP</NewProtocol> | |
<NewInternalPort>22</NewInternalPort> | |
<NewInternalClient>10.10.10.202</NewInternalClient> | |
<NewEnabled>1</NewEnabled> | |
<NewPortMappingDescription>Temp SSH</NewPortMappingDescription> | |
<NewLeaseDuration>0</NewLeaseDuration> | |
</u:AddPortMapping> | |
</s:Body> | |
</s:Envelope>" 2>/dev/null | |
echo "SSH Port: $port" | /usr/local/bin/pushbullet | |
elif [ "$command" == "close ssh" ]; then | |
if [ ! -e $PORTFILE ]; then | |
echo "Cannot determine port for closing. File missing." | /usr/local/bin/pushbullet | |
exit | |
fi | |
# get the saved port | |
port=$(cat $PORTFILE) | |
echo "Closing SSH on Port ${port}" | |
# call upnp to delete port mapping | |
echo -n "Response: " | |
curl 'http://10.10.10.1:1980/control?WANIPConnection' \ | |
-X 'POST' \ | |
-H 'Content-Type: text/xml; charset="utf-8"' \ | |
-H 'Connection: close' \ | |
-H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#DeletePortMapping"' \ | |
-d "<?xml version=\"1.0\"?> | |
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> | |
<s:Body> | |
<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\"> | |
<NewRemoteHost></NewRemoteHost> | |
<NewExternalPort>${port}</NewExternalPort> | |
<NewProtocol>TCP</NewProtocol> | |
</u:DeletePortMapping> | |
</s:Body> | |
</s:Envelope>" 2>/dev/null | |
# send update | |
echo "SSH Port Closed" | /usr/local/bin/pushbullet | |
else | |
# unknown | |
echo "Unknown command '${command}'" | /usr/local/bin/pushbullet | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment