Example #1
0
def ping(host):
    param = "-n" if system_name().lower() == "windows" else "-c"
    command = ["ping", param, "1", host]
    if system_name().lower() == "windows":
        retcode = system_call(command, creationflags=0x00000008)
        return retcode == 0
    else:
        retcode = system_call(command)
Example #2
0
def clear():
    """
    Limpa a tela comparando sistema operacional
    """
    from platform import system as system_name
    from subprocess import call as system_call

    command = 'cls' if system_name().lower() == 'windows' else 'clear'
    system_call([command])
Example #3
0
def clear_screen():
    """
    Clears the terminal screen.
    """

    # Clear screen command as function of OS
    command = 'cls' if system_name().lower() == 'windows' else 'clear'

    # Action
    system_call([command])
Example #4
0
def ping(host):
    fn = open(os.devnull, 'w')
    param = '-n' if system_name().lower() == 'windows' else '-c'
    command = ['ping', param, '1', host]
    retcode = system_call(command, stdout=fn, stderr=subprocess.STDOUT)
    fn.close()
    return retcode == 0
Example #5
0
def ICMP_Ping_Flood(host, cmds):
    """
	Sends a flood of pings n pings set by amount using the ICMP protocols.
	Remember that a host may not respond to a ping (ICMP) request even if 		the host name is valid.
	"""
    # defaults amount to 1 if user didn't specify an amount of pings
    try:
        amount = int(cmds[2])
    except IndexError:
        amount = 1
    except Exception as e:
        print('in ping flood: ')
        print('something was wrong with arguments: ', cmds)
        print('\n', e)
        return
    try:
        amount = int(amount)
        # Ping command count option as function of OS
        param = '-n' if system_name().lower() == 'windows' else '-c'
        for i in range(0, amount):
            # Building the command. Ex: "ping -c 1 google.com"
            # <cmd> <os> <num packets> <hosts>
            command = ['ping', param, '1', host]
            # Pinging
            print(system_call(command))
    except Exception as e:
        print('something went wrong in ping flood ', e)
        print('host and number of pings set: ', cmds)
Example #6
0
def pinghost(host):
    try:
        output = str(
            subprocess.Popen(["ping.exe", host],
                             stdout=subprocess.PIPE).communicate()[0])
        if 'unreachable' in output:
            log_error('!!!!!' + host + ' is unreachable!!!!!')
            return False
        else:
            log_info(host + ' is online')
            return True
    except Exception as error:
        log_warn('Current Machine is not running Windows-based OS: ' +
                 str(error))
        # Ping command count option as function of OS
        param = '-n' if system_name().lower() == 'windows' else '-c'
        # Building the command. Ex: "ping -c 1 phlamtecdb-a"
        command = ['ping', param, '1', host]
        # Pinging
        if system_call(command) == 0:
            log_info(host + ' is online')
            return True
        else:
            log_error('!!!!!' + host + ' is OFFLINE!!!!!')
            return False
Example #7
0
def ping(host):
    # Ping command count option as function of OS
    param = '-n' if system_name().lower() == 'windows' else '-c'
    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]
    # Pinging
    return system_call(command) == 0
Example #8
0
def ping(host):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    # Ping command count option as function of OS
    param = '-n' if system_name().lower() == 'windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    # Pinging
    if system_name().lower() == 'windows':
        return system_call(command, stdout=open('nul')) == 0
    else:
        return system_call(command, stdout=open('/dev/null')) == 0
Example #9
0
def ping(host):

    # Ping command count option as function of OS
    param = '-n 1' if system_name().lower() == 'windows' else '-c 1'

    # Building the command. Ex: "ping -c 1 or ping -n 1"
    command = "ping " + param + " " + host
    # Pinging
    return system_call(command) == 0
Example #10
0
def domain_up(domain):
    # Ping command count option as function of OS
    param = '-n 1' if system_name().lower() == 'windows' else '-c 1'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, domain]

    # Pinging
    return system_call(command) == 0
Example #11
0
def send_ping(host):
    if system_name().lower() == 'windows':
        # command = ['ping', '-n', '1', host]
        if not send_ping_win(host):
            return False
    else:

        # Check if ping error (r==0=>ok;r==2=>no_reponse;else=>failed)
        command = ['ping', '-c', '1', host]
        if system_call(command, stdout=open(os.devnull, 'wb')) != 0:
            return False
    return True
