Logging in unix account taking password from a parameter file

Hi All,

I am writing a script where it updates a file in an unix account.
To update that file i need to be logged in as that account user.

say account name is ab01 and its password is passab01.

What i want to do is, my script should read login id and password from a parameter file and login to that environment.

Parameter file

ab01 passab01
ab02 passab02
ab03 passab03
.... and so on

before running script
server1:/home/oper/pkbond> id
uid=121(pkbond) gid=101(oper)

after running script
server1:/home/oper/ab01> id
uid=121(ab01) gid=101(oper)

Please help

Thanks in Advance :slight_smile:

Regards,
pkbond

This sounds a bit difficult using shell script. Try enabling ssh for the required users, that should be a cake walk. If you still want to do it with script, probably you should try expect

regards,
Ahamed

I dont know exeactly what do you want but i try to make some simple script.

first of all you have to root permission.
if you change home directory other user then you need write permission in home directory else we get error like in below (permission error)
after you must run visudo and add pkbond to wheel group.
because of usermod is neccessary us and this is super command.

# usermod -G wheel pkbond
# cat /usr/etc/.paramlist
ab01 passab01 pkbond
ab02 passab02 ??????
[pkbond@rhnserver ~]$ ./justdoit
Before  running script
/home/oper/pkbond
uid=514(pkbond) gid=514(oper)
mkdir: cannot create directory `/home/oper/ab01': Permission denied
Only root can do that.
ab01 processes are OK!!
 
After running script
/home/oper/ab01
uid=514(ab01) gid=514(oper)
 
ab02 is not logged or is not parameter list!!
 
[pkbond@rhnserver ~]$ cat justdoit
#!/bin/bash
## justdoit
parameterfile=/usr/etc/.paramlist
#chown pkbond.pkbond $parameterfile
#chmod go-rwx $parameterfile
shopt -s expand_aliases
alias usermod='sudo '/usr/sbin/usermod' '
HOME=/home/oper
USER=$(id -un)
change_users () {
 while read -r newuser passwd loggeduser
  do
   if [[ $(who -u | grep $USER ) ]] && [[ $(echo $loggeduser | grep $USER) ]]  ; then
    echo -e "Before  running script \n$(finger $USER |awk '/Direct/ { print $2 }') \n$(id $USER |sed 's/\([^ ]* [^ ]*\).*/\1/')\n"
    mkdir -p $HOME/$newuser ; usermod -d $HOME/$newuser $USER
    usermod -l $newuser $USER ; echo "$passwd" | passwd --stdin $newuser ; echo "$newuser processes are OK!!" ; echo
    echo -e "After running script \n$(finger $newuser |awk '/Direct/ { print $2 }') \n$(id $newuser |sed 's/\([^ ]* [^ ]*\).*/\1/')"
   else
    echo -e "\n$newuser is not logged or is not parameter list!!"
   fi
  done < $parameterfile
}
change_users ""

regards
ygemici

I really wish people would stop suggesting expect as the duct-tape universal solution to interactive login issues. They're often not doing you any favors and inviting the creation of moon-sized security holes. These utilities are designed to prevent you from using stored plaintext passwords for a reason -- it's a really bad idea.

Do what you need to do as root instead, or perhaps with sudo configured to let you and only you do this and only this one particular thing as root, in one particular way. Things shouldn't prompt you for user passwords when you're root, which prevents the need for an insecure file holding the passwords for every user on your system, in plaintext. Sheesh!

1 Like

I agree completly you.