Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-09-2012, 12:45 PM   #1
LQ Newbie
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
Bash (with expect) script for ssh login

Hi all,

I'm writing a bash script to login into my servers using a set of few regularly used passwords and collect stats.
1. N possible passwords
2. Multiple ssh attempts.

However written, I'm unable to either try out all passwords or 3 time ssh login attempts. I'm unable to exactly figure out how to achieve this -


passwords=(abc123 Password123 new123 linux_2 thomas45)

echo "IP of the server to connect :"
read IP


if [$i -lt ${#passwords[@]}]      # Try all known Passwords

while [$VAR="true"]

for j in {1..3}                  # Try 3 SSH attempts else re-spawn
VAR=$(expect -c "
spawn ssh root@$IP -o StrictHostKeyChecking=no
"password:" {send "${passwords[$i]}\r"}
"#" {interact}

# Collect server stats here

Old 09-09-2012, 03:43 PM   #2
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
did you look at?
1 members found this post helpful.
Old 09-11-2012, 09:38 AM   #3
LQ Newbie
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi Ygrex,
I tried out sshpass. Its a good resource. But every time I successfully login into ssh, my script stops and the prompt of the logged in device is displayed
Old 09-11-2012, 04:15 PM   #4
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
did you try using ssh-keygen?
Old 08-03-2018, 03:05 AM   #5
LQ Newbie
Registered: Jan 2009
Posts: 15

Rep: Reputation: 1
need help

I have list of servers with 2 different domain name.


single host with 2 different domain alias names for R&D purpose

now im trying to execute the remote command for file modification with help of expect and spawn

in my environment sometimes will login and pinging so no issues for executing the scripts with but sometimes not reachable and will reaching. so I cant able to execute the script. here my script below

this script is working only for one suffix domain.

I want to run this script, if not reachable then try

I have 5k servers with 2 domain alias.

my host list file have the below content

those servers have 2 domain alias for R&D purpose

help me
Old 08-03-2018, 12:06 PM   #6
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
@prakash0106: Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. Discussion should continue in your other thread as it already has replies.

Also, please do no post to older, inactive threads as many members will not see it and your problem may differ from the case previously posted.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
Auto SSH-login expect script creatorrr Linux - Software 7 10-26-2011 03:09 AM
Expect with bash script edwardcode Programming 14 08-13-2011 08:06 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration