LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-20-2010, 10:45 PM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
Execute commands after logging in into the ssh connection not locally


Hi,

I am creating this script which will login to a server with ssh and check if a particular exists there, if not it will create the user.

This is the script:

Quote:
ssh 192.168.100.5

cat /etc/passwd|grep -i $1 > /dev/null
if [ $? -eq 0 ];
then
echo "User already exists";
else
{
useradd $1
echo $1|passwd --stdin $1 > /dev/null
}
fi

After running the script it is asking for ssh password, which is fine.
But the problem is, its not doing the rest of the work after logging in into the server, its checking and creating the user only when I'm logging out of the ssh connection, means the user is created locally only and not in the server.
How can I force this script to execute the rest of the commands in the server after logging in, not in the client?

Thanks in advance
 
Old 10-21-2010, 07:06 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
While this is not a trivial question -- I don't know the answer off the top of head -- I am surprised at a man w/ your credentials asking it.

Did you Google on it? If so, it would help us to not repeat your efforts if you would show us your search terms.

Did you re-read the man page? I believe your answer is there; although it took me a 2nd read to see it.
 
Old 10-23-2010, 08:55 PM   #3
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
you're close, try putting the "script" you want to run on the remote server in quotes like so:

Code:
ssh 192.168.100.5 "if grep -iq $1 /etc/passwd
  then
    echo \"User already exists\"
  else
    useradd $1
    echo $1|passwd --stdin $1 > /dev/null 2>&1
  fi
"
I cleaned up your script a hair too since im anal... there is no reason to use cat with grep as grep is perfectly capable of reading from the file system. Also no reason to redirect with grep either if you dont want to see the output -q will supress printing out. Next, the if statement is good to check a command for return status, a 0 is true, anything else is false. Last "> /dev/null" only redirects stdout, just fyi.

edit: Inside the "'s you have to escape " and ', so the echo commadn would need to be llike so to work:

echo \"blah blah blah\" I do this all day every day and always forget that... not surprised i forgot to do it here.

Last edited by trey85stang; 10-23-2010 at 09:03 PM. Reason: i forgot to esacpe "'s inside the script...
 
1 members found this post helpful.
Old 10-25-2010, 01:58 AM   #4
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by trey85stang View Post
you're close, try putting the "script" you want to run on the remote server in quotes like so:

Code:
ssh 192.168.100.5 "if grep -iq $1 /etc/passwd
  then
    echo \"User already exists\"
  else
    useradd $1
    echo $1|passwd --stdin $1 > /dev/null 2>&1
  fi
"
I cleaned up your script a hair too since im anal... there is no reason to use cat with grep as grep is perfectly capable of reading from the file system. Also no reason to redirect with grep either if you dont want to see the output -q will supress printing out. Next, the if statement is good to check a command for return status, a 0 is true, anything else is false. Last "> /dev/null" only redirects stdout, just fyi.

edit: Inside the "'s you have to escape " and ', so the echo commadn would need to be llike so to work:

echo \"blah blah blah\" I do this all day every day and always forget that... not surprised i forgot to do it here.

Hi trey85stang,

Its really working, thanks a lot.

This is the script I am using now

Quote:
ssh $2 "if [ $(id -g) -eq 0 ]
then
if grep -iq $1 /etc/passwd
then
echo "User already exists"
else
{
sudo /usr/sbin/useradd $1
echo $1|passwd --stdin $1 > /dev/null

}
fi

else
echo "Error: You ust be root"
fi
"
But here the sudo is not working, its throwing this error:
Quote:
sudo: sorry, you must have a tty to run sudo
Can you please tell me how to provide a tty to run sudo in the script?
 
Old 10-25-2010, 06:44 AM   #5
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
EDIT:[content removed]

Last edited by Sayan Acharjee; 10-25-2010 at 09:13 AM.
 
Old 10-25-2010, 09:15 AM   #6
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
So, how to declare the tty for the ssh connection? any idea?
 
Old 10-25-2010, 10:19 AM   #7
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
im not sure, i have never run into that problem. You should have a pseudo terminal assigned which should work fine for sudo
 
Old 10-27-2010, 02:01 AM   #8
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by trey85stang View Post
im not sure, i have never run into that problem. You should have a pseudo terminal assigned which should work fine for sudo
Hey thanks for the concern, yes I know the terminal should be declared in the script in order to do that and I have tried with $TTY_SSH=/dev/pts/1 and $TTY=/dev/pts/1 and found both of them are not working.

If anyone knows how to declared the terminal in the script kindly let me know.
Thanks in advance.
 
Old 10-28-2010, 01:06 PM   #9
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
sayan, I just though of something.. perhaps the useradd command is requirng you to have an interactive terminal session. Doesnt useradd want you to enter in information? Im thinking it does and its not a sudo problem but a useradd problem.
 
Old 10-30-2010, 10:56 AM   #10
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by trey85stang View Post
sayan, I just though of something.. perhaps the useradd command is requirng you to have an interactive terminal session. Doesnt useradd want you to enter in information? Im thinking it does and its not a sudo problem but a useradd problem.
Thanks for bothering

I'm not sure about it, need to check again, will let you know the result.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rsync ssh - how to execute shell commands on server banjer Linux - Newbie 4 11-04-2009 02:01 PM
Execute commands via ssh - working and failing billlee Linux - Software 5 04-11-2009 07:40 AM
can ssh execute multiple commands at the same time? Singist Linux - Networking 2 04-04-2006 09:14 AM
biff works over ssh but not locally (in Konsole) jashmenn Slackware 0 03-17-2006 07:23 PM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration