LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-08-2015, 11:37 AM   #1
Ashish Sood
Member
 
Registered: Feb 2009
Location: Gwalior
Distribution: Fedora 22, RHEL 6,7
Posts: 180

Rep: Reputation: 15
Using expect with if condtion


Hi All,

I am new to expect, to trying to automate shh login but also like to check the condition,because first time ssh will ask for confirmation to add the public key "yes/no" but next time it won't, Please check my below script and let me know if i am using it in a correct way.

Quote:

#!/usr/bin/expect
#!/bin/sh

message1="Are you sure you want to continue connecting (yes/no)?"
message2="password"

spawn ssh root@192.168.10.3

if [ expect == $message1 ]
then
send "yes\r"
expect "password"
send "redhat\r"
fi

if [ expect == $message2 ]
then
send "redhat\r"
fi

interact
Output

Quote:
root@localhost script]# ./copy1.exp
invalid command name "message1="Are"
while executing
"message1="Are you sure you want to continue connecting (yes/no)?""
(file "./copy1.exp" line 4)
 
Old 07-08-2015, 01:08 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
A far better way would be to use SSH with digital certificates ... actually disallowing(!) password login altogether ... so that the script can connect to the host server without any sort of password challenge at all.

This would be secure, despite the absence of the password prompt: either you possess a copy of a known and acceptable (should be unique...) key, or you do not. Possession of a recognized key causes the door to open silently in front of you. Lack of a key (or, possession of a key that has been individually revoked ...) makes the door impenetrable.
 
  


Reply



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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 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
expect: line seems to match exactly, however expect thinks not deadeyes Linux - General 5 06-16-2010 06:05 PM
Calling expect scripts from other expect scripts sevapopov Linux - Software 3 04-03-2008 09:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:51 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