LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2011, 04:51 AM   #1
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Rep: Reputation: 2
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 am running Fedora 15.
 
Old 07-30-2011, 06:32 AM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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 !!!
 
Old 07-30-2011, 07:36 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If you are looking for question and answer type scripting you can have a look at expect.
 
Old 07-30-2011, 12:36 PM   #4
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by bastl View Post
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.
 
Old 07-30-2011, 02:13 PM   #5
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
Quote:
Originally Posted by Jetso View Post
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.
 
Old 07-30-2011, 02:43 PM   #6
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by bastl View Post
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.
 
Old 07-30-2011, 06:12 PM   #7
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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)).
 
Old 07-30-2011, 07:15 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'd simply add the one command you wish to run as a different user (root?)
to your sudoers file w/ a NOPASSWD:


Plain and elegant.


Cheers,
Tink
 
Old 07-30-2011, 08:01 PM   #9
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by Tinkster View Post
I'd simply add the one command you wish to run as a different user (root?)
to your sudoers file w/ a NOPASSWD:


Plain and elegant.


Cheers,
Tink
Okay, and how would one go about doing that? Sorry if it sounds stupid.
 
Old 07-30-2011, 10:51 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Okay, and how would one go about doing that? Sorry if it sounds stupid.
You type:
visudo
You find the line that describes you.
You add at the end of it NOPASSWD: /path/to/command *
You save.
You enjoy.


Cheers,
Tink
 
Old 07-31-2011, 12:23 AM   #11
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Original Poster
Rep: Reputation: 2
[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.
 
Old 07-31-2011, 02:05 AM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What about group memberships?
 
Old 07-31-2011, 02:10 AM   #13
Jetso
Member
 
Registered: Sep 2010
Posts: 40

Original Poster
Rep: Reputation: 2
This is probably gonna sound stupid, but what? Should I post a copy of the sudoers files?
 
Old 07-31-2011, 06:00 AM   #14
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Can you not just add it to root's crontab?
 
Old 07-31-2011, 12:31 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Jetso View Post
This is probably gonna sound stupid, but what? Should I post a copy of the sudoers files?
You could do that; or you could embrace the learning opportunity,
and learn about the "id" command.

Try id -a <your_username>. Look at the groups it lists.
Try to find one of those in sudoers.



Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripting for accepting input from different directories automatically kswapnadevi Programming 2 11-10-2010 12:11 PM
Bash Scripting handling user input sections automatically Gavin Harper Programming 3 06-18-2010 10:35 PM
bash scripting... taking user input and adding it to a particular area of a file... dagummit Programming 7 01-13-2010 12:07 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration