Operating System - HP-UX
1748163 Members
3714 Online
108758 Solutions
New Discussion юеВ

Re: useradd script question

 
SOLVED
Go to solution
emp001
Advisor

useradd script question

I have the issue, I put the ssh command into the script ( I have 2 hosts in the hosts file and 3 users тАУ user1, user2 , user3 in the user_list) :



If I use ssh command in the script it uses only user1:

for i in `cat hosts`

do

echo "Adding users on $i"

cat user_list | while read uid comment

do

echo "Adding user $uid on host $i"

ssh -q $i useradd -c \"$comment\" -g users -d /home/$uid -s /usr/bin/ksh -m -k /etc/skel $uid

#echo $i $uid $comment

done

done



This is the output:



root @ host3 [/var/tmp/scripts]

# ./useradd.sh

Adding users on host1

Adding user user1 on host host1

host1 user1 users1 test

Adding users on host2

Adding user user1 on host host2

host2 user1 users1 test

root @ host3 [/var/tmp/scripts]

#







If I comment out ssh command and just use echo command it takes all 3 users for each host, so something is wrong with ssh command I am using, can you please help me with that:



root @ host3 [/var/tmp/scripts]

# vi useradd.sh

for i in `cat hosts`

do

echo "Adding users on $i"

cat user_list | while read uid comment

do

echo "Adding user $uid on host $i"

###ssh -q $i useradd -c \"$comment\" -g users -d /home/$uid -s /usr/bin/ksh -m -k /etc/skel $uid

echo $i $uid $comment

done

done



The output is good when ssh is commented out:



root @ host3 [/var/tmp/scripts]

# ./useradd.sh

Adding users on host1

Adding user user1 on host host1

host1 user1 users1 test

Adding user user2 on host host1

host1 user2 users2 test

Adding user user3 on host host1

host1 user3 users3 test

Adding users on host2

Adding user user1 on host host2

host2 user1 users1 test

Adding user user2 on host host2

host2 user2 users2 test

Adding user user3 on host host2

host2 user3 users3 test

root @ host3 [/var/tmp/scripts]

#



So, how can I use the ssh inside those 2 loops to make it working for all3 users to add to 2 hosts ?
I also found that it does not work with any ssh commands, even if I did "ssh host1 ll" inside that wile loop
if I do ll locally it works with all 3 users,

Also if I use only 1st "for" loop and use the fixed user it works.

Thank you.
Mike.
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: useradd script question

Hi Mike:

Add the '-n' option :

# ssh -q $i -n useradd -c \"$comment\" -g users -d /home/$uid -s /usr/bin/ksh -m -k /etc/skel $uid

Regards!

...JRF...
emp001
Advisor

Re: useradd script question

Thank you very much it is working, it is quick and great.

Mike
James R. Ferguson
Acclaimed Contributor
Solution

Re: useradd script question

Hi (again) Mike:

> Thank you very much it is working, it is quick and great.

I'm happy to have helped. Help future readers find useful answers too by taking a few minutes to read:

https://forums13.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
emp001
Advisor

Re: useradd script question

Sorry, the points are submitted
emp001
Advisor

Re: useradd script question

Hi James,
I got one more question,

I am using the same script to reset the passwd and instead of useradd command I use:

ssh -t $i -n passwd $uid inside the while loop, but it does not work, giving me the error:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Changing password for user3
pam_chauthtok: System error

Is there any way it can be fixed, and is there any way some how to input the hardcoded passwd ?
Dennis Handly
Acclaimed Contributor

Re: useradd script question

>I am using the same script to reset the passwd and instead of useradd command I use:

You might look in other threads for useradd.sam.

>ssh -t $i -n passwd $uid .. but it does not work, giving me the error:
>Pseudo-terminal will not be allocated because stdin is not a terminal.

You need that -n to work with loops. But that -n is preventing passwd from working. Catch-22.

>is there any way some how to input the hardcoded passwd?

useradd.sam?
emp001
Advisor

Re: useradd script question

What is the sintax to use the useradd.sam creating the user remotely with the passwd using ssh ?
emp001
Advisor

Re: useradd script question

what is the syntax of useradd.sam command ?
or do you have sny other ideas how to reset passwd on the servers remotely via ssh ???

Thank you.
Mike.
Johnson Punniyalingam
Honored Contributor

Re: useradd script question

>> do you have sny other ideas how to reset passwd on the servers remotely via ssh ??<<

Check below Link

http://www.unix.com/unix-advanced-expert-users/43352-using-ssh-change-password-remotely.html
Problems are common to all, but attitude makes the difference