nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

SSH - Passing Unix login passwords through shell scripts

This is a discussion on SSH - Passing Unix login passwords through shell scripts within the Shell scripting forums, part of the Development/Scripting category; Hi, I am new to SSH and need your inputs to achieve the following: Develop a shell script which will ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 07-20-2007, 12:24 PM
Junior Member
User
 
Join Date: Jul 2007
My distro: IBM AIX
Posts: 1
Rep Power: 0
arulkumarr is on a distinguished road
Default SSH - Passing Unix login passwords through shell scripts

Hi,

I am new to SSH and need your inputs to achieve the following:

Develop a shell script which will read a configuration file with entries like

server1 smith/tiger
server2 dave/lion

where server1 is machine name and smith/tiger is username / password respectively.

By reading the configuration file, shell script should login to server1 as user smith preferably using SSH.

Thanks,
Arul.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 07:17 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

You need to create a login file as follows login.txt:
Code:
server1|user|password
A shell script as follows (sshlogin.sh):
Code:
#!/bin/bash
FILE=login.txt
CONNECT=sshlogin.exp
SERVERNAME=$1
MyServer=""
MyUser=""
MyPassword=""
exec 3<&0
exec 0<$FILE
while read line
do
        MyServer=$(echo $line | cut -d'|' -f1)
        MyUser=$(echo $line | cut -d'|' -f2)
        MyPassword=$(echo $line | cut -d'|' -f3)
        if [ "$SERVERNAME" == "$MyServer" ];
        then
           echo "Running ssh $MyUser@$MyServer..."
          $CONNECT $MyPassword $MyServer $MyUser
        fi
done
exec 0<&3
echo "$SERVERNAME not found in login.txt file"
Modified sshlogin.exp from Ssh login expect script to supply password

In order to use following script you need to install expect tool, use apt-get or yum command!

Code:
#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server 
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# For example:
#  ./sshlogin.exp password 192.168.1.11 who 
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0] 
set ipaddr [lrange $argv 1 1]   
set username [lrange $argv 2 2] 
set timeout -1   
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $username@$ipaddr
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password 
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
To run script - set permissions
Code:
chmod +x sshlogin.sh
chmod +x  sshlogin.exp
Test it by connecting 127.0.0.1
Code:
./sshlogin 127.0.0.1
__________________
May the force with you!

Last edited by monk; 07-20-2007 at 07:20 PM..
Reply With Quote
  #3 (permalink)  
Old 01-20-2008, 10:43 PM
Junior Member
User
 
Join Date: Jan 2008
My distro: Solaris
Posts: 1
Rep Power: 0
rnls001 is on a distinguished road
Default

Hi Experts,

The information provided here is really helpful but it does not serve my requirements.

I have more than 200 machines in my network running linux and I want to be able to ssh to each one of them using thier IP address stored in a file and then run some commands inside each machine, log out and log in the next machine in the list and do the same, so on ...

Now, using key-gen is not practical for me and I do not want to install the "expect" utility due to some reason.

Please tell me if there is any way to supply ssh password using bash scripting? I know supplying the password in script might not be very secure, but still I want to do it this way. I shall be greatful to any help.

Regards, R.
Reply With Quote
  #4 (permalink)  
Old 01-29-2008, 03:54 PM
Junior Member
User
 
Join Date: Jan 2008
My distro: fedora
Posts: 1
Rep Power: 0
emailtokmani is on a distinguished road
Default Use ssh-keygen option

Hi,

There is one option in SSH,i.e ssh-keygen, we can generate the public keys and we need to move it to particulars users home directory...

/home/dave/.ssh/ur-keys

use this following link to know little bit clear

ssh-keygen: password-less SSH login
ssh-keygen - authentication key generation
SSH with Keys HOWTO: SSH with Keys in a console window

Good luck..............
Reply With Quote
  #5 (permalink)  
Old 08-27-2008, 10:22 PM
Junior Member
User
 
Join Date: Aug 2008
My distro: Fedora 7
Posts: 1
Rep Power: 0
MeeToo is on a distinguished road
Default ssh script to execute commands on a remote machine

Thanks to Monk for the script. i am very new to Linux but i need to create a script to run another script on a remote server. i can now log in through SSH but i cannot even run an ls command. it returns "invalid". i have done a lot of research and still cannot find the answer. the script i am using is below and i have inserted the TOP command as an example. what i need to do is:
1. Open SSH connection on one remote server.
2. run a script ./Myscript -m
3. capture the results
4. it would be nice to keep the connection open

#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server
# and execute command.
# This script needs three argument to( connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# For example:
# ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit Bash Shell Scripting Directory For Linux / UNIX for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set username [lrange $argv 2 2]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh root@123.456.789.123
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "MyPassWord\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
top -b -n 1 | head -n 8
expect eof
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
passing options to a shell script bigpaw Shell scripting 2 10-11-2007 03:40 AM
shell scripts required for students roni Shell scripting 6 07-20-2007 07:21 PM
Linux / UNIX set increase the number of failed login retries with SSH client sweta Getting started tutorials 0 06-12-2007 03:35 AM
running command a root in shell scripts chiku Shell scripting 1 07-17-2006 07:39 PM
sample scripts for FTP in Unix jerry Shell scripting 1 06-24-2006 12:01 AM


All times are GMT +5.5. The time now is 07:15 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36