Bash scripting with sudo and password prompt, and accepting user input.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash scripting with sudo and password prompt, and accepting user input.
Okay, so I'm creating a bash script to do some tasks for me. I would like the script to be run at a set time of everyday. My first question is if it is possible that if one of the commands in the script requires sudo, is there a way to get around it with out making sudo not require a password. Such as, is there a way to include the password in the script? If that is the case, I can always just set the file as read only by sudo. I've been looking for a way to do this, with no success.
My other question is if I have a command that wants input, how do I give it to the program. For example, if I want to make a zip file that is encrypted, the command would go as:
Code:
zip -r example * -e
now how would I get the script to insert my wanted password.
This script is something that I want to be run under cron, that's why I ask.
I would script it like this:
Compare the time and if it is not time sleep a minute or some seconds in a loop.
for executing programms there you can set a group for that programm and you get a member of it. So there is no need to sudo.
Because the shell that needs the passord is running, your current script is sleeping the while and can not do anything (not submit a password). So there is no possibility to give passwords automaticaly !!!
I would script it like this:
Compare the time and if it is not time sleep a minute or some seconds in a loop.
for executing programms there you can set a group for that programm and you get a member of it. So there is no need to sudo.
Because the shell that needs the passord is running, your current script is sleeping the while and can not do anything (not submit a password). So there is no possibility to give passwords automaticaly !!!
I am sorry Bastl, I don't understand what you are say. This is probably my fault.
My Main problem is the automated user input. I checked out Expect, and I don't know how that can be used in my situation. My example was one was the things I am doing. It was
Code:
zip example -r * -e
That is the code to make a zip file, and encrypt it with a password. I want my script to do this, along with some other file stuff. My problem comes when I want to encrypt the file. I have no way to set what the password will be.
That is the code to make a zip file, and encrypt it with a password. I want my script to do this, along with some other file stuff. My problem comes when I want to encrypt the file. I have no way to set what the password will be.
Then you have to read the manual for your encryption programm that you use for your zip archives.
Maybe you have an option that takes your password, but I think no, because this would be a big security lack.
Then you have to read the manual for your encryption programm that you use for your zip archives.
Maybe you have an option that takes your password, but I think no, because this would be a big security lack.
Well the program that I use is just called zip. It came with Fedora. Normally when you use it just like in a terminal, when you enter the command it asks for the wanted password, asks it again, then the entered password is required to unzip the file. The password that I intend on using is the same as my root password, so maybe that'll help. But all I need is a way to make the zip files require a password with out me entering the password.
I guess fedora uses GnuPG for file encryption.
Like I read does fedora has fundamental encryption mechanisms. Maybe you can use them for your task.
Read also about VPN, SSH tunnel-ing and LUKS.
Your task can get very fedora specific. So you should look what your system setup about encryption is (init, kernel, ...).
Then you can ask more detailed questions.
At the moment it looks like I described it in my first post, that the password request is needed and your script is not running (idle) at that time, so you can not enter a password automatically.
This is because of the high security standard you want, too. You also encrypt your zip archives and that should make sense.
Maybe you want to take zip archives out of that encryption mechanism (if you don't have a full encrypted system (harddisk)).
[QUOTE=Tinkster;4429670]
You add at the end of it NOPASSWD: /path/to/command *
[QUOTE]
I tried that and my name is not mentioned in the file at all. I even opened it up in Gedit and searched it for "jetso" and nothing. I know I am in the sudoer's file though. I use sudo under jetso all the time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.