LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-09-2012, 12:45 PM   #1
ThomasIvan
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.
Requirements
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 -

Code:
#!/bin/bash

passwords=(abc123 Password123 new123 linux_2 thomas45)

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

i="0"

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

while [$VAR="true"]
do

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

# Collect server stats here

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

Rep: Reputation: 68
did you look at? http://sourceforge.net/projects/sshpass/
 
1 members found this post helpful.
Old 09-11-2012, 09:38 AM   #3
ThomasIvan
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
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
did you try using ssh-keygen?

http://rcsg-gsir.imsb-dsgi.nrc-cnrc....et/node31.html
 
Old 08-03-2018, 03:05 AM   #5
prakash0106
LQ Newbie
 
Registered: Jan 2009
Posts: 15

Rep: Reputation: 1
need help

I have list of servers with 2 different domain name.

pp1.servera.com
pp1.serverb.com

pp2.servera.com
pp2.serverb.com

......

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 pp1.servera.com will login and pinging so no issues for executing the scripts with x.servera.com but sometimes pp1.servera.com not reachable and pp1.serverb.com 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 x.servera.com not reachable then try x.serverb.com

I have 5k servers with 2 domain alias.


my host list file have the below content
pp1
pp2
pp3
pp4
pp5

those servers have 2 domain alias for R&D purpose

.servera.com
.serverb.com

help me
 
Old 08-03-2018, 12:06 PM   #6
astrogeek
Moderator
 
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.
 
  


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 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration