LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-13-2009, 07:09 AM   #1
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Rep: Reputation: 36
Inject value to bash input


Hi,

to explain this, I'm going to start with an example.
When I run a script for ProFTPd to create a user, with ftpasswd it prompts me for a password. Which is not a problem, I type it in and voila.
But, now I have a script, which creates the user, creates his directories, sets file permissions and so on.
Better I just paste the script:
Code:
#!/bin/bash

if [ $2 == "xshells" ]
then
        /usr/sbin/ftpasswd --passwd --name $1 --file /etc/ftp.passwd --uid 1001 --gid 1002 --shell /bin/false --home /var/ftp/users/x/$1
        mkdir /var/ftp/users/x/$1
        chown xshells:xshells /var/ftp/users/x/$1
fi

if [ $2 -ne "xhsells" ]
then
        /usr/sbin/ftpasswd --passwd --name $1 --file /etc/ftp.passwd --uid 1000 --gid 1000 --shell /bin/false --home /var/ftp/users/$1
        mkdir /var/ftp/users/$1
        mkdir /var/ftp/users/$1/pub
        if [ $2 == "proficio" ]
        then
                #/usr/sbin/ftpasswd --group --name proficio --file /etc/ftp.group --gid 1001 --member $1
                mkdir /var/ftp/users/$1/proficio
                mount --bind /var/ftp/proficio /var/ftp/users/$1/proficio
                echo "mount --bind /var/ftp/proficio /var/ftp/users/$1/proficio" >> /etc/rc.d/rc.mounts
        fi
        mount --bind /var/ftp/pub /var/ftp/users/$1/pub
        echo "mount --bind /var/ftp/pub /var/ftp/users/$1/pub" >> /etc/rc.d/rc.mounts
        chown ftpusers:ftpusers -R /var/ftp/users/$1
fi
and I'm runnig it as:
ftpcreate username [groupg
The group is not the group of ftpasswd, but our own group, because each group has different shared directories and home dir location(no group is possible).
Now I want to simply run:
ftpcreate username password group
Now how would I tell the bash script to inject that password when ftpasswd prompts for it(twice)?
 
Old 10-13-2009, 08:45 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
According to the ftpasswd man page, there's an option to pass it to the program through stdin.
Quote:
--stdin
Read the password directly from standard in rather than prompting for it.
This is useful for writing scripts that automate use of ftpasswd.
Now I've never used this program, but I think you might be able to do something like this:
Code:
password=$2

ftpasswd --stdin <rest_of_command> <<<"$password"
I can't guarantee it will work though.

If you don't know what, "<<<" is, it's a here string, which lets you pass the value of a variable directly to stdin.

Last edited by David the H.; 10-13-2009 at 08:48 AM. Reason: typo
 
Old 10-13-2009, 11:32 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Can I just point out
Code:
if [ $2 == "xshells" ]
is not the same value being tested as
Code:
if [ $2 -ne "xhsells" ]
If they are supposed to be the same, why not use 'else' ?
 
Old 10-14-2009, 01:50 AM   #4
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by chrism01 View Post
Can I just point out
Code:
if [ $2 == "xshells" ]
is not the same value being tested as
Code:
if [ $2 -ne "xhsells" ]
If they are supposed to be the same, why not use 'else' ?
It's not finished yet, I had to add around 20 users in a hurry and just made a simple script in 5 minutes and added those urgent users, I'm rewriting the whole script or should I say, already rewrote the whole script, used else statements as well. :P Thanks for pointing it out anyway.
 
Old 10-14-2009, 06:38 AM   #5
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Original Poster
Rep: Reputation: 36
Going to just use this thread instead of making a new one.
I'm sure it's a simple solution, but I can't come up with it.
How would I add a user to my /etc/ftp.group file? If I use ftpasswd script it deletes all previous users from the group and adds only the new one. Now I'd just like to open up the file, find out on which line, let's say, group poweruser is, and append ,username at the end of that line.
 
Old 10-14-2009, 08:25 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
You should be able to append the new name with sed. Store the value in a variable, then use the variable's output in the sed command. I don't know what the format of the /etc/ftp.group file is exactly, but I'm assuming here that you simply need to match a line and append the new name to the end of it.
Code:
groupname="the name of the group you want to match"
newname="$(<command that generates the username>)"

sed --in-place=".bkp" "/$groupname/ s/.*/&,$newname/" /etc/ftp.group
"--in-place=.bkp" makes sed edit the file directly. You can remove the "=.bkp" if you don't want it to produce a backup file.

/$groupname/ matches any line with that name in it, which is then edited by the s/// command. And the substitution simply matches the whole line and tacks the new string to the end of it.

Note that this will match and edit all instances of the string in the file, though. If there's a possibility of there being more than one line, you'll have to alter the matching expression. You can change it to "0,/$groupname/" to match only the first instance, for example.

Finally, note that you need to use " (double quotes) in the sed expression here. The variables will not expand if you use single quotes.
 
Old 10-15-2009, 03:38 AM   #7
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Original Poster
Rep: Reputation: 36
Thanks, worked like a charm.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash input question zcrxsir88 Programming 2 03-14-2008 08:02 PM
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 04:09 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
Text input in bash? pk21 Programming 12 06-05-2003 09:23 AM


All times are GMT -5. The time now is 06:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration