1828219 Members
1920 Online
109975 Solutions
New Discussion

Script help

 
Senthil Prabu.S_1
Trusted Contributor

Script help

Hi all,
I want to write a shell script to automate the useradd and assigning password to that user [ only through shell script ]. I tried with using "EOF" but still the script prompts for passowrd.

The script that I wrote was
!/usr/bin/sh -x
useradd prasad
echo "vnc" 2>&1
# Here prasad is username and test is password.
passwd prasad << EOF
test
test
EOF


Can anyone help me to automate this......

Thanks,
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
15 REPLIES 15
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

And also I dont want to do this using expect.
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Alexander Chuzhoy
Honored Contributor

Re: Script help

here's an example
#!/bin/bash
for i in `seq 1 10`
do useradd user$i
echo password$i |passwd --stdin user$i
done
exit
it adds 10 users user1, user2,etc
and gives each the password same as username.
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

No, the script fails

The error is
Try `passwd --help' or `passwd --usage' for more information.
passwd: unrecognized option `--stdin'

One man's "magic" is another man's engineering. "Supernatural" is a null word.
Alexander Chuzhoy
Honored Contributor

Re: Script help

what operating system do you use?
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

Hi,

cat /etc/SuSE-release
SUSE LINUX Enterprise Server 9 (i586)
VERSION = 9
PATCHLEVEL = 2

Any inputs......
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

Hi alex,
I googled but no use.
My passwd utility doesnot support --stdin option.

Is this possible to do in a shell script?
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Alexander Chuzhoy
Honored Contributor

Re: Script help

You can use the -p key for useradd command
try:
useradd test -p password for example.
Where password is the crypted password.

Senthil Prabu.S_1
Trusted Contributor

Re: Script help

Hi Alex,
My problem is that I wanted to create a user account and also assign password to that user, to be done in a shell script. Any another script uses this user to do some operations.

So, useradd -p will never going to help me.
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Muthukumar_5
Honored Contributor

Re: Script help

;) hi how r u.

You can not achive this with back scripting with EOF format as like ftp.

Few ways to accomplish as,

1) expect scripting to accomplish job
2) telnet piping script as,

(
sleep 1
echo "root"
sleep 1
echo ""
sleep 1
echo "passwd "
sleep 1
echo "password"
sleep 1
echo "password"
sleep 1
echo "exit"
) | telnet localhost


Change the sleep timings based on your system speed. Revert back if this is not working.

-Muthu
Easy to suggest when don't know about the problem!
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

Hi,
:-), nice to hear from you.I modified ur script as below;
#!/bin/bash
useradd test
(
sleep 1
echo "test"
sleep 1
echo "test"
sleep 1
echo "passwd test"
sleep 1
echo "test"
sleep 1
echo "test"
sleep 1
echo "exit"
) | telnet localhost


When I execute it,
kassel1:/home/rtp40/prabu # ./2.sh
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused


entry in /etc/passwd
test:x:5002:100::/home/test:/bin/bash

entry in /etc/shadow
test:x:5002:100::/home/test:/bin/bash

Can you throw some more lights on this......
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Muthukumar_5
Honored Contributor

Re: Script help

change localhost to your machine name or machine ip-address itself. Just try to connect with the machine where you want to change password of the user account with telnet. It may be localhost or 127.0.0.1 or hostname or ip-address or FQDN.

-Muthu
Easy to suggest when don't know about the problem!
Senthil Prabu.S_1
Trusted Contributor

Re: Script help

I tried on another machine.

Output:
bash-3.00# ./2.sh
+ useradd test
+ sleep 1
+ telnet localhost
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+ echo test
+ sleep 1
+ echo test
+ sleep 1
login: Password: + echo 'passwd test'
+ sleep 1

+ echo test1
test1
+ sleep 1
+ echo test1
test1
+ sleep 1
+ echo exit
exit
Connection to localhost closed by foreign host.
bash-3.00#


But I couldnot login as test using passeord,
"test1". It fails.
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Muthukumar_5
Honored Contributor

Re: Script help

It is the problem sleep statements. You have to vary the sleep timings based on your machine. I hope your machine is *dead* slow to get login prompt. Change first sleep before echo "account name" as sleep 3 and try. Vary sleep timings upto the passwd command is getting worked.

-Muthu
Easy to suggest when don't know about the problem!
Jess Long
Frequent Advisor

Re: Script help

The following modification of the above script will add 10 users, user1, user2, etc. and set the password for each the same as the username.

#!/bin/bash
for i in `seq 1 10`
do
useradd -m -p user${i} user${i}
done
exit

The -p option sets the encrypted password.

JL
Alan_152
Honored Contributor

Re: Script help

The CD that comes with Nemeth et al's Unix System Administration Handbook has exactly the utility you want. You should be able to download a current image of the CD from the publisher's web site.