LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 04:51 AM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 592

Rep: Reputation: 64
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)
 
Old 10-20-2010, 05:24 AM   #2
ionrivera
Member
 
Registered: May 2010
Distribution: Debian, RHEL, AIX, Solaris, HP-UX
Posts: 61

Rep: Reputation: 4
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 05:44 AM. Reason: sorry i didn't notice you metioned (I can't use public key authentication here )
 
Old 10-20-2010, 05:27 AM   #3
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
Install sshpass

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:
Quote:
sshpass -p 'qaz123wsx' ssh john@192.168.1.10
Under shell script you may need to disable host key checking:

Quote:
sshpass -p 'qaz123wsx' ssh -o StrictHostKeyChecking=no john@192.168.1.10
 
1 members found this post helpful.
Old 10-20-2010, 05:28 AM   #4
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 592

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by ionrivera View Post
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.
 
Old 10-20-2010, 05:56 AM   #5
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 592

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by prayag_pjs View Post
Install sshpass

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.
 
Old 10-20-2010, 05:28 PM   #6
Deviathan
Member
 
Registered: Dec 2005
Posts: 46

Rep: Reputation: 16
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.
 
Old 10-20-2010, 11:44 PM   #7
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
Thumbs up

Quote:
Originally Posted by sayan_acharjee View Post
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.

Can you explain with example.Please elaborate
 
Old 10-20-2010, 11:48 PM   #8
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 592

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by prayag_pjs View Post
Can you explain with example.Please elaborate
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?
 
Old 10-21-2010, 12:00 AM   #9
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
Hi,

Hope this examples helps you:

List what ports are open on remote host

Quote:
ssh root@192.168.1.10 netstat -vatn

Following command displays memory in format of "available memory = used + free memory" :

Quote:
ssh root@192.168.1.10 free -m | grep "Mem:" | awk '{ print "Total memory (used+free): " $3 " + " $4 " = " $2 }'
 
Old 10-21-2010, 02:11 AM   #10
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Bangalore, India
Distribution: Linux Mint 13
Posts: 592

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by prayag_pjs View Post
Hi,

Hope this examples helps you:

List what ports are open on remote host




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?
 
Old 10-21-2010, 02:51 AM   #11
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
Hi,

Try ssh again in script as show in example:

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Run a script automatically upon user login? FastFeet Linux - General 3 05-21-2009 06:23 AM
How to create new user that can login SSH only and cant do other things else? kien23 Linux - Newbie 8 04-26-2009 11:55 AM
shell script to automatically kill user on a server vathsan AIX 3 01-06-2009 04:22 AM
Run shell script automatically after Login. hnshashi Linux - Newbie 14 09-26-2008 05:43 AM
Create user accounts from shell script? maheshkodamati Linux - Newbie 1 02-26-2008 12:46 PM


All times are GMT -5. The time now is 01:05 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration