Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ping with output
#1
i have one question related to output file when ping.
if i type in 8.8.8.8 ip address it will export the ping status to the file, but if i used unreachable ip address, my temp file will only display ###result:FAIL###0.

is there any method i can let it print the same as 8.8.8.8

for example if i type in 8.8.8.8 it will print the ping result to temp.txt, but if i type in unreachable ip address it will only display fail.

is there any method i can also display below when type in unreachable ip address.

Pinging 7.7.7.7 with 32 bytes of data:
Request timed out.

i know it related to "TTL=", due to i don't want to display the ping statics

import os
import time
count=0

#hostname = raw_input("enter your ip address(host): ")
hostname = input("enter your ip address(host):")
print ("#################start################")

for i in range (2):
    #ping ip address 
    response = os.system("ping -n 5 " + hostname + " | FIND " + '"TTL=" > tmp.log')
    #response = os.system("ping " + hostname + "> tmp.log")	
    open('tmp.log', 'r').read()


	# reset modem by snmp 
    count = 0
    strCount = str(count)
    time.sleep( 1 )
    if response == 0:
   
        count = count + 1
        strCount = str(count)

        print ("###result:PASS###",count)
        print ("==============================")

        fd = open("tmp.log",'a+')
        fd.write("###result:PASS###")
        fd.write(strCount)
        fd.close()
    else:
        print ("result:FAIL")

        fd = open("tmp.log",'a+')
        fd.write("###result:FAIL###")
        fd.write(strCount)
        fd.close()
     
    break


print ("#################end################")
os.system("pause")
#time.sleep( 5 )
#response = os.system("snmpwalk -cpublic -v 2c 192.168.41.15 1.3.6.1.2.1.69.1.4.5.0 ")
#response = os.system("ping -n 5 " + hostname)
Reply
#2
Don't use os.system for that.

Example

from subprocess import check_output
out = check_output(["ping", "-c 5", "8.8.8.8"])

print("".join(map(chr, out)))
Output:
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=27.3 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=28.2 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=57 time=28.2 ms 64 bytes from 8.8.8.8: icmp_seq=4 ttl=57 time=27.6 ms 64 bytes from 8.8.8.8: icmp_seq=5 ttl=57 time=27.9 ms --- 8.8.8.8 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4006ms rtt min/avg/max/mdev = 27.394/27.886/28.269/0.389 ms
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some guidance on a script to ping a list of ip's cubangt 11 1,825 Aug-10-2023, 02:39 PM
Last Post: snippsat
  non-stop ping script kucingkembar 1 1,359 Aug-23-2022, 06:29 AM
Last Post: menator01
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,212 Aug-12-2021, 06:54 AM
Last Post: tester_V
  Looking for discord bot to make loop ping for address ip tinkode 0 1,824 Jul-26-2021, 03:51 PM
Last Post: tinkode
  Ping command using python 3.6.5 Martin2998 6 17,391 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha
  GPIO high if network IP has good ping duckredbeard 3 2,332 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  Create a program that PING a list of IPs skaailet 7 6,330 Mar-26-2020, 10:46 PM
Last Post: snippsat
  PING PONG GAME akea 0 5,686 May-08-2019, 04:30 PM
Last Post: akea
  Ping Code Problem MTom5 1 2,757 Sep-04-2018, 09:58 PM
Last Post: MTom5
  ping and pong run both well sylas 1 3,168 Sep-24-2017, 05:14 PM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020