..using socket and fork with expect and the genric tcl tcp
socket interface?
Here's my code for the server and client, which works fine
when there is an active (t|p)ty open, but dies without.
Server:
socket interface?
Here's my code for the server and client, which works fine
when there is an active (t|p)ty open, but dies without.
Code:
Client:
proc mkTime {} {
return [binary format H* [clock format [clock seconds] -format %m%d%H%M%y]]
}
proc write_log {l} {
global stat start
set fd [open $stat(logfile) a+]
puts $fd "$start - $l"
close $fd
}
proc doSend {} {
global stat sockopen
set badlist {}
foreach hst $stat(connectlist) {
set sockopen [socket $hst $stat(port)]
fconfigure $sockopen -buffering line -blocking 0
if {[catch {puts $sockopen [mkTime]} err_open]} { lappend badlist "$hst - $err_open" }
}
if {[llength $badlist]} {write_log $badlist ; return -1}
return 0
}
#main()
doSend
#doh
Server:
Code:
proc changeTime {tm} {
global expect_out stat
set timeout 25 ; set result -1
spawn -noecho sudo date $tm
expect {
timeout {write_log "Timed out waiting for password" ; set stat(error) 1}
eof {write_log "Read eof from sudo..[clock seconds]..SUCCESS"}
}
catch {close $stat(sock)}
return 0
}
proc handleConnect {sock addr port} {
global stat start
#fill in the stat(e) array
uplevel #0 {init}
set stat(last_recv) [clock format [clock seconds]]
set stat(port) $port
set stat(sock) $sock
set stat(connect_ip) $addr
fconfigure $sock -buffering line -blocking 0 -translation auto
fileevent $sock readable {doRead}
}
proc doRead {} {
global stat
set stat(data) ""
if {$stat(debug) > 0} {parray stat}
set stat(data) [gets $stat(sock)]
puts "Read $stat(data)"
if {[string length $stat(data)] && ![catch {binary scan $stat(data) H* newtime} err_out]} {
puts "$stat(data) read from $stat(sock)"
if {[string length $newtime] == 10} {
set stat(error) 0
return [changeTime $newtime]
} else {
catch {close $stat(sock)}
set stat(error) 1
write_log "Incorrect data format after string length valid: $stat(data)"
return -1
}
} else {
#puts $stat(sock) "Incorrect data format..logged and recorded from $stat(connect_ip)"
set stat(error) 1
catch {close $stat(sock)}
write_log "Incorrect data format after failed init: $stat(data)"
return -1
}
}
#main()
if {[set id [fork]] != 0} {
puts "Closing parent.."
exit
} else {
trap {parray stat} USR1
set srvsock [socket -server handleConnect $stat(port)]
vwait forever
}