#!/opt/exp/bin/expect ################################################################## # File is: class.cgi # # Description: This tcl/expect script is run as a CGI and will # execute a user input command on a remote machine # via telnet, returning the output to the browser. # ################################################################### ################################################################### # # proc: String_Handler # purpose: This proc will take the name and value pairs from the # QUERY_STRING and store in variables. Right now the # only item being sent from the HTML form is the command # to be executed. This is stored in the global variable # action. proc String_Handler {string} { global action set message [split $string &] foreach pair $message { set input [lindex [split $pair =] 0] set val [lindex [split $pair =] 1] if {$input == "action"} { set action [split $val +] } } } ################################################################### # # proc: Cgi_Header # purpose: This proc will output the header information to display # the CGI output in the browser using HTML. # args: title - title for the HTML document # bodyparams - parameters to be placed within the body tag # of the HTML document proc Cgi_Header {title {bodyparams {}}} { puts stdout \ "Content-Type: text/html $title
"
}



###################################################################
#
# proc: Cgi_End
# purpose: This proc will output the HTML to end the output 
#          from the CGI script displayed in the browser. 
# args: none

proc Cgi_End {} { puts "
" } ################################################################### # # proc: Connect # purpose: This proc will telnet to a remote machine and execute # the command provided as a parameter. # args: host - machine to connect to # user - user login on the machine # password - user password on the machine # prompt - system prompt on the machine # command - command to execute proc Connect {host user password prompt command} { # Turn off logging. Don't want show all of the login # process on stdout. log_user 0 # Wait this long (seconds) for each expect statement. set timeout 30 # Check for errors in spawning the telnet program if [catch {spawn telnet $host} err] { send_error "spawn failed: $err\n" close;wait } # Catch all EOFs here, instead of in each expect statement. expect_after { eof {send_error "\nERROR: telnet to $host died unexpectedly!\n" exit 1 } } # Wait for login prompt and then send user login name expect { timeout { send_error "ERROR: no login prompt\n" exit 1 } -nocase "login:" {send "$user\r"} } # Wait for password prompt and then send password expect { timeout { send_error "ERROR: no password prompt!\n" exit 1 } -nocase "password:" {send "$password\r"} } # Wait for system prompt and then send command # NOTE: # Depending on the system additional work may be needed # here. For example, if the .profile on the system # asks for a TERM type or any other input before returing # the system prompt, additional statements are necessary to # handle the interaction. # Turn on logging after sending the command so it # is display at stdout expect { timeout { send_error "\nERROR: TIMED OUT waiting for system prompt\n" exit 1 } $prompt { send "$command\r" log_user 1 } } # Wait for system prompt to indicate command completed and # then send exit expect { timeout { send_error "\nERROR: TIMED OUT waiting for system prompt\n" exit 1 } $prompt {send "exit\r"} } } ################################################################## # # MAIN # ################################################################## global action # Save the QUERY_STRING in a variable to be parsed later set temp $env(QUERY_STRING) # For this implementation, these values are hardcoded. # You would set these values as needed for your application. set host ; # machine to execute command on set user ; # user login on that machine set password ; # user password for that login # Build system prompt to expect. This probably will need to be # changed depending on the system prompt used on your system set prompt "$host:" # Call the proc to handle the QUERY_STRING String_Handler $temp # Call the proc to output the HTML header for the # presentation of the data. Cgi_Header $action {BGCOLOR=ORANGE TEXT=BLUE} puts "Executing $action on $host....\r" # Call the proc to connect to the remote machine and # execute the command Connect $host $user $password $prompt $action # Call the proc to output the HTML tags to end the # presentation of CGI output Cgi_End