Execute commands after logging in into the ssh connection not locally
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.