Example #12
0
    def ping(host):
        """
        A method that replicates the command line ping utility.

        :param host: Host to ping to
        :return: A boolean type that means if the ping reaches the destination or not
        """
        ret = None
        # Ping command count option as function of OS
        param = '-n' if system_name().lower() is 'windows' else '-c'
        # Building the command. Ex: "ping -c 1 google.com"
        command = ['ping', param, '1', host]
        with open("/dev/null", "w+") as f:
            ret = system_call(command, stdout=f) == 0

        return ret
Example #13
0
    def ping(host):
        try:
            from platform   import system as system_name  # Returns the system/OS name
            from subprocess import call   as system_call  # Execute a shell command
        except ImportError:
            print "ERROR IMPORTING PING TOOLS"
            return False


        # Ping command count option as function of OS
        param = '-n 1' if system_name().lower()=='windows' else '-c 1'

        # Building the command. Ex: "ping -c 1 google.com"
        command = ['ping', param, host]

        # Pinging
        return system_call(command) == 0
Example #14
0
    def check_device_up(self):
        """
            Returns True if host (str) responds to a ping request.
            Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
            """

        # Ping command count option as function of OS
        param1 = '-n' if system_name().lower() == 'windows' else '-c'
        param2 = '-w' if system_name().lower() == 'windows' else '-W'
        # Building the command. Ex: "ping -c 1 google.com"
        command = ['ping', param2, '200', param1, '2', self.ip]

        if system_name().lower() == 'windows':
            self.device_up = system_call(command, shell=True) == 0
        else:
            self.device_up = os.system(f"ping -c 2  -W 1 {self.ip} ") == 0
        # Pinging
        return self.device_up
Example #15
0
def update_status(request, flag):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """
    header = flag

    if header == 'linux':
        cls = Linux
    elif header == 'windows':
        cls = Windows
    else:
        cls = Network

    ip_list = cls.objects.all().values_list('ip_address')

    template = 'inventory/index.html'

    # Ping command
    param = '-n'

    print("Started Updating")

    for host in ip_list:
        # Build command
        command = ['ping', param, '1', host]
        # Ping, stores True or False in val
        val = str(system_call(command) == 0)

        cls.objects.filter(ip_address=host[0]).update(status=val)
        # obj = cls.objects.get(ip_address=host)
        # obj.status = val
        # obj.save()
        print(str(host[0]))
        print(header)
    items = cls.objects.all()

    context = {
        'products': items,
        'flag': header
    }

    return render(request, template, context)
Example #16
0
def ping_host(host, hostname, count=10, delay=1):
    """
    Print response message if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP)
    request even if the host name is valid.

    If the count number is 0, it means endless ping.
    """
    if count == 0:
        command = ['ping', host]
    else:
        param = '-c %s' % count
        command = ['ping', param, host]

    while True:
        result = system_call(command, stdout=DEVNULL)
        if result == 0:
            print("%s %s: Responded" % (hostname, host))
        else:
            print("%s %s: Responded None" % (hostname, host))
def ping(host):
    """
    Credit: https://stackoverflow.com/a/32684938/5460704
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    # Ping command count option as function of OS
    param = '-n' if system_name().lower() == 'windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = [
        'ping',
        param,
        '1',
        host,
    ]

    # Pinging
    return system_call(command, stdout=open(os.devnull, 'wb')) == 0
Example #18
0
def ping(host):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    from platform import system as system_name  # Returns the system/OS name
    from subprocess import call as system_call  # Execute a shell command
    from os import devnull

    # Ping command count option as function of OS
    param = '-n' if system_name().lower() == 'windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    # Pinging
    with open(devnull, "w") as devnull_f:
        result = system_call(command, stdout=devnull_f)
    return result == 0
Example #19
0
 async def ping_adress(self, ctx, ip):
     packages = 30
     wait = 0.3
     try:
         try:
             m = await ctx.send(f"Pinging {ip}...")
         except:
             m = None
         t1 = time.time()
         param = '-n' if system_name().lower() == 'windows' else '-c'
         command = ['ping', param, str(packages), '-i', str(wait), ip]
         result = system_call(command) == 0
     except Exception as e:
         await ctx.send("`Error:` {}".format(e))
         return
     if result:
         t = (time.time() - t1 - wait*(packages-1))/(packages)*1000
         await ctx.send("Pong ! (average of {}ms per 64 byte, sent at {})".format(round(t, 2), ip))
     else:
         await ctx.send("Unable to ping this adress")
     if m is not None:
         await m.delete()
Example #20
0
 async def ping_adress(self,ctx,ip):
     packages = 40
     wait = 0.3
     try:
         try:
             m = await ctx.send("Ping...",file=await self.bot.cogs['UtilitiesCog'].find_img('discord-loading.gif'))
         except:
             m = None
         t1 = time.time()
         param = '-n' if system_name().lower()=='windows' else '-c'
         command = ['ping', param, str(packages),'-i',str(wait), ip]
         result = system_call(command) == 0
     except Exception as e:
         await ctx.send("`Error:` {}".format(e))
         return
     if result:
         t = (time.time() - t1 - wait*(packages-1))/(packages)*1000
         await ctx.send("Pong ! (average of {}ms per 64 byte, sent at {})".format(round(t,2), ip))
     else:
         await ctx.send("Unable to ping this adress")
     if m!=None:
         await m.delete()
Example #21
0
def Ping(host):
    from platform import system as system_name  # Returns the system/OS name
    from subprocess import call as system_call  # Execute a shell command

    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP)
    request even if the host name is valid.
    """

    if 'http' in host:
        host = host.split('//')[1]

    if '/' in host:
        host = host.split('/')[0]

    # Ping command count option as function of OS
    param = '-n' if system_name().lower() == 'windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    # Pinging
    return system_call(command, stdout=False) == 0
Example #22
0
def ping(host):
    param = "-n" if system_name().lower() == "windows" else "-c"
    return ensure(
        lambda: system_call(["ping", param, "1", host], stdout=DEVNULL) == 0)
Example #23
0
def ping(host):
    param = ' -n 1 -w 1000 ' if system_name().lower(
    ) == 'windows' else ' -c 1 -W 1 '
    #command = ['ping', param, host]
    command = 'ping' + param + host
    return system_call(command) == 0
Example #24
0
def ping(host):
    """
    Pings host string 
    """
    command = ['ping', '-c', '1', host]
    return system_call(command) == 0
Example #25
0
    def clear_screen():

        command = 'cls' if system_name().lower() == 'windows' else 'clear'

        system_call([command])
Example #26
0
def ping(host):
    #Currently not being used
    param = '-n' if system_name().lower()=='windows' else '-c'
    command = ['ping', param, '1', host]
    return system_call(command) == 0
Example #27
0
def connect(ip, port, gateway):
    cmd_iptables = [
        "iptables", "-t", "mangle", "-I", "OUTPUT", "-m", "owner",
        "--gid-owner", GID, "-j", "MARK", "--set-mark", GID
    ]
    # TODO: use pyroute2
    cmd_rule = ["ip", "rule", "add", "fwmark", GID, "table", GID]
    cmd_route = [
        "ip", "route", "add", ip + "/32", "via", gateway, "table", GID
    ]

    system_call(cmd_iptables, stdout=DEVNULL)
    system_call(cmd_rule, stdout=DEVNULL)
    system_call(cmd_route, stdout=DEVNULL)

    def can_connect():
        try:
            s = socket()
            s.settimeout(2)
            s.connect((ip, port))
            s.close()
            return True
        except OSError as e:
            traceback.print_exc()
            s.close()
            print("Error {error} while connecting to {ip}:{port} via {gw}".
                  format(error=e.errno, ip=ip, port=port, gw=gateway),
                  file=sys.stderr,
                  flush=True)
            return False

    result = ensure(can_connect)

    cmd_iptables[3] = "-D"
    cmd_rule[2] = "del"
    cmd_route[2] = "del"

    system_call(cmd_iptables, stdout=DEVNULL)
    system_call(cmd_rule, stdout=DEVNULL)
    system_call(cmd_route, stdout=DEVNULL)

    return result
Example #28
0
def ping(host):
    """Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid."""
    param = '-n' if system_name().lower() == 'windows' else '-c'
    command = ['ping', param, '1', host]
    return system_call(command) == 0
def ping_host(host):
    param = '-n' if system_name().lower() == 'windows' else '-c'
    command = ['ping', param, '1', host]
    return system_call(command) == 0