-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipnow.hs
90 lines (71 loc) · 2.86 KB
/
ipnow.hs
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
78
79
80
81
82
83
84
85
86
87
88
89
module Main (
main, start
) where
-- Systemwide imports
import Graphics.UI.Gtk.MenuComboToolbar.ImageMenuItem
import Graphics.UI.Gtk.Abstract.Box as ABox
import Graphics.UI.Gtk.Multiline.TextBuffer
import Graphics.UI.Gtk.Multiline.TextView
import Graphics.UI.Gtk.Display.Image
import Graphics.UI.Gtk.Entry.Entry
import Graphics.UI.Gtk.Buttons.Button
import Graphics.UI.Gtk.Builder
import Graphics.UI.Gtk
import Data.Maybe
import Graphics.UI.Gtk.General.RcStyle
import System.IO
import System.Cmd
import System.Process
--local imports
-- ############# FUNCTIONS ##############
getnsetip interface ipfirstbytes snmask = do
ipfirsttext <- entryGetText ipfirstbytes
let ip_string = (ipfirsttext)
putStrLn $ "IP string:" ++ ip_string
mask_string <- entryGetText snmask
setip interface ip_string mask_string
setip interface ip snmask = do
--execute something like ifconfig eth0 192.168.1.45 up
let ifdown_cmd_string = "ifconfig " ++ interface ++ " down"
let cmd_string = "ifconfig " ++ interface ++ " " ++ ip ++ " up"
putStrLn $ "cmd string: " ++ cmd_string
_ <- system $ "ifconfig " ++ interface
_ <- system $ ifdown_cmd_string
_ <- system $ cmd_string
--re-add the gateway, because it was removed
let add_gateway_cmd_string = "route add default gw 192.168.1.1 " ++ interface
_ <- system $ add_gateway_cmd_string
_ <- system $ "ifconfig " ++ interface
putStrLn $ "Tried to set IP:" ++ ip ++ " Subnet:" ++ snmask
createMainGUI = do
--load gladefile
builder <- builderNew
builderAddFromFile builder "./gtk/ipnow.glade"
--getEntryfields
ipfirstbytes <- builderGetObject builder castToEntry "ipfirstbytes"
entrySetWidthChars ipfirstbytes 14 --11
snmask <- builderGetObject builder castToEntry "snmask"
entrySetWidthChars snmask 12
--set defaults
let interface = "eth0"
interfacelable <- builderGetObject builder castToLabel "interface_name_lbl"
labelSetText interfacelable interface
--get current ip and set
current_ip <- readProcess "./currentifconfig.sh" [interface,"ip"] []
current_snmask <- readProcess "./currentifconfig.sh" [interface, "subnet"] []
entrySetText ipfirstbytes current_ip
entrySetText snmask current_snmask
--register event for set button
setbtn <- builderGetObject builder castToButton "setbtn"
onClicked setbtn $ getnsetip interface ipfirstbytes snmask
--window managment
main_window <- builderGetObject builder castToWindow "main_window"
onDestroy main_window mainQuit
widgetShowAll main_window
-- ######### MAIN ##############
start= main
main = do
--rcParse "../gtk/gtkrc-2.0-ipnow" -- STYLE if wished
initGUI -- GTK INIT
main_gui <- createMainGUI -- GUI CONSTRUCTION
mainGUI -- START