LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2018, 02:15 PM   #1
Bodiless Sleeper
LQ Newbie
 
Registered: Nov 2018
Posts: 1

Rep: Reputation: Disabled
Basically I need to open a expect script from a bash script


This is the bash script

Code:
#!/bin/bash

cd /home/my_user/Downloads
username="vpnbook"
password="h5bhea6u"
read -sp "Enter Sudo Password: " sudopassword

exec /home/my_user/Downloads/VPN2.exp
And this is the expect script

Code:
#!/usr/bin/expect

spawn sudo openvpn vpnbook-de4-tcp443.ovpn
expect "password for my_user: "
send "$sudopassword\r"

expect "Enter Auth Username: "
send "$username\r"

expect "Enter Auth Password: "
send "$password\r"

expect "$ "

EOF
When I run the bash script in terminal I get this output:

Enter Sudo Password: (here it waits until I enter the password and then it prints out the following text which is accidentaly or not the first line of code in the script) spawn sudo openvpn vpnbook-de4-tcp443.ovpn

And it just stops there
I tried putting source and sh instead of exec but it didn't help
I hardcoded the variables in expect script and it runs perfectly so I guess that'll have to do

Last edited by Bodiless Sleeper; 11-02-2018 at 04:53 PM.
 
Old 11-01-2018, 02:48 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
exec with a command "replaces the shell," which would mean that the variables would be lost.
sh would create a new shell, which also wouldn't contain the variables.

I'd expect source (or .) to work, but you say not.

Try exporting the variables in the bash script.
 
Old 11-01-2018, 03:35 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
expect is an extension to the tcl language. In addition to the above suggestions you can add command lines arguments to your expect to "pass" the username and password or just add the code to perform the same functions in your bash script to your expect script.
 
Old 11-06-2018, 12:47 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,801

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Wink

Quote:
Originally Posted by Bodiless Sleeper View Post
This is the bash script

Code:
#!/bin/bash

cd /home/my_user/Downloads
username="vpnbook"
password="h5bhea6u"
read -sp "Enter Sudo Password: " sudopassword

exec /home/my_user/Downloads/VPN2.exp
And this is the expect script

Code:
#!/usr/bin/expect

spawn sudo openvpn vpnbook-de4-tcp443.ovpn
expect "password for my_user: "
send "$sudopassword\r"

expect "Enter Auth Username: "
send "$username\r"

expect "Enter Auth Password: "
send "$password\r"

expect "$ "

EOF
I don't think that "username" and "password" are being passed to the environment seen by Expect.

You ought to be able to pass that information to the expect script on the command line as
Code:
exec /home/my_user/Downloads/VPN2.exp $username $password
and retrieve these within the Expect script with something like
Code:
set username [lindex $arg0]
set password [lindex $arg1]
You'll need to include the "-f" switch on the shebang line for this to work, though. And... the big caveat with this is that "username" and "password" would be visible to anyone who knows to execute "ps" while your Expect script is running. Also, if they were defined in your environment, you could see them with the right "ps" switch anyway.

You could also put the login information (say: "username/password") into a hidden file with permissions that hide it from everyone but you (and root) and open that file in the Expect script. That's a little more involved and my Expect is pretty rusty but you could do something like:
Code:
set pwfile ".vpninfo.dat"
set pw [open ./$pwfile r]
set pwrecord [split [read $pw] "\n"]
if {[string length $pwrecord] > 0} {
    # Split "pwrecord" to get sitename, username, password, whatever
    # How you do this depends on how you format the user/pw record(s)
    # If you have multiple records in the file you'll need a foreach{}
    # loop.
}
# Spawn the VPN connect command using the parsed information...
This is approximately what you'd need. (Hey... I did say my Expect was rusty. And my textbooks are in storage for the time being.) I suspect you can figure out the rest.

Have fun...

Last edited by rnturn; 11-06-2018 at 12:50 AM. Reason: Fixed broken shell script syntax
 
  


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
[SOLVED] Unable to execute expect script from bash script AND from shell jonaskellens Programming 9 04-05-2018 03:42 AM
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
How to use expect and Bash script together in Same script sagar666 Linux - Server 7 07-19-2016 09:22 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 - Newbie

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