Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl on linux,, problem with "exec su" 1

Status
Not open for further replies.

immad2007

Programmer
Nov 9, 2007
5
GB
i m new to tcl. running a script on a linux machine and have got this code in my script

exec su
expect{
-re "ssword:"{
send "password\r"
}
}

the script hangs when it gets to this block of code. ive tried putting an "interact" after "exec su" but get a message that cant interact with the same process.
 
If you want to drive su you need to use spawn.
 
macd68, thanks very much. i had tried 'spawn' before, which works. but the problem is, because of the way in which code is written, using spawn is not possible. i'll have to do massive changes in the code.

is there any way to change to root user without spawning?
or, (this migh not entirely be applicable to this forum) is there a way to grant root permissions to a 'non root' user ?

many thanks
 
Bad design == unworkable solution.
You could use TclX to setuid outside the parent and in child + fork a child to do the pertinent processing..that's all I can think of.
 
macd68, on the note of bad design..i agree. i am working with code written by someone else so cant really do much.

could you elaborate your solution a bit please.
 
I can give you my approach but it may not be a good fit. Tuesday.
 
I can't think of a way to make this work using exec wihtout really torturous effort. Closest is something like:

Code:
package require Expect
exp_internal 1
log_user 0
set timeout 100
if {[catch {set fd [open "| su  - root" "a+"]} err_su_open]} {
    puts "Error: su complains $err_su_open"
    exit
}

puts "Enter password.."
set msg [gets stdin]
if {[catch {set s_id [spawn -open $fd]} err_opn]} {puts "Expect failed to use open descriptor: $err_opn"; exit}
puts "Status: spawn_id = $spawn_id and return = $s_id. File descriptor at $fd with settings = [fconfigure $fd]"
expect -i $spawn_id -re ".*assword.*" {send -i $spawn_id $msg; interact -i $spawn_id}
expect -i $spawn_id -re ".*\n" {send_user "Saw data: $expect_out(buffer)\n"}

Setuid and creating a binary wrapper with forkpty() is really very ugly (that's why expect was created..to do away with the need for this type of thing).
Sorry I don't have a better suggestion, and it's worth noting that using spawn -open like this doesn't solve issues like stdio buffering without tinkering....I didn't get the above to adequately behave.
 
macd68, thanks very much mate. i really appriciate your effort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top