Changed to Ubuntu and updated for Py3

This commit is contained in:
PackeTsar 2020-04-29 08:40:01 -07:00
parent 8b40c17d6f
commit 65cfe8fa3c
2 changed files with 45 additions and 101 deletions

124
README.md Executable file → Normal file
View file

@ -12,7 +12,7 @@ A Telnet, SSH and Simple HTTP Based Public IP Address Lookup Service
----------------------------------------- -----------------------------------------
### VERSION ### ### VERSION ###
The version of CheckMyIP documented here is: **v1.1.0** The version of CheckMyIP documented here is: **v1.3.0**
----------------------------------------- -----------------------------------------
### TABLE OF CONTENTS ### ### TABLE OF CONTENTS ###
@ -69,11 +69,9 @@ shutdown -r now
Install Dependencies Install Dependencies
``` ```
yum install git -y sudo apt install python3-pip
yum install gcc -y sudo -H pip3 install paramiko
yum install libffi-devel -y sudo apt install python3-gssapi
yum install openssl-devel -y
pip install python-gssapi
``` ```
Clone Repo Clone Repo
@ -81,98 +79,35 @@ Clone Repo
git clone https://github.com/PackeTsar/checkmyip.git git clone https://github.com/PackeTsar/checkmyip.git
``` ```
Install Binary Create Service (`sudo nano /etc/systemd/system/checkmyip.service`)
``` ```
cd checkmyip [Unit]
Description=CheckMyIP Service
After=network-online.target
Wants=network-online.target
cp checkmyip.py /bin/checkmyip [Service]
chmod 777 /bin/checkmyip Type=simple
mkdir -p /etc/checkmyip/ PIDFile=/var/tmp/checkmyip.pid
WorkingDirectory=/home/ubuntu/checkmyip
ExecStart=/usr/bin/python3 checkmyip.py
Restart=on-failure
RestartSec=30
PrivateTmp=true
[Install]
WantedBy=multi-user.target
``` ```
Create Service (`vi /etc/init.d/checkmyip`)
```
#!/bin/bash
# checkmyip daemon
# chkconfig: 345 20 80
# description: CheckMyIP Daemon
# processname: checkmyip
DAEMON_PATH="/bin/"
DAEMON=checkmyip
STDOUTFILE=/etc/checkmyip/stdout.log
STDERR=/etc/checkmyip/stderr.log
NAME=CheckMyIP
DESC="CheckMyIP Daemon"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`stdbuf -o0 $DAEMON >> $STDOUTFILE 2>>$STDERR & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s
" "Fail"
else
echo $PID > $PIDFILE
printf "%s
" "Ok"
fi
;;
status)
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s
" "Process dead but pidfile exists"
else
echo "$DAEMON (pid $PID) is running..."
fi
else
printf "%s
" "$DAEMON is stopped"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -HUP $PID
printf "%s
" "Ok"
rm -f $PIDFILE
else
printf "%s
" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
```
Finish and Start Up Service Finish and Start Up Service
``` ```
chmod 777 /etc/init.d/checkmyip sudo systemctl daemon-reload
chkconfig checkmyip on sudo systemctl enable checkmyip
service checkmyip start sudo systemctl start checkmyip
service checkmyip status sudo systemctl status checkmyip
``` ```
@ -183,6 +118,13 @@ service checkmyip status
- Was seeing issues where SSH would be very slow to exchange. Likely related to log file sizes, so I change the logging function to turnover to new logging files every day. - Was seeing issues where SSH would be very slow to exchange. Likely related to log file sizes, so I change the logging function to turnover to new logging files every day.
-----------------------------------------
### UPDATES IN V1.1.0 --> V1.3.0 ###
- README updated for install on Ubuntu instead of CentOS
- Small tweaks to support Python3
----------------------------------------- -----------------------------------------
### CONTRIBUTING ### ### CONTRIBUTING ###

22
checkmyip.py Executable file → Normal file
View file

@ -8,7 +8,7 @@
##### Inform version here ##### ##### Inform version here #####
version = "v1.2.0" version = "v1.3.0"
##### Import python2 native modules ##### ##### Import python2 native modules #####
@ -37,7 +37,8 @@ j2send = """{
"port": "{{ port }}", "port": "{{ port }}",
"protocol": "{{ proto }}", "protocol": "{{ proto }}",
"version": "%s", "version": "%s",
"website": "https://github.com/packetsar/checkmyip" "website": "https://github.com/packetsar/checkmyip",
"sponsor": "Sponsored by ConvergeOne, https://www.convergeone.com/"
}""" % version }""" % version
@ -178,7 +179,8 @@ def listener(port, talker):
def telnet_talker(client, valdict, proto="telnet"): def telnet_talker(client, valdict, proto="telnet"):
valdict.update({"proto": proto}) # Add the protocol to the value dict valdict.update({"proto": proto}) # Add the protocol to the value dict
log(j2format(j2log, valdict)) # Log the query to the console and logfile log(j2format(j2log, valdict)) # Log the query to the console and logfile
client.send(j2format(j2send, valdict)+"\n") # Send the query response # Send the query response
client.send(f'{j2format(j2send, valdict)}\n'.encode())
client.close() # Close the channel client.close() # Close the channel
@ -228,17 +230,17 @@ def http_talker(client, valdict, proto="http"):
Content-Length: %s Content-Length: %s
Content-Type: application/json; encoding=utf8 Content-Type: application/json; encoding=utf8
Connection: close""" % str(len(response_body_raw)) # Response with headers Connection: close""" % str(len(response_body_raw)) # Response with headers
client.send(response_headers_raw + "\n\n" + response_body_raw) client.send(f'{response_headers_raw}\n\n{response_body_raw}'.encode())
client.close() client.close()
##### Server startup method. Starts a listener thread for each TCP port ##### ##### Server startup method. Starts a listener thread for each TCP port #####
def start(): def start():
talkers = {22: ssh_talker, 23: telnet_talker, talkers = {22: ssh_talker, 23: telnet_talker,
80: http_talker} # Three listeners on different ports 80: http_talker} # Three listeners on different ports
for talker in talkers: for talker in talkers:
# Launch a thread for each listener # Launch a thread for each listener
thread = threading.Thread(target=listener, thread = threading.Thread(target=listener,
args=(talker, talkers[talker])) args=(talker, talkers[talker]))
thread.daemon = True thread.daemon = True
thread.start() thread.start()
@ -261,19 +263,19 @@ class CheckMyIP_Client:
def get(self): # Primary method to run IP check def get(self): # Primary method to run IP check
if self._af == "auto": # If we are using an auto address family if self._af == "auto": # If we are using an auto address family
try: # Try using IPv6 try: # Try using IPv6
sock = self._socket.socket(self._socket.AF_INET6, sock = self._socket.socket(self._socket.AF_INET6,
self._socket.SOCK_STREAM) self._socket.SOCK_STREAM)
sock.connect((self.server, 23)) sock.connect((self.server, 23))
except: # Fall back to IPv4 if IPv6 fails except: # Fall back to IPv4 if IPv6 fails
sock = self._socket.socket(self._socket.AF_INET, sock = self._socket.socket(self._socket.AF_INET,
self._socket.SOCK_STREAM) self._socket.SOCK_STREAM)
sock.connect((self.server, 23)) sock.connect((self.server, 23))
elif self._af == "ipv6": # If we are using the IPv6 address family elif self._af == "ipv6": # If we are using the IPv6 address family
sock = self._socket.socket(self._socket.AF_INET6, sock = self._socket.socket(self._socket.AF_INET6,
self._socket.SOCK_STREAM) self._socket.SOCK_STREAM)
sock.connect((self.server, 23)) sock.connect((self.server, 23))
elif self._af == "ipv4": # If we are using the IPv4 address family elif self._af == "ipv4": # If we are using the IPv4 address family
sock = self._socket.socket(self._socket.AF_INET, sock = self._socket.socket(self._socket.AF_INET,
self._socket.SOCK_STREAM) self._socket.SOCK_STREAM)
sock.connect((self.server, 23)) sock.connect((self.server, 23))
self._raw_data = sock.recv(1024).decode() self._raw_data = sock.recv(1024).decode()