is it possible to ping between two servers/computers from an external server?
example:
we have three servers A,B and C, running the script from A, i want to test the ping between B and C.
is it possible to ping between two servers/computers from an external server?
example:
we have three servers A,B and C, running the script from A, i want to test the ping between B and C.
You can try this
import os
alive = os.system("ping -c 1 " + "B or C ip")
if alive == 0:
print "up!"
else:
print "down!"
I would take this kind of approach (other viable solution with python script could be doing this trough SSH-tunnel from A to B and C). I am bit winging it below, but the code should be quite accurate vs. comments...
Create REST API function /api/ping
in e.g. with Flask in B and C
from flask import json, request
import subprocess
def callPing(ip):
# this returns True|False, but other `subprocess` methods can return more info from called Linux command
if subprocess.check_output(["ping", "-c", "1", ip]):
return "OK"
else:
return "Fail"
@app.route('/ping', methods = ['POST'])
def ping():
ip = str(request.data) # if in POST body, plain
ip = request.json["ip"] # body (f.ex.) {"ip":"127.0.0.1"} and headers has Content-Type: application/json
txt = callPing(ip)
request.headers['Content-Type'] == 'text/plain':
return txt
Send POST request from A to B and/or C
import requests
from json import dumps
targetIP = '8.8.8.8'
serverIP = '127.0.0.1'
data = {'user_name': targetIP }
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
url = "http://"+serverIP+"/api/ping"
r = requests.post(url, headers=headers, data=dumps(data))
Putting up Flask REST API in existing Linux server takes 1-3 man hours, if port 80 is OK for the API.