Help with calling to file for a username and password combo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with calling to file for a username and password combo
# 1  
Old 01-29-2013
Help with calling to file for a username and password combo

Hokay...first post, and I have been doing Linux scripting for a total of 2 days now. I think I am doing pretty well, but awk and arrays(what I think I need here) is a bit above me so far.

I have written a script that will take and either create or modify 5 users, and passwords.

It checks the device to see if a user exists, if they don't, it adds the user, and gives them sudo access, and changes the password to the one specified. If they do exist, it changes the password to the specified password, and then checks for sudoers access, and gives the user access if they don't have it.

Currently this script isn't really scaleable. it us for 5 users. It can be tweaked to add more, but I know there is a better way to do it, I just don't know how at my level of knowledge.

What I want to have it do, is call to a text file that has something like
username password
username password
username password

And have set the username and password on each line, on loop, till EOF. I don't know how to do that, and have it keep username on line1 with username on line1, and username on line2 with username on line2 successfully.

I have already seen a few ways I can clean up the existing code and make it cleaner, but I am trying to get this part figured out before tomorrow night first.

If you have any help, please let me know.

Here is my current script: (it is much cleaner than how it pasted in here)

Code:
#!/bin/sh
user1=blah
user2=blahette
user3=blahjr
user4=mrblah
user5=blaaaaaaaa
password1=adsfasdfaf
password2=asdfasdfasd
password3=asdfasdfasd
password4=asdfadsf
password5=asdfasd
createuser1(){
useradd -m $user1
sh -c "echo '$user1 ALL=(ALL) ALL' >> /etc/sudoers"
echo -e "$password1\n$password1" | (passwd --stdin $user1)
}
changepass1(){
addroot(){
sh -c "echo '$user1 ALL=(ALL) ALL' >> /etc/sudoers"
echo $user1 "has been given root access"
}
printroot(){
echo $user1 already has root access
}
echo -e "$password1\n$password1" | (passwd --stdin $user1)
if [ `grep $user1 /etc/sudoers | wc -l` -lt "1" ];
   then addroot
   else printroot
 
fi
}
 
createuser2(){
useradd -m $user2
sh -c "echo '$user2 ALL=(ALL) ALL' >> /etc/sudoers"
echo -e "$password2\n$password2" | (passwd --stdin $user2)
}
 
changepass2(){
addroot(){
sh -c "echo '$user2 ALL=(ALL) ALL' >> /etc/sudoers"
echo $user2 "has been given root access"
}
printroot(){
echo $user2 already has root access
}
echo -e "$password2\n$password2" | (passwd --stdin $user2)
if [ `grep $user2 /etc/sudoers | wc -l` -lt "1" ];
   then addroot
   else printroot
 
fi
}
 
createuser3(){
useradd -m $user3
sh -c "echo '$user3 ALL=(ALL) ALL' >> /etc/sudoers"
echo -e "$password3\n$password3" | (passwd --stdin $user3)
}
changepass3(){
addroot(){
sh -c "echo '$user3 ALL=(ALL) ALL' >> /etc/sudoers"
echo $user3 "has been given root access"
}
printroot(){
echo $user3 already has root access
}
echo -e "$password3\n$password3" | (passwd --stdin $user3)
if [ `grep $user3 /etc/sudoers | wc -l` -lt "1" ];
   then addroot
   else printroot
 
fi
}
 
createuser4(){
useradd -m $user4
sh -c "echo '$user4 ALL=(ALL) ALL' >> /etc/sudoers"
echo -e "$password4\n$password4" | (passwd --stdin $user4)
}
changepass4(){
addroot(){
sh -c "echo '$user4 ALL=(ALL) ALL' >> /etc/sudoers"
echo $user4 "has been given root access"
}
printroot(){
echo $user4 already has root access
}
echo -e "$password4\n$password4" | (passwd --stdin $user4)
if [ `grep $user4 /etc/sudoers | wc -l` -lt "1" ];
   then addroot
   else printroot
 
fi
}
 
createuser5(){
useradd -m $user5
sh -c "echo '$user5 ALL=(ALL) ALL' >> /etc/sudoers"
echo -e "$password5\n$password5" | (passwd --stdin $user5)
}
changepass5(){
addroot(){
sh -c "echo '$user5 ALL=(ALL) ALL' >> /etc/sudoers"
echo $user5 "has been given root access"
}
printroot(){
echo $user5 already has root access
}
echo -e "$password5\n$password5" | (passwd --stdin $user5)
if [ `grep $user5 /etc/sudoers | wc -l` -lt "1" ];
   then addroot
   else printroot
 
fi
}
 
