Repo for ping machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
2.6 KiB

from glob import escape
from flask import Flask,request,jsonify
from markupsafe import escape
import os
import requests
import datetime
app = Flask(__name__)
from flask import Flask
app = Flask(__name__)
token = os.environ['TOKEN']
@app.route('/')
def hello():
greet = '<h1>You should not be here!</h1>'
link = '<p><a href="https://www.google.com">Click me!</a></p>'
return greet + link + token
@app.route('/ping/<name>/')
def ping(name):
now = str(datetime.datetime.now())
apiToken = request.args.get('token')
if apiToken == token:
response = os.system("ping -c 1 " + name)
if response == 0:
instruc = 'up'
else:
instruc = 'down'
else:
instruc = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify(
time = now,
status = instruc)
@app.route('/http/<name>/')
def http(name):
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
if apiToken == token:
address = 'http://%s' %name
try:
r = requests.get(address)
status = r.status_code
status_string = '%s' %str(status)
except requests.exceptions.Timeout:
status_string = "Timeout"
except requests.exceptions.TooManyRedirects:
status_string = "TooManyRedirects"
except requests.exceptions.RequestException as e:
status_string = '%s' %str(e)
else:
status_string = 'wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify (
time = now,
status = status_string)
@app.route('/https/<name>/')
def https(name):
apiToken = request.args.get('token')
now = str(datetime.datetime.now())
if apiToken == token:
address = 'https://%s' %name
try:
r = requests.get(address)
status = r.status_code
status_string = '%s' %str(status)
except requests.exceptions.Timeout:
status_string = "Timeout"
except requests.exceptions.TooManyRedirects:
status_string = "TooManyRedirects"
except requests.exceptions.RequestException as e:
status_string = '%s' %str(e)
else:
status_string = ' wrong token'
ip_addr = request.remote_addr
out = '%s - Unauthorized from IP: %s' %(now, ip_addr)
print (out)
return jsonify (
time = now,
status = status_string)
if __name__ == '__main__':
app.run(debug=True)