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 ###
The version of CheckMyIP documented here is: **v1.1.0**
The version of CheckMyIP documented here is: **v1.3.0**
-----------------------------------------
### TABLE OF CONTENTS ###
@ -69,11 +69,9 @@ shutdown -r now
Install Dependencies
```
yum install git -y
yum install gcc -y
yum install libffi-devel -y
yum install openssl-devel -y
pip install python-gssapi
sudo apt install python3-pip
sudo -H pip3 install paramiko
sudo apt install python3-gssapi
```
Clone Repo
@ -81,98 +79,35 @@ Clone Repo
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
chmod 777 /bin/checkmyip
[Service]
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
```
chmod 777 /etc/init.d/checkmyip
chkconfig checkmyip on
service checkmyip start
service checkmyip status
sudo systemctl daemon-reload
sudo systemctl enable checkmyip
sudo systemctl start checkmyip
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.
-----------------------------------------
### UPDATES IN V1.1.0 --> V1.3.0 ###
- README updated for install on Ubuntu instead of CentOS
- Small tweaks to support Python3
-----------------------------------------
### CONTRIBUTING ###

22
checkmyip.py Executable file → Normal file
View file

@ -8,7 +8,7 @@
##### Inform version here #####
version = "v1.2.0"
version = "v1.3.0"
##### Import python2 native modules #####
@ -37,7 +37,8 @@ j2send = """{
"port": "{{ port }}",
"protocol": "{{ proto }}",
"version": "%s",
"website": "https://github.com/packetsar/checkmyip"
"website": "https://github.com/packetsar/checkmyip",
"sponsor": "Sponsored by ConvergeOne, https://www.convergeone.com/"
}""" % version
@ -178,7 +179,8 @@ def listener(port, talker):
def telnet_talker(client, valdict, proto="telnet"):
valdict.update({"proto": proto}) # Add the protocol to the value dict
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
@ -228,17 +230,17 @@ def http_talker(client, valdict, proto="http"):
Content-Length: %s
Content-Type: application/json; encoding=utf8
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()
##### Server startup method. Starts a listener thread for each TCP port #####
def start():
talkers = {22: ssh_talker, 23: telnet_talker,
talkers = {22: ssh_talker, 23: telnet_talker,
80: http_talker} # Three listeners on different ports
for talker in talkers:
# Launch a thread for each listener
thread = threading.Thread(target=listener,
thread = threading.Thread(target=listener,
args=(talker, talkers[talker]))
thread.daemon = True
thread.start()
@ -261,19 +263,19 @@ class CheckMyIP_Client:
def get(self): # Primary method to run IP check
if self._af == "auto": # If we are using an auto address family
try: # Try using IPv6
sock = self._socket.socket(self._socket.AF_INET6,
sock = self._socket.socket(self._socket.AF_INET6,
self._socket.SOCK_STREAM)
sock.connect((self.server, 23))
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)
sock.connect((self.server, 23))
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)
sock.connect((self.server, 23))
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)
sock.connect((self.server, 23))
self._raw_data = sock.recv(1024).decode()