
Prompting users for passwords in a shell script?How do you write a bash script for example, a user logins to the server's shell, then I want a bash script that will prompt him a password to verify he is a legit user not an intruder. The answer of the password will be located in a file (for example: /etc/verify). If the user not able to type the correct password 3 times the server will kill that connection and bans his IP address from the server. First off, I have to say that while I am a big fan of shell scripts as the universal solution to almost any problem, I am a bit leery about using it as a security screen rather than coding something in C or similar. But what you ask about can certainly be done. The key is to know that you can turn off input echo with the stty command, leading to a simple script snippet to prompt for a password: echo -n "Password: " I've left blank lines so you can see the three line sequence that lets the password not be shown as the user types it in. With this script in your toolkit, you then need to grab the correct password from the /etc/verify file: correct="$(cat /etc/verify)" and then compare the two: if [ $password = $correct ] ; then ... If it fails, increment a counter: failed=$(( $failed + 1 )) Put those pieces together and you'll have everything except the action that should happen when they fail three times in a row. To log someone out, you can simply kill their login shell, which can be quickly identified by finding the parent process ID of the script itself, which is typically the third field in a ps -l output. To block their IP, I assume you'd need to automatically append the IP address to some sort of firewall, but since there are a number of different firewalls, you're on your own with that last one. Hope that's helpful. I'll leave putting all these building blocks together as an "exercise for the reader". :-)
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 4267)
Tagged: Previous: How do I remove widgets from Dashboard in Mac OS X Tiger? Next: Sony Playstation Portable (PSP) Top Questions and Answers Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. hello Hi Dave, Thanks for that tip, very handy. Get a question and also a possibly useful addition... Q: You mention using /etc/verify, is this a standard *NIX/Linux thing or is it just by way of example here? The reason I ask is that I work almost entirely on OS X, where it doesn't seem to exist (I assume because of NetInfo/OpenLDAP or because it just don't), is there some kind of equivalent on OS X if it is not just an example? Addittion: I noticed that somebody else using a similar technique to you pointed out that if stty was not configured to echo before the script it would be after using this method. They suggested using the following construct: - oldmodes=`stty -g` Cheers You can turn the echoing off by giving -s option to the read command # read -s password Posted by: Ratnadeep Joshi at September 4, 2007 5:33 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|