LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-07-2014, 10:46 PM   #1
sagar666
Member
 
Registered: Feb 2013
Posts: 212

Rep: Reputation: Disabled
How to use expect and Bash script together in Same script


Hi,

I want to use expect and Bash script in same script. How can i use that?

Below is the example what i need actually

-->I want to login server1 (For that i want to use expect script to login machine)
-->Next i wanted to some testing using Bash command
-->Exit from Server1 Machine

Same procedure for Server2,server3 and So on
 
Old 12-07-2014, 10:59 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

can you explain why you need to use expect to log into the machine?

Are you using ssh? If so, you should use public-key authentication so that you don't need to enter a password for each login.

Regards,

Evo2.
 
Old 12-07-2014, 11:42 PM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
exactly agreed with evo2..

Expect and send are useful for some testing purpose but not for all, So what you are trying to achieve with this ?

Please show us what have you tried till now ..
 
Old 12-08-2014, 12:09 AM   #4
sagar666
Member
 
Registered: Feb 2013
Posts: 212

Original Poster
Rep: Reputation: Disabled
Hi Evo2 and Sabhi,

I am using ssh only. As per your instruction i will use public-key authentication. May i have steps or document to create password less auth using ssh.

Thanks
 
Old 12-08-2014, 12:43 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

to set it up you need to run "ssh-keygen" on the local machine and then distribute the resulting public key to each of the target machines using "ssh-copy-id server1" for each serverN.

Once you have done the above you'll need to run ssh-add once each session to enter your passphrase. After doing so, for the rest of the session you can ssh to each serverN without using a password. The ssh-add command will connect to the local ssh-agent which will have been automatically started by most modern desktops. Otherwise you can start it manually with:
Code:
eval `ssh-agent`
before running ssh-add.

There are many tutorials online. Post back with questions if you get stuck.

HTH,

Evo2.
 
Old 12-10-2014, 08:47 AM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by sagar666 View Post
Hi Evo2 and Sabhi,

I am using ssh only. As per your instruction i will use public-key authentication. May i have steps or document to create password less auth using ssh.

Thanks
follow the links in my signatures for HOWTO guides on setting up your ssh keys.
 
Old 07-19-2016, 09:04 AM   #7
baber
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Rep: Reputation: Disabled
i have the same problem i want in bash just get a parameter then in expect connect to second server and echo a file with that parameter

when i run just expect work correctly
Code:
#!/usr/bin/expect -f
set timeout 3
spawn ssh  vi-admin@172.20.35.222
expect "password:"
send  "P@ssw0rd6555\r"
expect "$"
send  "echo /usr/lib/vmware-vcli/apps/general/credstore_admin.pl add  >/tmp/96666665.txt\r"
send "exit\r"

expect eof"

but when i write below code not work and get error
Code:
#!/bin/bash
read p
#!/usr/bin/expect -f
set timeout 3
spawn ssh  vi-admin@172.20.35.222
expect "password:"
send  "P@ssw0rd6555\r"
expect "$"
send  "echo /usr/lib/vmware-vcli/apps/general/credstore_admin.pl add $p/tmp/96666665.txt\r"
send "exit\r"

expect eof"

get this error

Code:
./nn.sh: line 5: spawn: command not found
couldn't read file "password:": no such file or directory
./nn.sh: line 7: send: command not found
couldn't read file "$": no such file or directory
./nn.sh: line 9: send: command not found
./nn.sh: line 10: send: command not found
./nn.sh: line 12: unexpected EOF while looking for matching `"'
./nn.sh: line 13: syntax error: unexpected end of file

what do i have to do ?
how can solve this problem?

Best regards
 
Old 07-19-2016, 09:22 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by baber View Post
i have the same problem i want in bash just get a parameter then in expect connect to second server and echo a file with that parameter when i run just expect work correctly
Code:
#!/usr/bin/expect -f
set timeout 3
spawn ssh  vi-admin@172.20.35.222
expect "password:"
send  "P@ssw0rd6555\r"
expect "$"
send  "echo /usr/lib/vmware-vcli/apps/general/credstore_admin.pl add  >/tmp/96666665.txt\r"
send "exit\r"
expect eof"
but when i write below code not work and get error
Code:
#!/bin/bash
read p
#!/usr/bin/expect -f
set timeout 3
spawn ssh  vi-admin@172.20.35.222
expect "password:"
send  "P@ssw0rd6555\r"
expect "$"
send  "echo /usr/lib/vmware-vcli/apps/general/credstore_admin.pl add $p/tmp/96666665.txt\r"
send "exit\r"
expect eof"
get this error
Code:
./nn.sh: line 5: spawn: command not found
couldn't read file "password:": no such file or directory
./nn.sh: line 7: send: command not found
couldn't read file "$": no such file or directory
./nn.sh: line 9: send: command not found
./nn.sh: line 10: send: command not found
./nn.sh: line 12: unexpected EOF while looking for matching `"'
./nn.sh: line 13: syntax error: unexpected end of file
what do i have to do ? how can solve this problem?

Best regards
While this is on topic, it's best not to reopen old threads...visibility is MUCH better if you open your own thread for your own question. Also, please see the "Question Guidelines" link in my posting signature.

That said, there are several example scripts that you can find to do this, just by putting "how to use expect in a bash script", into Google, such as this:
http://sharadchhetri.com/2010/12/07/...n-bash-script/

Again, though, as was said to sagar666 when he opened this thread, you *REALLY* shouldn't do this. It is NEVER, EVER a good idea to hard code a user ID and password into a script...from a security standpoint, it makes the remote machine vulnerable, and you can script much more easily if you do a key swap (as described in this thread), to accomplish what you're after. Once you do the keyswap, read the man page on the ssh command, and pay particular attention to the "-c" flag, which will let you run your command.
 
  


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 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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
Expect / BASH script Gabel__ Programming 0 07-25-2012 03:33 AM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 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 > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:12 PM.

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