Operating System - HP-UX
1847212 Members
2449 Online
110263 Solutions
New Discussion

Re: Setting variable for remote ssh connection.

 
ankurp
Frequent Advisor

Setting variable for remote ssh connection.

Hi friends,
Need help in executing the following.

i=server_name
group=sysadmin

ssh $i 'useradd -g $group -d /opt/home/x32810 -f 30 -s /bin/sh -c test -m x32810'

This is result i get.
Group -d specified with -g does not exist

pls. help

rgds
Ankur

11 REPLIES 11
Suraj Singh_1
Trusted Contributor

Re: Setting variable for remote ssh connection.

You need to make sure that group 'sysadmin' exists in machine 'server_name'

ssh $i 'cat /etc/group|cut -f 1 -d :|grep sysadmin'

Regards
What we cannot speak about we must pass over in silence.
Muthukumar_5
Honored Contributor

Re: Setting variable for remote ssh connection.

Is sysadmin group existing in server_name machine? check with /etc/group file.

Is group name getting effect in remote machine? Try to check it as,
ssh $i 'echo $group'

hth.
Easy to suggest when don't know about the problem!
ankurp
Frequent Advisor

Re: Setting variable for remote ssh connection.

Thanks friends for replying,
Actually the problem is with the passing of variable.
i need to know if the single quote is enough to pass $grp variable.
ssh $i 'useradd -g $group -d /opt/home/x32810 -f 30 -s /bin/sh -c test -m x32810'
Group -d specified with -g does not exist

Its is not takin the $group variable

waitin

rgds
Ankur
Muthukumar_5
Honored Contributor

Re: Setting variable for remote ssh connection.

Yes. It won't. Bcas local varibles will not be identified by remote machine. Even though if you try locally, that shell will not identify this.

You can do one thing as,

put group=sysamin in /etc/sshrc remote machine or $HOME/.ssh/rc file.

It will work.

hth.
Easy to suggest when don't know about the problem!
ankurp
Frequent Advisor

Re: Setting variable for remote ssh connection.

hi ,

i m doin this to create users and groups remotely from one server to another.
So need to use variables for groups and users.

rgd
Ankur
Doug O'Leary
Honored Contributor

Re: Setting variable for remote ssh connection.

Oh my god, people, this is simple shell expansion!

The reason $group isn't getting sent is because you have single quotes around the command line. In this case, you actually don't even need quotes but if you want them, use double quotes. The shell will then expand all variables and send the command to ${i}...

You don't have to do any of the things the previous posts were mentioning...

To script this whole thing, create a text file with contents like:

host1 user1 group1
host1 user2 group1
host2 user3 group2
host2 user4 group2
host2 user5 group3
...
host# user# group#

cat ${file} | while read host user group
do
ssh -l root ${host} /usr/sbin/useradd -g ${group} -d /opt/home/${user} -f 30 -s /bin/ksh -c test -m ${user}
done

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Gopi Sekar
Honored Contributor

Re: Setting variable for remote ssh connection.


you need to double quote the command line:

ssh $i "useradd -g $group -d /opt/home/x32810 -f 30 -s /bin/sh -c test -m x32810"

shell does not expand any variables if it is surrounded by single quote('). you should use double quote to expand them

Hope this helps,
Gopi

Never Never Never Giveup
Suraj Singh_1
Trusted Contributor

Re: Setting variable for remote ssh connection.

Oh yes,

You must use double quotes (") and not single quotes.

Tested in one of my machines, and it works!!

Regards
What we cannot speak about we must pass over in silence.
Devesh Pant_1
Esteemed Contributor

Re: Setting variable for remote ssh connection.

Ankur,
according to what I tested on my servers you can get rid of the single quotes and you are all set.

thanks
Devesh
ankurp
Frequent Advisor

Re: Setting variable for remote ssh connection.

That worked friends.
Actually the double quotes of comment was also creating problems.
I m proud to be associated with this forum.

Thanks again.

rgds
Ankur
ankurp
Frequent Advisor

Re: Setting variable for remote ssh connection.

This is what works
ssh $i " useradd -u 400 -g ${grp} -d /opt/home/x32810 -f 30 -s /bin/sh -c 'test user' -m xtest "

bye
Rgds
Ankur