-1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • There is no way to do this except by adding a "ping_other_server" interface to servers B and C (or adding a "run_arbitrary_code" interface…) that A can call. – abarnert May 04 '18 at 18:56
  • You'll have more luck asking on serverfault.com. – tdelaney May 04 '18 at 19:42
  • I'd use [tag:paramiko] to run `ping` remotely over an SSH connection. See [this answer](https://stackoverflow.com/a/3586168/8747) for an example. – Robᵩ May 04 '18 at 21:47

2 Answers2

0

You can try this

import os

alive = os.system("ping -c 1 " + "B or C ip")

if alive == 0:
print "up!"
else:
print "down!"

0

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.

Mika72
  • 413
  • 2
  • 12