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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with scripting the 'su' command

Status
Not open for further replies.

cmeth3

Programmer
Dec 6, 2002
3
US
I'm trying to automate the use of 'su'. I've already put the su command in a shell script, but could not figure out how to include the password for root. Does anyone have a sample script that has su command and maybe the password piped with it?
 
Double Ditto

Maybe you can explain what it is your trying to accomplish and we may have a few tricks up our sleeves that do not include giving away the keys to the city.

Thankx
Doug
 
cmeth3:

The easiest thing to do is to use expect:


Code:
#!/usr/bin/expect

set timeout -1
#exp_internal 1
log_file "/home/$env(USER)/su.log"
set rootprompt "root@[exec uname -n]:.*"
set pass ""

proc getHiddenIn {msg} {
  puts -nonewline $msg
  flush stdout
   
     stty -echo
     set ans [gets stdin]
     stty echo
     return ans
}

spawn -noecho su
        expect {
                -re "Passwor.+" {
                 send "[getHiddenIn]\r"
                 #or send "$pass\r"
                   expect {
                    -re "$rootprompt" {
                   send_user "\nRoot login successful..\n"
                   send "\n\r"
                   send_user "Type \"quit\" to leave\n"
                    interact {
                    "quit" {send_user "\n" ; exit}
                     }
                    }
                 }
                
               -re "^(su:) .+" {
                send_user "Bad password..\n"
                exit
                }
            }
}

If you need an embedded password you can do that
as well, though it is a bad idea.
 
Depending on what you are trying to do you could use sudo and configure it not to use a password for the command you are trying to execute. Are you trying to su to a user or to root.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top