-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunix-1.2.0.tm
126 lines (120 loc) · 3.41 KB
/
unix-1.2.0.tm
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# While tuapi is what we ideally want to use, we fallback to running
# exec when tuapi is not available.
#
# 10/14 - use tuapi to discover iface if needed
#
catch { package require tuapi }
package require fileutil
package require ensembled
namespace eval unix { ensembled }
if 0 {
@ unix platform
| Simplified platform handling by parsing and returning
| a simple enumeration based on the os/platform.
@returns {?osx|linux?}
}
proc ::unix::platform {} {
switch -- $::tcl_platform(platform) {
unix {
switch -nocase -- $::tcl_platform(os) {
darwin {
return osx
}
linux - default {
return linux
}
}
}
default {
# This is not a unix platform
return
}
}
}
if 0 {
@ unix restart
| Restart the system using the [shutdown -r now] command
@arg delay {entier}
An argument giving a ms delay before conducting the restart.
Defaults to 5000 ms.
}
proc ::unix::restart { {delay 5000} } {
after $delay {
if { [catch { ::tuapi::syscall::reboot }] } {
try {
exec -- shutdown -r now
} on error {result options} {
puts stderr $result
}
}
}
}
if 0 {
@ unix get
| Capture various data from the unix system. Generally returns
| raw data that should then be parsed and handled as needed.
@arg what {ps|route|load|mem|uptime|mount|arp|fdisk|nameservers|stat|hostname|mac}
@args {mixed}
additional arguments to provide the requested command when
specified.
@returns {mixed}
}
proc ::unix::get { what args } {
switch -nocase -glob -- $what {
ps { set cmd [list ps {*}$args] }
route { set file /proc/net/route }
load* { set file /proc/loadavg }
mem* { set file /proc/meminfo }
upt* { set file /proc/uptime }
mount* { set file /proc/mounts }
arp { set file /proc/net/arp }
fdisk { set cmd [list fdisk -l {*}$args] }
ns - nameserv* { set file /etc/resolve.conf }
stat - procst* { set file /proc/stat }
hostname - host* {
switch -- [unix platform] {
osx {
return [info hostname]
}
linux {
try {
return [::tuapi::syscall::hostname]
} on error {result options} { return [info hostname] }
}
}
}
mac - hwaddr {
switch -- [unix platform] {
osx {
lassign $args iface
if { $iface eq {} } { set iface en0 }
set cmd [list ifconfig $iface | awk "/ether/{print \$2}"]
}
linux {
lassign $args iface
if {![catch {package require tuapi}]} {
if {$iface eq {}} {
set ifaces [::tuapi::syscall::ifconfig]
set ifaces [lsearch -inline -all -not -exact $ifaces lo]
set iface [lindex $ifaces 0]
}
return [dict get [::tuapi::syscall::ifconfig $iface] hwaddr]
} else {
if { $iface eq {} } { set iface eth0 }
set file [file join / sys class net ${iface} address]
}
}
}
}
default {
throw UNIX_GET_DEFAULT "\[unix_system\] - sysread doesnt know how to get $what $args"
}
}
if { [info exists cmd] } {
return [string trim [exec -ignorestderr -- {*}$cmd]]
} elseif { [info exists file] && [file isfile $file] } {
return [string trim [::fileutil::cat $file]]
} else {
throw UNIX_GET_FAILURE "Failed to Read Unix System $what $args"
}
}