View previous topic :: View next topic |
Author |
Message |
NiceGuy Guru
Joined: 12 Jun 2006 Posts: 451 Location: Canada
|
Posted: Thu Feb 01, 2007 6:26 pm Post subject: Bash Programming Question |
|
|
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 |
|
|
JRV Apprentice
Joined: 10 Jan 2004 Posts: 291
|
Posted: Thu Feb 01, 2007 6:48 pm Post subject: Re: Bash Programming Question |
|
|
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 |
|
|
dort n00b
Joined: 06 Sep 2003 Posts: 42 Location: NL
|
|
Back to top |
|
|
aussiemale n00b
Joined: 21 Oct 2006 Posts: 32 Location: Melbourne, Australia
|
Posted: Mon Aug 11, 2008 1:07 am Post subject: |
|
|
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 |
|
|
BitJam Advocate
Joined: 12 Aug 2003 Posts: 2508 Location: Silver City, NM
|
Posted: Mon Aug 11, 2008 6:37 am Post subject: |
|
|
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 |
|
|
Janne Pikkarainen Veteran
Joined: 29 Jul 2003 Posts: 1143 Location: Helsinki, Finland
|
Posted: Mon Aug 11, 2008 6:59 am Post subject: Re: Bash Programming Question |
|
|
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 |
|
|
aussiemale n00b
Joined: 21 Oct 2006 Posts: 32 Location: Melbourne, Australia
|
Posted: Tue Aug 12, 2008 3:38 am Post subject: |
|
|
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 |
|
|
BitJam Advocate
Joined: 12 Aug 2003 Posts: 2508 Location: Silver City, NM
|
Posted: Tue Aug 12, 2008 4:16 am Post subject: |
|
|
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 |
|
|
aussiemale n00b
Joined: 21 Oct 2006 Posts: 32 Location: Melbourne, Australia
|
Posted: Tue Aug 12, 2008 5:15 am Post subject: |
|
|
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 _________________ (IRC: weirdedout)
9 Gentoo Boxes & growing! |
|
Back to top |
|
|
upengan78 l33t
Joined: 27 Jun 2007 Posts: 711 Location: IL
|
Posted: Tue Jan 29, 2013 10:49 pm Post subject: |
|
|
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 |
|
|
anybody n00b
Joined: 14 Sep 2003 Posts: 32
|
Posted: Fri May 24, 2013 9:12 pm Post subject: |
|
|
Unable to find autoexpect too. Doc useflags only installs a manpage file. |
|
Back to top |
|
|
BitJam Advocate
Joined: 12 Aug 2003 Posts: 2508 Location: Silver City, NM
|
Posted: Fri May 24, 2013 9:59 pm Post subject: |
|
|
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 |
|
|
Tyler H n00b
Joined: 11 May 2013 Posts: 21
|
Posted: Fri May 24, 2013 10:56 pm Post subject: |
|
|
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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21490
|
Posted: Fri May 24, 2013 11:14 pm Post subject: |
|
|
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 |
|
|
|