nixCraft Linux / UNIX / Shell Scripting Forum

nixCraft

Linux / UNIX 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 ...


Register free or login to your account to remove all advertisements.

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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 20th July 2007, 11:24 AM
Junior Member
 
Join Date: Jul 2007
OS: IBM AIX
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 20th July 2007, 06:17 PM
monk's Avatar
Senior Member
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 570
Thanks: 2
Thanked 25 Times in 22 Posts
Rep Power: 9
monk is just really nicemonk is just really nicemonk is just really nicemonk is just really nice
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; 20th July 2007 at 06:20 PM.
Reply With Quote
  #3 (permalink)  
Old 20th January 2008, 09:43 PM
Junior Member
 
Join Date: Jan 2008
OS: Solaris
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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 29th January 2008, 02:54 PM
Junior Member
 
Join Date: Jan 2008
OS: fedora
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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 27th August 2008, 09:22 PM
Junior Member
 
Join Date: Aug 2008
OS: Fedora 7
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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(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 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
  #6 (permalink)  
Old 23rd June 2010, 05:59 PM
Junior Member
 
Join Date: Jun 2010
OS: Ubuntu 10.04
Scripting language: Bash and friends
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
trotskyicepick is on a distinguished road
Default

I'm having the same problem, using the above scripts I can login to the remote server, but cannot execute any commands. I have added a command to the sshlogin.exp file as has MeeToo, but just get a response stating "invalid command name while executing .....".

Any ideas why this isn't working?

Thanks In Advance
Andrew

PS. Just tried adding Exec before the command to be run, in this case a tail command, I now get the following :

cannot open `/var/log/emaild.log' for reading: No such file or directory
tail: no files remaining
while executing
"exec tail -f /var/log/emaild.log"

Last edited by trotskyicepick; 23rd June 2010 at 06:02 PM.
Reply With Quote
  #7 (permalink)  
Old 23rd June 2010, 09:08 PM
nixcraft's Avatar
Never say die
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash, Perl, Python
Posts: 3,300
Thanks: 13
Thanked 413 Times in 306 Posts
Rep Power: 10
nixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond reputenixcraft has a reputation beyond repute
Default

Can you paste your script here? Also specify your UNIX and expect version.
__________________
Vivek Gite
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Do you run a Linux? Let's face it, you need help!
Cricket & IPL News Blog
Reply With Quote
  #8 (permalink)  
Old 25th June 2010, 02:00 PM
Junior Member
 
Join Date: Jun 2010
OS: Ubuntu 10.04
Scripting language: Bash and friends
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
trotskyicepick is on a distinguished road
Default

Hi, thanks for the speedy response.

I'm actually using Linux, Ubuntu 10.04 Lucid to be precise, and Expect v5.44.1.14. Please find my scripts attached.

Could the problem simply be that it won't work under Linux?

Regards
Andrew
Attached Files
File Type: txt sshlogin.exp.txt (1.3 KB, 0 views)
File Type: txt sshlogin.sh.txt (514 Bytes, 0 views)

Last edited by trotskyicepick; 25th June 2010 at 02:03 PM.
Reply With Quote
  #9 (permalink)  
Old 2nd July 2010, 01:57 PM
Junior Member
 
Join Date: Jun 2010
OS: Ubuntu 10.04
Scripting language: Bash and friends
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
trotskyicepick is on a distinguished road
Default

I take it that as there have been no responses for quite some time, 2 years in the case of MeeToo's enquiry, that either no one is interested, or no one knows the answer?
Reply With Quote
  #10 (permalink)  
Old 2nd July 2010, 03:05 PM
Junior Member
 
Join Date: Jun 2010
OS: Ubuntu 10.04
Scripting language: Bash and friends
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
trotskyicepick is on a distinguished road
Default

Solved problem.

Just use
spawn ssh -t $username@$ipaddr "tail -f /var/log/emaild.log"

and don't send the command at the end of the script.
Reply With Quote
Reply


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 01:35 PM.


Powered by vBulletin® Version 3.8.6 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38 39 40