if [ `grep $user1 /etc/passwd | wc -l` -gt "0" ];
   then changepass1 && echo $user1 "already exists , password has been changed."
   else createuser1 && echo $user1 "has been created and given root access."
fi
if [ `grep $user2 /etc/passwd | wc -l` -gt "0" ];
   then changepass2 && echo $user2 "already exists , password has been changed."
   else createuser2 && echo $user2 "has been created and given root access."
fi
if [ `grep $user3 /etc/passwd | wc -l` -gt "0" ];
   then changepass3 && echo $user3 "already exists , password has been changed."
   else createuser3 && echo $user3 "has been created and given root access."
fi
if [ `grep $user4 /etc/passwd | wc -l` -gt "0" ];
   then changepass4 && echo $user4 "already exists , password has been changed."
   else createuser4 && echo $user4 "has been created and given root access."
fi
if [ `grep $user5 /etc/passwd | wc -l` -gt "0" ];
   then changepass5 && echo $user5 "already exists , password has been changed."
   else createuser5 && echo $user5 "has been created and given root access."
fi
exit

# 2  
Old 01-29-2013
You can use logic similar to the following. This assumes each line contains a space separated username/password combo. You can then just act upon $username and $password on each loop cycle:

Code:
$ while read username password; do
>    echo "Username is: ${username} Password is: ${password}"
> done < cashman04.txt
Username is: username1 Password is: password1
Username is: username2 Password is: password2
Username is: username3 Password is: password3
$ cat cashman04.txt
username1 password1
username2 password2
username3 password3

Cheers,
ZB
# 3  
Old 01-29-2013
When writing functions use positional parameters instead of hard-coded vars, reads user and password from file:

Not sure why your were using sh -c for the append to /etc/sudoers - changed to standard echo

Code:
#!/bin/bash
 
createuser(){
    useradd -m $1
    echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
    passwd --stdin $1<<EOF
$2
$2
EOF
}
 
changepass(){
    addroot(){
        echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
        echo "$1 has been given root access"
    }
 
    printroot(){
        echo "$1 already has root access"
    }
 
    passwd --stdin $1<<EOF
$2
$2
EOF
    if [ grep -q "$1 ALL=" /etc/sudoers ]; then
        printroot $1
    else
        addroot $1
    fi
}
 
i=0
while read username password
do
    if [ grep -q "^$username:" /etc/passwd ]
    then
        changepass $username $password
        echo "$username already exists , password has been changed."
    else
        createuser $username $password
        echo "$username has been created and given root access."
    fi
    i=$((i+1))
done < cashman04.txt


Last edited by Chubler_XL; 01-29-2013 at 09:48 PM.. Reason: Use file for user/pass, here-docs for input to passwd
# 4  
Old 01-29-2013
EDIT: I think I am starting to get a better grasp of it.. Still not entirely sure where $username and $password are coming from though.

Any chance of some comments of what is going on where? A lot of this is over my head. I have no clue now how the if statement is determining whether to do "then" or "else". Don't see where $username and $password are being set either, lol.

And I'm getting "line 34: [: too many arguments" on the below line:

if [ grep -q "^$username:" /etc/passwd ]


I'm gonna tear it apart for a while and see if I can figure out what is going on...

But thanks so much, I get a little of it, which is helping out immensely.





Quote:
Originally Posted by Chubler_XL
When writing functions use positional parameters instead of hard-coded vars, reads user and password from file:

Not sure why your were using sh -c for the append to /etc/sudoers - changed to standard echo

Code:
#!/bin/bash
 
createuser(){
    useradd -m $1
    echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
    passwd --stdin $1<<EOF
$2
$2
EOF
}
 
changepass(){
    addroot(){
        echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
        echo "$1 has been given root access"
    }
 
    printroot(){
        echo "$1 already has root access"
    }
 
    passwd --stdin $1<<EOF
$2
$2
EOF
    if [ grep -q "$1 ALL=" /etc/sudoers ]; then
        printroot $1
    else
        addroot $1
    fi
}
 
i=0
while read username password
do
    if [ grep -q "^$username:" /etc/passwd ]
    then
        changepass $username $password
        echo "$username already exists , password has been changed."
    else
        createuser $username $password
        echo "$username has been created and given root access."
    fi
    i=$((i+1))
