Author Topic: Pinger - A Finished Python Project  (Read 1230 times)

0 Members and 3 Guests are viewing this topic.

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Pinger - A Finished Python Project
« on: January 03, 2016, 12:55:03 am »

Introduction

Last week, I've been programming a Script in Python called "Pinger". What Pinger does, is it imports a plain-text (.txt) file given, where on each line, there's a (different) URL Link. Pinger imports that file and it checks one line after the other, and Pings it into Terminal. Just like you'd type into the Terminal:
Code: [Select]
ping www.google.com
But the problem is, that if you have a list of URLs that you'd like to Ping, with the default Terminal Command, you won't be able to - you can only ping a single URL at a time. Which is why I developed Pinger!

Requirements

In order for this Python Script to work, you must have Python 2.7 (not 3.x)
Usage

To use this Tool, open up the Terminal, cd (Change Directory) to the path of the Script and execute the following Command:

Code: [Select]
python Pinger.py
And that's pretty much all you need to do to get this Tool to work!

To change the URLs that you'd like to Ping, open up "URL_list.txt" - which is located in the same directory as the Python Script - with a simple text editor like Notepad/Leafpad/Text Edit etc. and make any modifications to the URLs that you like. Of course, you can also add/remove any URLs that you wish!

Conclusion

This Python Script took me about a week to develop and I'm excited to finally release it. If you have any issues/questions, you can contact me by posting a Forum Reply down below.

I've also added some lines to my Code, just to make this Tool more "entertaining"!

I've uploaded the file and set it as an Attachment for you all to try it out and let me know what you think! Positive/Negative feedback is much appreciated!


~ Coto
« Last Edit: January 03, 2016, 12:58:13 am by Coto »

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: Pinger - A Finished Python Project
« Reply #1 on: January 03, 2016, 01:12:52 am »
Soooooo it's this?

Code: [Select]
@echo off
for /f "delims=" %%a in (pinglist.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond)
pause
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry

Offline iikibT

  • Serf
  • *
  • Posts: 41
  • Cookies: 7
    • View Profile
Re: Pinger - A Finished Python Project
« Reply #2 on: January 03, 2016, 01:26:51 am »
Hey, I see you are new to programming and probably young so I don't want to discourage you, but this script is pretty much useless.

This being said, you should try to improve this things in your following projects:
  • Remove sleeping (time.sleep()). It is annoying af and you can always avoid it.
  • Avoid asking for agreement each time a script is run. Include the license with you application and don't mention it in the program itself (or at least don't ask people to confirm it each time).
  • Don't hardcode input files. Accept input file with parameter (for example -i "URL_list.txt") or read from stdin (this way users can decide how to pass the URLs - write them, pass them from another app, pass them from a file...)
  • Add '#!/usr/bin/env python' as the first line of your script. This tells the terminal to run this script with python interpreter.

EDIT:
Also, instead of comparing 'enter' variable with each possible mix of upper and lower letters in 'yes', you can just say
Code: [Select]
elif enter.lower() == "yes":This way you can replace your 20+ lines of 'elif's with
Code: [Select]
if enter.lower() in ('y', 'yes'):
  // True
else:
  // False
« Last Edit: January 03, 2016, 01:39:51 am by iikibT »
Hacking for no fun and no profit

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Re: Pinger - A Finished Python Project
« Reply #3 on: January 03, 2016, 01:44:29 am »
Soooooo it's this?

Code: [Select]
@echo off
for /f "delims=" %%a in (pinglist.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond)
pause

I don't know, is this even Python? I'm currently a beginner Python programmer. I don't program other languages at the moment, but that could work in the programming language that it's written in.


Hey, I see you are new to programming and probably young so I don't want to discourage you, but this script is pretty much useless.

This being said, you should try to improve this things in your following projects:
  • Remove sleeping (time.sleep()). It is annoying af and you can always avoid it.
  • Avoid asking for agreement each time a script is run. Include the license with you application and don't mention it in the program itself (or at least don't ask people to confirm it each time).
  • Don't hardcode input files. Accept input file with parameter (for example -i "URL_list.txt") or read from stdin (this way users can decide how to pass the URLs - write them, pass them from another app, pass them from a file...)
  • Add '#!/usr/bin/env python' as the first line of your script. This tells the terminal to run this script with python interpreter.

Thanks for your feedback, the first 2 points you mentioned were mentioned on the Conclusion of my Forum Posts - "I've also added some lines to my Code, just to make this Tool more "entertaining"!"

Not for a reason really, just to test my Python Skills to a bit greater extent.

Apart from the first 2 points, I will definitely make use of all the tips you gave me me for future references! Thanks!

