PDA

View Full Version : [SOLVED] BASH running expect



Binary-Synapse
May 13th, 2011, 06:54 PM
Hello.

I want to automate a bash script so it does not ask you for a password.
But this time I cannot touch the sudoers file.

The very simple bash script is this one:



#!/bin/bash

command1="./Documents/program/superY"
sudo $command1

rm -f /home/userX/MYscript.sh

exit 0


Some notes:
The script will be run on many equal machines (same hardware and software image).
All machines have the same user passwords.
Let's assume userX pass is something like: ccXdfd729mYv

The problem is that program superY has to run with root privileges.
And I don't want to type the long and complex userX password everytime.

How can I do this?

I thought of program expect (which is installed on all the machines where the script is going to run).

But how can I integrate expect inside my BASH script?

Can someone provide me an example?
I'm no pro and I have seen some ssh examples where expect is used but I didn't understand them quite well (maybe because expect uses Tcl which I never used until now).

Thank you

bashologist
May 15th, 2011, 12:11 AM
Unless I've misunderstood the question would this work?

echo password | sudo -S ./script
sudo -k
Read the manual for more details if that's what you were looking for. Good luck.

Binary-Synapse
May 15th, 2011, 02:00 PM
Hello bashologist.

Your suggestion worked perfectly! Thank you.

Follow-up question:

Can I instruct sudo to not display the following message:

[sudo] password for userX:

I don't want to show that line of text asking for the password in the terminal.
It's only a cosmetic issue, but still...
Is it possible?

Thank you

bashologist
July 1st, 2011, 06:08 PM
Slow response, but better late than never?

echo password | sudo -S ./script 2> /dev/null
Redirect stderr to null.

Binary-Synapse
July 24th, 2011, 07:58 PM
Hello bashologist.

It worked perfectly!!

I want to be your friend. :D

Regards