Skip to content

Commit 1309d25

Browse files
Merge pull request #50 from Moduland/network_control
Network control
2 parents cb5afb5 + e8e47b5 commit 1309d25

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- `network_control`, `network_enable` and `network_disable` functions
810
### Changed
911
- `sync` parameter added to `wakeup` function
1012
## [0.45] - 2020-12-26

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ orangetool.set_ip("192.168.1.46","eth0") #this function set static ip for syste
135135

136136
mac_dic=orangetool.mac() # return dict of all system net devices mac addresses
137137

138+
#7- network_enable
138139

140+
status=network_enable("eth0") # enable network device
141+
142+
#8- network_disable
143+
144+
status=network_disable("eth0") # disable network device
139145

140146
```
141147

orangetool/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""Orangetool modules."""
33

44
from .orangetool_display import hdmi_on, hdmi_off, hdmi_size
5-
from .orangetool_ip import internet, local_ip, global_ip, set_ip, ping, mac
5+
from .orangetool_ip import internet, local_ip, global_ip, set_ip, ping, mac, network_disable, network_enable
66
from .orangetool_system import check_update, get_temp, uptime, idletime, wakeup, version, sleep, hibernate, halt, restart
77
from .orangetool_ram import ram_total, ram_used, ram_free, ram_percent, freeup
88
from .orangetool_storage import mount_status, storage_status, unmount, unmount_all, mount, usb_on, usb_off

orangetool/orangetool_ip.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,57 @@ def mac(debug=False):
180180
if debug:
181181
print(str(e))
182182
return GENERAL_ERROR_MESSAGE
183+
184+
185+
def network_control(command, device="eth0", debug=False):
186+
"""
187+
Control network adaptor.
188+
189+
:param command: input command
190+
:type command: str
191+
:param device: network device name
192+
:type device:str
193+
:param debug: flag for using debug mode
194+
:type debug:bool
195+
:return: True in successful and False otherwise
196+
"""
197+
try:
198+
cmd = "up"
199+
if command == "down":
200+
cmd = "down"
201+
cmd_out = sub.Popen(["ifconfig", device, cmd],
202+
stderr=sub.PIPE, stdin=sub.PIPE, stdout=sub.PIPE)
203+
output = list(cmd_out.communicate())
204+
if len(output[0]) == 0 and len(output[1]) == 0:
205+
return True
206+
return False
207+
except Exception as e:
208+
if debug:
209+
print(str(e))
210+
return GENERAL_ERROR_MESSAGE
211+
212+
213+
def network_enable(device="eth0", debug=False):
214+
"""
215+
Shortcut to enable network adaptor.
216+
217+
:param device: network device name
218+
:type device:str
219+
:param debug: flag for using debug mode
220+
:type debug:bool
221+
:return: True in successful and False otherwise
222+
"""
223+
return network_control("up", device=device, debug=debug)
224+
225+
226+
def network_disable(device="eth0", debug=False):
227+
"""
228+
Shortcut to disable network adaptor.
229+
230+
:param device: network device name
231+
:type device:str
232+
:param debug: flag for using debug mode
233+
:type debug:bool
234+
:return: True in successful and False otherwise
235+
"""
236+
return network_control("down", device=device, debug=debug)

orangetool/orangetool_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def wakeup(day=0, hour=0, minute=0, sync=True, debug=False):
129129
try:
130130
if sync:
131131
_ = sub.Popen(
132-
["hwclock","-w"],
132+
["hwclock", "-w"],
133133
stderr=sub.PIPE,
134134
stdout=sub.PIPE,
135135
stdin=sub.PIPE)

0 commit comments

Comments
 (0)