EDIT: Yep, I actually struggled with that a bit - knew something wasn't right, but got that way to work. Will use that tip next time too. Thanks.
« Last Edit: January 03, 2016, 01:45:57 am by Coto »

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Pinger - A Finished Python Project
« Reply #4 on: January 03, 2016, 05:07:25 am »
Code: (Python) [Select]
print '\033[1;32mSOME MESSAGE\033[1;m'
It would be better to replace these with a function and function call:
Code: (Python) [Select]
def print_light_green(str):
    # Unless you've seen it before, "033[1;32m" is probably meaningless. What if you want to change the colour, or don't bother colouring output if using Windows?
    # We use a function here to communicate what it does through its name and make it easier to change due to being in a single place, not to mention remove repetition and 'noise' in the code.
    print '033[1;32m' + str + '\033[1;m'


print_light_green("Hello, world!")
print_light_green("Isn't this much prettier?")

Code: (Python) [Select]
non_blank_count = 0

with open('URL_list.txt') as infp:
    for line in infp:
       if line.strip():
          non_blank_count += 1

fo = open("URL_list.txt", "rw+")
Why are you opening this twice, the second in rw mode?
Code: (Python) [Select]
file = open('url_list.txt')

non_blank_lines = 0
for line in file:
  if line.strip():
    non_blank_lines += 1

file.seek(0, 0) # Go back to beginning of file.
Note how I've given your variables more descriptive names and grouped the blank line calculation code together to show that they're related and doing a specific task, making it easier for a programmer to skim over.

Code: (Python) [Select]
line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),

for x in range(0, URL):
os.system("ping -c 1 %s" % (line)),
line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),
What an odd way to do things. I guess you didn't use for line in file for learning purposes, but why do this in and out of the loop instead of:
Code: (Python) [Select]
for x in range(0, non_blank_lines):
        line = file.readline()
        continue if !file.strip() # Skip if the line is blank :P I think this is valid syntax || Perl has altered my memories.
        print_light_green("Pinging: %s' % (line))
os.system("ping -c 1 %s" % (line))

file.close()

Note that I use the non_blank_lines variable instead of URL because it tells us more about what is contained in the variable, Python tends to prefer lowercase_with_underscores for most variables, and URL was non_blank_lines anyway. Also, it's never a bad thing to close files when we're done.

I don't know, is this even Python?
Batch.
« Last Edit: January 03, 2016, 05:08:02 am by Fur »

Offline khofo

  • EZ's Swashbuckler
  • Knight
  • **
  • Posts: 350
  • Cookies: 25
  • My humor is so black, it could go cotton picking.
    • View Profile
Re: Pinger - A Finished Python Project
« Reply #5 on: January 04, 2016, 05:38:48 am »
Code: (Python) [Select]

enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter == 'Y':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'y':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yeS':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yES':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yEs':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YEs':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'Yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YES':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YeS':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)


This part hurt my eyes so much.
simply convert "enter" to lowercase, and look for it in a list.


Code: (Python) [Select]
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
yes = ["y","yes"]
if enter.lower() in yes:
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)

Or simpler

Code: (Python) [Select]
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter.lower() is "y" or "yes":
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
    time.sleep(1.5)
« Last Edit: January 04, 2016, 05:45:20 am by khofo »
Quote from: #Evilzone
<Spacecow18> priests are bad ppl
<Insanity> Holy crap
Of course God isnt dead. He's out there partying with the Easter Bunny, Santa Clause, Tooth Fairy, and the Man on the moon...
Some of my work: Introduction to Physical Security

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: Pinger - A Finished Python Project
« Reply #6 on: January 04, 2016, 05:20:13 pm »
Code: (Python) [Select]

enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter == 'Y':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'y':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yeS':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yES':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'yEs':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YEs':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'Yes':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YES':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)
elif enter == 'YeS':
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)


This part hurt my eyes so much.
simply convert "enter" to lowercase, and look for it in a list.


Code: (Python) [Select]
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
yes = ["y","yes"]
if enter.lower() in yes:
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
   time.sleep(1.5)

Or simpler

Code: (Python) [Select]
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter.lower() is "y" or "yes":
   print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
    time.sleep(1.5)

Or remove the gay Hacker-Toolkit shit all in all

@OP
That code I put up is batch, bash has a similar way of doing it as well. Why install python and run a skid script when it's one line from batch or the command prompt/terminal.

The only benefit from this script is you trying out your python. As far as a "release" it's worthless. As far as a "hackers-toolkit" that is just beyond gay
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Pinger - A Finished Python Project
« Reply #7 on: January 05, 2016, 08:43:12 am »
He is learning so i will encourage it and let it slide through as he horns his python skills.

One thing though, IF YOU ARE GOING PYTHON ALL THE WAY, DO A PYTHON EQUIVALENT OF THE PING UTILITY.
I am not gonna run this script only to call an external program instead. That way, bash would a be better choice.

If you are going to program in your life, learn version control systems like github or better, we have a gitlab here.
http://git.evilzone.org
Only reason i haven't looked at your code is that i have to download it to my system which i am lazy to do now. SO GIT MOTHAFUCKA!!!:/
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]