2 # This package provides access for lists of Windows network resources.
3 # This preliminary version has no error checking and is Windows-only
4 # Unix version is to be written
7 # Toplevel wrapper. Use
10 # net shares computer ?print?
11 # net glob //computer/share/subdir/wildcard
12 # note - slashes are _forward_
14 set subcommand [lindex $args 0]
15 set args [lrange $args 1 end]
16 if {[lsearch -exact {glob domains computers shares} $subcommand]==-1} {
17 error "Usage: net option ?args? where option is one of domains\
20 uplevel ::smbnet::$subcommand $args
22 namespace eval smbnet {
23 switch -exact $tcl_platform(platform) {
25 #get netbios name of localhost
27 set line [exec nmblookup -A localhost]
28 if {![regexp "\n\t\(\[^ \]+) +<00>" $line match name]} {
29 return -code error "Cannot determine local NETBIOS name"
33 variable my_name [localname]
35 # get netbios name of localhost
37 set line [exec smbclient -N -L $my_name]
38 if {![regexp "\n\tWorkgroup +Master\n\t-+ +-+\n(.*)$" \
40 return -code error "Cannot get workgroups list"
43 foreach elem [split $line \n] {
44 if [regexp "^\t(\[^ \]+) " $elem match domain] {
45 lappend result $domain
51 proc computers {domain} {
53 set line [exec smbclient -N -L $my_name]
54 if {![regexp "\n\tWorkgroup +Master\n\t-+ +-+\n(.*)$" \
56 return -code error "Cannot get workgroups list"
58 foreach elem [split $line \n ] {
59 if [regexp "^\t(\[^ \]+) +(\[^ \]+)" $elem match dom master] {
60 if {![string compare $dom $domain]} {
67 if {![info exists master]} {
68 return -code error "Couldn't get master browser for domain $domain"
70 set line [exec smbclient -N -L $master]
71 if {![regexp "\n\tServer +Comment\n\t-+ +-+\n(.*)\n\n" \
73 return -code error "Cannot get server list from $master"
76 foreach elem [split $line \n] {
77 if {! [string length $elem]} break
78 if [regexp "^\t(\[^ \]+) +(\[^ \]+)" $elem match\
80 lappend result $machine
85 proc shares {computer {sharetype disk}} {
86 set line [exec smbclient -N -L $computer]
87 if {![regexp "\n\tSharename +Type +Comment\n\t-+ +-+ +-+\n(.*)\n\n" \
89 return -code error "Cannot get shares list from $computer"
93 foreach elem [split $line \n] {
94 if {![string length $elem]} break
95 if {[regexp "^\t(.*) +(Disk|Print|IPC )" $elem match name type]
96 && ![string compare [string trim [string tolower $type]] $sharetype]} {
97 lappend result [string trim $name]
103 set list [split $path /]
104 set share [join [lrange $list 0 3] "\\"]
105 set realpath [join [lrange $list 4 end] "/"]
106 set responce [exec smbclient $share << "ls $realpath"]
108 regexp "\nsmb: .>(.*)\n\n" $responce match responce
109 foreach line [split $responce \n] {
110 lappend result [string trim [string range $line 0 \
111 [expr [string length $line] - 36]]]
118 # List SMB domains on current network
120 set domlist [lrange [split [exec net view /domain] "\n"] 4 end]
123 if {[string match "The command completed*" $i]} break
124 lappend final [string trim $i]
128 # list computers in the given domain
129 proc computers domain {
130 set lines [lrange [split [exec net view /domain:$domain] "\n"] 4 end]
133 if [string match "The command completed*" $i] break
134 regexp {^\\\\([^ ]+) } $i match name
139 # list shares of given computer. optionally, sharetype - disk or
140 # print may be specified
141 proc shares {computer {sharetype disk}} {
142 if {$sharetype == "printer"} {
145 set lines [lrange [split [exec net view "\\\\$computer"] "\n"] 4 end]
147 if [string match "The command completed*" $l] break
148 set name ""; set type "";regexp {^(.*) (Print|Disk)} $l\
150 if {[string length $name]&&"$sharetype" == [string tolower $type]} {
151 lappend final [string trim $name]
156 # list files on the network disk. Note - slashes are forward
159 set list [split $path /]
160 if {[llength $list]>4} {
161 set globexp [lindex $list end]
162 if {[regexp {\*\?\[} $globexp]} {
163 set list [lreplace $list end end]
170 set realpath [join $list "\\"]
171 set files [split [exec $env(COMSPEC) /c dir /b $realpath] "\n"]
173 if [string match $globexp $i] {
181 default { error "Smbnet is not implemented for $tcl_platform(platform)" }
184 package provide Smbnet 0.2