help a beginner, expect script, command not found

hi,
i have a problem with my expect script, here is MyScript:

#!/usr/bin/expect
set pass [lindex $argv 0]
set c [lindex $argv 1]
set command [string map {"_" " "} $c]
spawn sudo $command
expect "assword"
send "$pass\r"
expect eof 

My problem is that when i execute MyScript with the command :

"./MyScript mypassword apt-get_install_git"

i get following error:

"spawn sudo apt-get install git
[sudo] password for gongotar: 
sudo: apt-get install git: command not found"

can anyone help me with that?!

how about `apt-get_install_git`?
try only this

apt-get install

or

apt-get install packagename

when i try

apt-get install packagename

it works,even if i alter my script as following and run it with the command

./MyScript mypassword

it works correctly:

#!/usr/bin/expect 
set pass [lindex $argv 0] 
spawn sudo apt-get install git 
expect "assword" 
send "$pass\r" 
expect eof

but what i want is an script that get my command (for example: apt-get install git) and execute it as root, i dont know how to do this.

Can you paste your script? Also, do you really need an expect. I mean we can configure sudoers to work without password.

--ahamed

i've already inserted my script in my first post in this thread, but here is my script again:

#!/usr/bin/expect 
set pass [lindex $argv 0] 
set c [lindex $argv 1] 
set command [string map {"_" " "} $c] 
spawn sudo $command 
expect "assword" 
send "$pass\r" 
expect eof

and yes,i need an script, because im using this script in my java code, and i can not execute commands as root in there without scripts, i need to execute commands such as "route add ... " or "route del ..." , and these commands need to be executed as root.

Firstly use clear password is very unscure!.you may must use just commands in shell without expect.or you can use sudo with NOPASSWD.
Even so i try to simple expect examples for your goal.

!! with clear passwd

#!/usr/bin/expect
set pass [lindex $argv 0]
eval spawn sudo -k [lrange $argv 1 end]
expect "assword"
send "$pass\r"
expect eof
./MyScript mypassword mycommands..

with passwd file (passwd file permission must only readable from owner!)

#!/usr/bin/expect
set filer [open "/home/xxx/.xfile" r]
set x [read $filer]
eval spawn sudo -k [lrange $argv 0 end]
expect "assword"
send "$x\r"
expect eof
./MyScript mycommands..

regards
ygemici

1 Like

that solved my problem, i used the one with the passwd file, and i have no problem, thanks