help a beginner, expect script, command not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help a beginner, expect script, command not found
# 1  
Old 09-19-2011
help a beginner, expect script, command not found

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

Code:
#!/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 :
Code:
"./MyScript mypassword apt-get_install_git"

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

can anyone help me with that?!

Last edited by gongotar; 09-19-2011 at 07:40 AM..
# 2  
Old 09-19-2011
Quote:
Originally Posted by gongotar
Code:
"./MyScript mypassword apt-get_install_git"

i get following error:
Code:
"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
Code:
apt-get install

or
Code:
apt-get install packagename

# 3  
Old 09-19-2011
when i try
Code:
apt-get install packagename

it works,even if i alter my script as following and run it with the command
Code:
./MyScript mypassword

it works correctly:
Code:
#!/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.
# 4  
Old 09-19-2011
Can you paste your script? Also, do you really need an expect. I mean we can configure sudoers to work without password.

--ahamed
# 5  
Old 09-19-2011
Quote:
Originally Posted by ahamed101
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:
Code:
#!/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.
# 6  
Old 09-19-2011
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
Code:
#!/usr/bin/expect
set pass [lindex $argv 0]
eval spawn sudo -k [lrange $argv 1 end]
expect "assword"
send "$pass\r"
expect eof

Code:
./MyScript mypassword mycommands..



with passwd file (passwd file permission must only readable from owner!)
Code:
#!/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

Code:
./MyScript mycommands..

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 7  
Old 09-25-2011
that solved my problem, i used the one with the passwd file, and i have no problem, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

script under cygwin suddenly says command not found

Running bash in cygwin under 2003 Server. I run about 6 shell windows. Every now and then when I check the machine, all of the scripts are scrolling and returning /usr/bin/whatever: command not found. I checked the event log for a windows event that would cause it, but there was nothing. I... (9 Replies)
Discussion started by: gman804s
9 Replies

3. Shell Programming and Scripting

Expect Script using the split command?

Hello All, I have an Expect Script that ssh'es to a server and does some stuff. In Expect there is a built-in Array Variable called "expect_out(buffer)" which contains all the output from the previous send command up until the expected output. I want to pass the array "expect_out(buffer)" to... (2 Replies)
Discussion started by: mrm5102
2 Replies

4. Shell Programming and Scripting

Can i use if else inside expect command in shell script?

hii,, I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this: "Configure which ports JIRA will use. JIRA requires two TCP ports that are not being used by any other applications on this machine. The HTTP port is where you... (1 Reply)
Discussion started by: nithinfluent
1 Replies

5. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

6. Shell Programming and Scripting

Script using SSH with expect command

Hi all, I want to connect to some host with "ssh". I have googled and got some commands of "expect" and "spawn". I was not aware of these commands and tried below script. $ cat auto.sh set host xx.xx.xx.xx set password abcd@1234 set user root spawn ssh $user@$host expect "*?assword:*"... (4 Replies)
Discussion started by: divya bandipotu
4 Replies

7. Shell Programming and Scripting

Can 'spawn' command be used more than once in an expect script ?

Can 'spawn' script be used more than once in a given expect script ?? What I'm trying to do is, first log-into a remote server through one 'ssh' spawn com and then from there log-into another server using a secod 'ssh' spawn command. But this approach is not working... the second ssh attempt... (1 Reply)
Discussion started by: clakkad
1 Replies

8. Shell Programming and Scripting

Script call : head: command not found

Hi, i am launching a script which open a ssh connexion to a cluster's node. Once on the node, the script calls the problematic command head and wc. I receive a message error as follow which come from two different scripts : line 31: head: command not found line 18: wc: command not found ... (6 Replies)
Discussion started by: masy1800
6 Replies

9. Shell Programming and Scripting

Expect script: obtain the exit code of remote command

Hi all, I'm trying to run the sipp simulator in crontab but after some attempt I came to the conclusion that for some reason this isn't possible (maybe due to sipp interactive nature). This is confirmed by these posts. Now I'm trying to launch sipp from an expect script that runs in crontab. ... (0 Replies)
Discussion started by: Evan
0 Replies

10. Shell Programming and Scripting

Expect: Beginner help with spawn and expect

Hello hello, did a quick search and didn't turn up anything helpful. I've got a book I'm sure has the answers, but I don't think I'll have time to finish it before this all has to work, so here goes~ if this isn't a good place to post and someone could direct me to another forum I'd appreciate... (1 Reply)
Discussion started by: Vryali
1 Replies
Login or Register to Ask a Question