done < cashman04.txt


Last edited by cashman04; 01-29-2013 at 10:21 PM..
# 5  
Old 01-29-2013
Oops, typo there should be:
Code:
if grep -q "^$username:" /etc/passwd

Same for this too:

Code:
if grep -q "$1 ALL=" /etc/sudoers ; then

EDIT: username and password come from the while read username password ; do ...... done < input_file statement. In shell if statements run a command and examine it's return code when zero the then part is executed otherwise the else part . ( [ is a shell builtin command used to do standard tests)

Last edited by Chubler_XL; 01-29-2013 at 10:38 PM..
# 6  
Old 01-29-2013
Ok, I am taking this apart bit by bit to understand it. I pretty much get the $username and $password, and how you can use "while read" to get that info, and assign it..
Now though, how are you passing this to $1 and $2? and how does it know to run the command with say user1 and password1, and then user2 and password2 a different time? and over, until end of file?
# 7  
Old 01-29-2013
the shell builtin while continues to run the code between do and done until the read statement returns non-zero (ie end of input file).

When you call a function (like changepass $username $password) $1 $2 $3... $n are populated from the arguments passwd on the function call (in this instance $1 = $username and $2 = $password, and $# is set to 2, indicating 2 arguments were passed).

This makes the changepass function quite useful as you can call it just like other unix commands and pass values directly in like:

Code:
changepass user1 pass1
changepass user2 pass2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Steps after username and password is entered !

Hi, I know this sounds crazy question.. but I am just curious to know what happens next when I enter username and password and hit enter on a new Unix session (using Putty)? I mean which file gets executed, how the default login shell is determined etc... regards juzz4fun (5 Replies)
Discussion started by: juzz4fun
5 Replies

2. UNIX for Dummies Questions & Answers

How do you reset username/password

Picked up a 3b2 running System V. Works fine, but it requires a username and password. Is the username "root" or "sysadm"? How do I find out and how to I reset it or bypass it? Thanks. (2 Replies)
Discussion started by: TanRuNomad
2 Replies

3. Shell Programming and Scripting

Username and password

Hi I am new to using unix and am struggling with a script i am writing. What i am trying to do is get a user to enter a username, check the original file i created with username and pin to see if their is a corresponding entry. Next ask the user to enter the pin and see if this matches... (5 Replies)
Discussion started by: somersetdan
5 Replies

4. UNIX for Dummies Questions & Answers

How can i hide username/password

hi all, i run sqlplus command on unix(HP-UX) like "sqlplus username/password@serverA @deneme.sql" but when someone run "ps -ef | grep sqlplus", it can see my username and password :( How can i hide username and password. thanx. (1 Reply)
Discussion started by: temhem
1 Replies

5. Red Hat

Trouble logging in with username and password

I have a RHEL 5 server that I can log into with an LDAP account hosted on a server running Sun DSEE 6.3 with an ssh key pair but not with my username and password. When I try to login to the console I am given the "login incorrect" message as if I fat fingered my password. Other users with... (5 Replies)
Discussion started by: ilikecows
5 Replies

6. Shell Programming and Scripting

wget with embedded username/password

Hi, I am encoding the username and password to the url and use it with wget. I.e wget ftp://username:password@myserver.com/test.mp3 However this does not work if the password contains @ character. if the password contains @, then the encoded url becomes wget... (1 Reply)
Discussion started by: learn more
1 Replies

7. Shell Programming and Scripting

Execute via crontab taking username/password from file

Dear All Here is the details what i want to achieve from shell scripts I have a sever where 5 databases are created. which i having diffrent SID's. Now i want to execute some SQL queries on each one of the databases. (SQL Query is same).That i want to acheive via crontab Now each one of the... (2 Replies)
Discussion started by: jhon
2 Replies

8. Shell Programming and Scripting

Read Oracle Username password SID from single file and pass it to shell

Dear All I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
Discussion started by: jhon
2 Replies

9. AIX

How transfer sql file from one serverto another without username and password

hi there, Please help me How transfer sql file from one server to another without username and password .i know just IP address. Thanks Arpit (1 Reply)
Discussion started by: Arpitmitm
1 Replies

10. Shell Programming and Scripting

username password in script

Can we write a script to telnet to a unix server from unix with the username and password hardcoded in the script?? something like ssh a@b -p password ??? (5 Replies)
Discussion started by: roshanjain2
5 Replies
Login or Register to Ask a Question