Re: [ALUG] Exit Script if sudo password not entered
Hi Steve, What's wrong with kicking the script off with sudo? For example: ewan@bluebox:~$ cat dist-upgrade.sh #!/bin/bash sudo apt-get update sudo apt-get dist-upgrade Bad typing: ewan@bluebox:~$ sudo ./dist-upgrade.sh [sudo] password for ewan: Sorry, try again. [sudo] password for ewan: Sorry, try again. [sudo] password for ewan: Sorry, try again. sudo: 3 incorrect password attempts ewan@bluebox:~$ Good typing: ewan@bluebox:~$ sudo ./dist-upgrade.sh [sudo] password for ewan: Hit http://extras.ubuntu.com quantal Release.gpg Hit http://ppa.launchpad.net quantal Release.gpg Hit http://security.ubuntu.com quantal-security Release.gpg Hit http://gb.archive.ubuntu.com quantal Release.gpg Hit http://extras.ubuntu.com quantal Release Hit http://ppa.launchpad.net quantal Release.gpg Hit http://gb.archive.ubuntu.com quantal-updates Release.gpg ...<yada-yada/> Cheers, Ewan On 12 June 2013 12:00, <main-request@lists.alug.org.uk> wrote:
Send main mailing list submissions to main@lists.alug.org.uk
To subscribe or unsubscribe via the World Wide Web, visit http://lists.alug.org.uk/mailman/listinfo/main or, via email, send a message with subject or body 'help' to main-request@lists.alug.org.uk
You can reach the person managing the list at main-owner@lists.alug.org.uk
When replying, please edit your Subject line so it is more specific than "Re: Contents of main digest..."
Today's Topics:
1. Exit Script if sudo password not entered (steve-ALUG@hst.me.uk)
----------------------------------------------------------------------
Message: 1 Date: Tue, 11 Jun 2013 21:16:34 +0100 From: steve-ALUG@hst.me.uk To: ALUG <main@lists.alug.org.uk> Subject: [ALUG] Exit Script if sudo password not entered Message-ID: <51B785A2.20502@hst.me.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi geniuses!
I have a few scripts with several commands prefixed by sudo. I often mistype my password, as it's complex, and, if I mistype it 3 times, I want the script to exit, rather than continue on to the next line and ask for the password again.
Is there a recommended way of doing this? I've googled & thought. One example I've seen asks for the password via zenity, then passes it through to a sudo line which does something like sudo cat /dev/nul
dev/nul and then checks if it failed - if it fails it displays a message and quits. See: http://ubuntuforums.org/showthread.php?t=2052976&page=2
Another alternative I dreamt up whilst reading another webpage was: sudo echo || { echo 'Login Failed'; exit; }
Is there a recommended/preferred/suggested/best way of doing this?
Cheers Steve
------------------------------
_______________________________________________ main@lists.alug.org.uk http://www.anglian.lug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main
End of main Digest, Vol 96, Issue 15 ************************************
On 12 June 2013 12:20, Ewan Slater <ewan.slater@googlemail.com> wrote:
What's wrong with kicking the script off with sudo?
This does make things a bit easier (just use something like whoami to check which user is running the script). However, it means that everything the script does now runs as root, which isn't a great idea unless that's what is required. Using sudo within the script is better, imho. I don't have any suggestions for the best way to abort if sudo fails; the ones Steve mention in the OP seem reasonable solutions at a first glance Otherwise, looking at the sudo manpage says that sudo returns the return code of the successfully run program, or 1 is unsuccessful. So sudo true .. should return 0 on success, 1 on failure, and therefore: sudo true || { echo 'Login Failed'; exit; } .. is how I would handle it, because to me just relying on echo returning 0 (true) isn't as "logical" as using true directly. -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) @ 13 Clarke Rd, Milton Keynes, MK1 1LG
On 12 June 2013 13:07, Mark Rogers <mark@quarella.co.uk> wrote:
Otherwise, looking at the sudo manpage says that sudo returns the return code of the successfully run program, or 1 is unsuccessful.
And looking more at the man sudo, sudo -v can be used to revalidate the login without running a command, so sudo -v || { echo 'Login failed'; exit; } .. is probably the "right" way to do it. If you want to check whether the user already has sudo rights without prompting for a password, you can do that too: sudo -nv || { echo 'Login failed'; exit; } Fascinating what manpages will tell you about commands you use all the time :-) -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) @ 13 Clarke Rd, Milton Keynes, MK1 1LG
On 12/06/13 13:11, Mark Rogers wrote:
On 12 June 2013 13:07, Mark Rogers <mark@quarella.co.uk> wrote:
Otherwise, looking at the sudo manpage says that sudo returns the return code of the successfully run program, or 1 is unsuccessful. And looking more at the man sudo, sudo -v can be used to revalidate the login without running a command, so sudo -v || { echo 'Login failed'; exit; }
.. is probably the "right" way to do it.
If you want to check whether the user already has sudo rights without prompting for a password, you can do that too: sudo -nv || { echo 'Login failed'; exit; }
Fascinating what manpages will tell you about commands you use all the time :-) -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) @ 13 Clarke Rd, Milton Keynes, MK1 1LG
Thanks all. Ewan, kicking off the script with Sudo is fine, provided that you remember to do it. I have a bunch of scripts, some need it, some don't. I won't reliably remember which is which. Cheers Steve
participants (3)
-
Ewan Slater -
Mark Rogers -
steve-ALUG@hst.me.uk