Shell script to automatically login through ssh and create user
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.
Shell script to automatically login through ssh and create user
Hi everyone,
I was trying to create a shell script which will automatically login to the server 192.168.1.7 and checks if a user exists there or not, if it doesn't exist then it should create the user.
I have very little idea about shell scripts, please help me out here.
This is what I tried:
Quote:
#!/bin/bash
pass="sacharje"
ssh 192.168.1.7
Now, how to pass that password automatically to the ssh when it asks for the password? (I can't use public key authentication here)
Putting your password in a script is not a good practice. All you need is to do is to:
1.) set-up your ssh to login automatically without prompting for a password
2.) include ssh connection(which connects using key authentication instead of password)on your script
3.) add the necessary commands for checking/creating users.
sorry i didn't notice you mentioned (I can't use public key authentication here )
Last edited by ionrivera; 10-20-2010 at 04:44 AM.
Reason: sorry i didn't notice you metioned (I can't use public key authentication here )
sshpass is a tool for non-interactivly performing password authentication with SSH's so called "interactive keyboard password authentication". Most users should use SSH's more secure public key authentication instead.
Login to ssh server 192.168.1.10 with password called qaz123wsx:
All you need is to set-up your ssh to login automatically without prompting for a password.
First you’ll need to generate your local public key. This is the public end of a local public / private pair that you’ll share with the remote machine to identify you.
Code:
ssh-keygen -t dsa (on your local machine)
Second you’ll need to copy this key to the remote machine using a command such as:
Code:
scp ~/.ssh/id_dsa.pub user@yourserver.com:~
Lastly, log into the remote machine via ssh (using your password for the last time!) and use this command to add the newly generated key to the list of authenticated keys:
Code:
cat id_dsa.pub >> .ssh/authorized_keys
You’ll also probably want to delete the original key as well.
Code:
rm id_dsa.pub
At this point a copy of your key is now stored on the remote machine as an authorized keys and any ssh connection coming from the local machine will match that key and connect with the key authentication instead of a password.
Thanks,
but I know that procedure, I want the script to automatically pass the password. As I posted earlier, I can't use the PUBLIC KEY authentication.
sshpass is a tool for non-interactivly performing password authentication with SSH's so called "interactive keyboard password authentication". Most users should use SSH's more secure public key authentication instead.
Login to ssh server 192.168.1.10 with password called qaz123wsx:
Under shell script you may need to disable host key checking:
Thanks Prayag, its working. I can log in to the server, but whatever command I'm running from the script after logging in is not executed in the server itself, those are getting executed only after logging out of the server, means they are not executed on the remote system.
You might want to look into using expect. It's a little awkward to get used to at first but I've written several utility scripts to make changes across hundreds of linux servers and workstations and it has worked pretty well.
whatever command I'm running from the script after logging in is not executed in the server itself, those are getting executed only after logging out of the server, means they are not executed on the remote system.
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?
Following command displays memory in format of "available memory = used + free memory" :
These commands are working fine 'cos everytime you are executing them its executing after logging in to the server.
I can't use the ssh command everytime in the script.
How should I edit the script now? any idea?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.