Skip to content

Commit fad89ba

Browse files
feat : ping function added
1 parent 732bbc8 commit fad89ba

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Orangetool
1616

1717
Consist of some useful script for Orangepi/Raspberrypi boards (Under Development)
18+
Tested on Lubuntu
1819

1920
----------
2021

@@ -27,7 +28,7 @@ By [Moduland Co](http://www.moduland.ir)
2728

2829
- [Download](https://github.com/Moduland/Orangetool/archive/master.zip) source from github
2930

30-
## Functions ##
31+
## IP Functions ##
3132

3233
```python
3334
import orangetool
@@ -39,22 +40,50 @@ local_ip=orange_tool.local_ip() # this function return local ip of board as stri
3940
#2- global_ip
4041

4142
global_ip=orange_tool.global_ip() # this function return global ip of board as string
43+
44+
#3- internet
45+
46+
status=orangetool.internet() #this function check internet connection and return True if internet connection is stable
47+
48+
```
49+
50+
## RAM Functions ##
51+
52+
```python
4253

43-
#3- get_temp
54+
#4- total ram
4455

45-
temp=orangetool.get_temp() # this function return cpu temperature as string
56+
ram=orangetool.ram_total() #this function return total ram of the board
4657

47-
#4- internet
58+
#5- free ram
4859

49-
status=orangetool.internet() #this function check internet connection and return True if internet connection is stable
60+
ram=orangetool.ram_free() # this function return how much ram is available in the board
61+
62+
#6- ram percentage
63+
64+
ram=orangetool.ram_percent() # this function return available ram percentage
65+
66+
```
67+
68+
## Other Functions ##
69+
70+
```python
71+
72+
#7- get_temp
73+
74+
temp=orangetool.get_temp() # this function return cpu temperature as string
75+
76+
#8- uptime
5077

78+
time=orangetool.uptime() # this function return uptime of system
5179

5280
```
5381

5482

5583

5684
- All of the functions in error state return `Error` String
5785
- local_ip() and global_ip() originally are available in ipz package [Link](http://github.com/sepandhaghighi/ipz)
86+
- RAM Functions in this version need psutil package
5887

5988
6089

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
psutil
2+
subprocess
3+
platform
4+
requests
5+

src/orangetool.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import platform
66
import psutil
7+
import multiprocessing as mu
78
ip_pattern=r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
89
api_1="http://ipinfo.io/ip"
910

@@ -188,7 +189,28 @@ def uptime(DEBUG=False):
188189
if DEBUG==True:
189190
print(str(e))
190191
return "Error"
191-
192-
192+
def ping(ip,packet_number=3,DEBUG=False):
193+
'''
194+
This function ping ip and return True if this ip is available and False otherwise
195+
:param ip: target ip
196+
:param packet_number: numer of packet to size
197+
:param DEBUG: Flag for using Debug mode
198+
:type ip :str
199+
:type packet_number:int
200+
:type DEBUG:bool
201+
:return: a boolean value (True if ip is available and False otherwise)
202+
'''
203+
try:
204+
if re.match(ip_pattern,ip)==False:
205+
raise Exception
206+
output=str(list(sub.Popen(["ping",ip,"-c",str(packet_number)],stdout=sub.PIPE,stderr=sub.PIPE).communicate())[0])
207+
if output.find("Unreachable")==-1:
208+
return True
209+
else:
210+
return False
211+
except Exception as e:
212+
if DEBUG==True:
213+
print(str(e))
214+
return "Error"
193215

194216

0 commit comments

Comments
 (0)