Skip to content

Instantly share code, notes, and snippets.

@trsqxyz
Last active December 30, 2015 11:29
Show Gist options
  • Save trsqxyz/7822872 to your computer and use it in GitHub Desktop.
Save trsqxyz/7822872 to your computer and use it in GitHub Desktop.
ping command
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import shlex, subprocess
class Ping():
def __init__(self):
self.hosts = {'Google': '173.194.38.82'}
def ping(self, check=[], count=1):
prm = len(self.hosts)
for host in self.hosts:
ip = self.hosts[host]
out, err = self.cmd('ping', '-n', '1', ip)
if not 'TTL' in out:
check.append(host)
else:
print '%d/%d: FINE!' % (count, prm)
count += 1
if count == prm:
print '%d/%d: FINE!' % (count, prm)
opt = raw_input('everything is OK.\n opt: s')
self.show(opt)
return check
def cmd(self, *args):
cmds = [arg for arg in args]
cmd = " ".join(cmds)
args = shlex.split(cmd)
p = subprocess.Popen(
args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, err = p.communicate()
return out, err
def show(self, cmd=''):
if cmd == 's':
for host in self.hosts:
print '%s: %s' % (host, self.hosts[host])
raw_input('Return...')
if __name__ == '__main__':
check = Ping()
done = check.ping()
if done:
for d in done:
print '%s is (x_x)' % d
raw_input('\nCOPY?')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment