-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbreak5.rb
More file actions
77 lines (51 loc) · 1.59 KB
/
break5.rb
File metadata and controls
77 lines (51 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def comment_lines(host_path, break_start_tag, break_end_tag)
host_content = File.read(host_path)
break_content = host_content[/#{break_start_tag}(.*?)#{break_end_tag}/m, 1].strip
break_lines = break_content.split("\n")
found_lines_to_comment = false
break_lines.collect! do |line|
if line.length > 0 and line[0] != "#"
found_lines_to_comment = true
line = "#"+line
end
end
if found_lines_to_comment
new_break_content = break_lines.join("\n")
host_content.sub! break_content, new_break_content
File.write(host_path, host_content)
return true
end
return false
end
def un_comment_lines(host_path, break_start_tag, break_end_tag)
host_content = File.read(host_path)
break_content = host_content[/#{break_start_tag}(.*?)#{break_end_tag}/m, 1].strip
break_lines = break_content.split("\n")
found_lines_to_un_comment = false
break_lines.collect! do |line|
if line.length > 0 and line[0] == "#"
found_lines_to_un_comment = true
line = line[1..-1]
end
end
if found_lines_to_un_comment
new_break_content = break_lines.join("\n")
host_content.sub! break_content, new_break_content
File.write(host_path, host_content)
return true
end
return false
end
type = ARGV.length > 0 ? ARGV[0] : "break"
break_length = ARGV.length == 2 ? ARGV[1].to_i : 5
if type == "break"
comment_lines("/etc/hosts", "#break", "#/break")
sleep(break_length * 60)
un_comment_lines("/etc/hosts", "#break", "#/break")
end
if type == "lock"
un_comment_lines("/etc/hosts", "#break", "#/break")
end
if type == "unlock"
comment_lines("/etc/hosts", "#break", "#/break")
end