Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash Programming Question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
NiceGuy
Guru
Guru


Joined: 12 Jun 2006
Posts: 451
Location: Canada

PostPosted: Thu Feb 01, 2007 6:26 pm    Post subject: Bash Programming Question Reply with quote

Hello all,

I have a generic bash programming question: ... I would like to write a simple bash script for my gentoo machine ... everything is going fine ... but there is one part of my script that needs to execute the "passwd" command.

Now .. when my script reaches that line (the one with the passwd command ... the system will stop ... because the passwd command requires the user to enter their password.

I was wondering if anybody knew how I could automate this .. so that when my script reaches this command (passwd) .. I would not literally have to be their at my desk to enter in my password.

Just for futher reference .. I'm trying to automate a particualr installation of gentoo that I preform quite often
Anyway, iyou help is greatly appreciated.
Thanks and Tke care
Back to top
View user's profile Send private message
JRV
Apprentice
Apprentice


Joined: 10 Jan 2004
Posts: 291

PostPosted: Thu Feb 01, 2007 6:48 pm    Post subject: Re: Bash Programming Question Reply with quote

Hi,

you can do this with the tool "expect" which can talk to interactive programs. I've written my own little expect script for setting passwords automatically:

Code:
spawn passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "password: "
sleep 0.2
send "$password\r"
expect "password: "
sleep 0.2
send "$password\r"
expect eof


You can call it this way:

Code:
expect expect_passwd.exp $uid $passwd
Back to top
View user's profile Send private message
dort
n00b
n00b


Joined: 06 Sep 2003
Posts: 42
Location: NL

PostPosted: Thu Feb 01, 2007 6:50 pm    Post subject: Reply with quote

Have a look at a similar problem:
https://forums.gentoo.org/viewtopic-t-534538-highlight-passwd.html

They use for example a script with the expect command.
http://expect.nist.gov/
Back to top
View user's profile Send private message
aussiemale
n00b
n00b


Joined: 21 Oct 2006
Posts: 32
Location: Melbourne, Australia

PostPosted: Mon Aug 11, 2008 1:07 am    Post subject: Reply with quote

Hi, don't mean to hijack this post but could someone quickly tell me which package contains the send command?
_________________
(IRC: weirdedout)
9 Gentoo Boxes & growing!
Back to top
View user's profile Send private message
BitJam
Advocate
Advocate


Joined: 12 Aug 2003
Posts: 2508
Location: Silver City, NM

PostPosted: Mon Aug 11, 2008 6:37 am    Post subject: Reply with quote

The script that JRV posted was written in the language used by the Expect program. The "send commands" inside that script are part of the language used by Expect. If you want to use Expect you will need to:
Code:
# emerge -av expect
Back to top
View user's profile Send private message
Janne Pikkarainen
Veteran
Veteran


Joined: 29 Jul 2003
Posts: 1143
Location: Helsinki, Finland

PostPosted: Mon Aug 11, 2008 6:59 am    Post subject: Re: Bash Programming Question Reply with quote

NiceGuy wrote:
Hello all,

I have a generic bash programming question: ... I would like to write a simple bash script for my gentoo machine ... everything is going fine ... but there is one part of my script that needs to execute the "passwd" command.

Now .. when my script reaches that line (the one with the passwd command ... the system will stop ... because the passwd command requires the user to enter their password.

I was wondering if anybody knew how I could automate this .. so that when my script reaches this command (passwd) .. I would not literally have to be their at my desk to enter in my password.

Just for futher reference .. I'm trying to automate a particualr installation of gentoo that I preform quite often
Anyway, iyou help is greatly appreciated.
Thanks and Tke care


expect as mentioned in this topic already is a very good tool for script automation; take also a look at autoexpect which AFAIR is bundled with expect. It records the shell commands you enter and saves the results as an expect script, which can be run automatically afterwards.
_________________
Yes, I'm the man. Now it's your turn to decide if I meant "Yes, I'm the male." or "Yes, I am the Unix Manual Page.".
Back to top
View user's profile Send private message
aussiemale
n00b
n00b


Joined: 21 Oct 2006
Posts: 32
Location: Melbourne, Australia

PostPosted: Tue Aug 12, 2008 3:38 am    Post subject: Reply with quote

Thanks, got it. autoexpect looks like it could be very useful but doesn't appear to be part of the expect package.
_________________
(IRC: weirdedout)
9 Gentoo Boxes & growing!
Back to top
View user's profile Send private message
BitJam
Advocate
Advocate


Joined: 12 Aug 2003
Posts: 2508
Location: Silver City, NM

PostPosted: Tue Aug 12, 2008 4:16 am    Post subject: Reply with quote

aussiemale wrote:
Thanks, got it. autoexpect looks like it could be very useful but doesn't appear to be part of the expect package.

You need to set the "doc" USE flag to get autoexpect. I added the following line to /etc/portage/package.use:
Code:
dev-tcltk/expect doc

It's one of 33 example scripts in /usr/share/doc/expect-5.42.1-r1/examples.
Back to top
View user's profile Send private message
aussiemale
n00b
n00b


Joined: 21 Oct 2006
Posts: 32
Location: Melbourne, Australia

PostPosted: Tue Aug 12, 2008 5:15 am    Post subject: Reply with quote

Quote:
You need to set the "doc" USE flag to get autoexpect. I added the following line to /etc/portage/package.use:

Quote:
It's one of 33 example scripts in /usr/share/doc/expect-5.42.1-r1/examples.


Works like a charm :D
_________________
(IRC: weirdedout)
9 Gentoo Boxes & growing!
Back to top
View user's profile Send private message
upengan78
l33t
l33t


Joined: 27 Jun 2007
Posts: 711
Location: IL

PostPosted: Tue Jan 29, 2013 10:49 pm    Post subject: Reply with quote

Sorry to bring this old thread up. Isn't 'autoexpect' included in 'expect' package anymore, even with 'doc' USE flag enabled?

equery f dev-tcltk/expect-5.45
Code:
 * Searching for expect5.45 in dev-tcltk ...
 * Contents of dev-tcltk/expect-5.45:
/usr
/usr/bin
/usr/bin/expect
/usr/include
/usr/include/expect.h
/usr/include/expect_comm.h
/usr/include/expect_tcl.h
/usr/include/tcldbg.h
/usr/lib64
/usr/lib64/expect5.45
/usr/lib64/expect5.45/libexpect5.45.so
/usr/lib64/expect5.45/pkgIndex.tcl
/usr/share
/usr/share/doc
/usr/share/doc/expect-5.45
/usr/share/doc/expect-5.45/ChangeLog.bz2
/usr/share/doc/expect-5.45/FAQ.bz2
/usr/share/doc/expect-5.45/HISTORY.bz2
/usr/share/doc/expect-5.45/NEWS.bz2
/usr/share/doc/expect-5.45/README.bz2
/usr/share/doc/expect-5.45/examples
/usr/share/doc/expect-5.45/examples/README.bz2
/usr/share/doc/expect-5.45/examples/autoexpect.man.bz2
/usr/share/doc/expect-5.45/examples/cryptdir.man.bz2
/usr/share/doc/expect-5.45/examples/decryptdir.man.bz2
/usr/share/doc/expect-5.45/examples/dislocate.man.bz2
/usr/share/doc/expect-5.45/examples/kibitz.man.bz2
/usr/share/doc/expect-5.45/examples/mkpasswd.man.bz2
/usr/share/doc/expect-5.45/examples/multixterm.man.bz2
/usr/share/doc/expect-5.45/examples/passmass.man.bz2
/usr/share/doc/expect-5.45/examples/tknewsbiff.man.bz2
/usr/share/doc/expect-5.45/examples/unbuffer.man.bz2
/usr/share/doc/expect-5.45/examples/xkibitz.man.bz2
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/expect.1.bz2
/usr/share/man/man3
/usr/share/man/man3/libexpect.3.bz2
Back to top
View user's profile Send private message
anybody
n00b
n00b


Joined: 14 Sep 2003
Posts: 32

PostPosted: Fri May 24, 2013 9:12 pm    Post subject: Reply with quote

Unable to find autoexpect too. Doc useflags only installs a manpage file.
Back to top
View user's profile Send private message
BitJam
Advocate
Advocate


Joined: 12 Aug 2003
Posts: 2508
Location: Silver City, NM

PostPosted: Fri May 24, 2013 9:59 pm    Post subject: Reply with quote

The script hosted here (on www.opensource.apple.com) is identical to the autoexpect script that was installed with expect-5.44.1.15 on my system.

You might want to try to find out if there was a good reason for it being removed.
Back to top
View user's profile Send private message
Tyler H
n00b
n00b


Joined: 11 May 2013
Posts: 21

PostPosted: Fri May 24, 2013 10:56 pm    Post subject: Reply with quote

This seems pointless to me. You can use echo and piping to accomplish what you want.
Code:
echo "toor" | passwd
This sends the input "toor" (w/o quotes, of course)[/code] to passwd, thus setting your password to "toor". I've used this to automate logging into an ssh server.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21490

PostPosted: Fri May 24, 2013 11:14 pm    Post subject: Reply with quote

Tyler H wrote:
This seems pointless to me. You can use echo and piping to accomplish what you want.
Code:
echo "toor" | passwd
This sends the input "toor" (w/o quotes, of course)[/code] to passwd, thus setting your password to "toor". I've used this to automate logging into an ssh server.
No, you cannot. Some programs, notably passwd, explicitly ignore stdin and instead read from the controlling terminal. When such a tool is used, expect's ability to create a programmatic controlling terminal is required.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum