简体   繁体   中英

Bash Script. Enter Username and Password

I'm trying to write a script that calls another one. Then it should fill in username and password. Unfortunately spawn does not call my script file.

I created both scripts and then created them with chmod +x filename.sh made it executable.

vpn.sh

#! /usr/bin/expect

spawn ./startvpn.sh


#expect "[sudo] Passwort für niclas:"
#send "**********"

#expect "Enter Auth Username:"
#send '*************'

#expect "Enter Auth Password:"
#send "******"

startvpn.sh

#! /bin/bash

sudo openvpn /etc/openvpn/ovpn_udp/se250.nordvpn.com.udp.ovpn

The plan:

I'm running ./vpn at the terminal, which then executes startvpn. This establishes a connection with Openvpn.

Then vpn.sh should enter the Sudo password and then my username and password for NordVpn.

Strangely enough, startvpn never runs.

I don't get an error message.

Update:

I updated my script. Now it is working except it isnt entering the password.

updated version of vpn.sh

#!/usr/bin/expect -f

spawn sudo openvpn /etc/openvpn/ovpn_udp/se250.nordvpn.com.udp.ovpn


expect "Passwort für niclas:"
send "******\r"

expect "Username:"
send "**************\r"

expect "Password:"
send "****\r"

The Console Output is:

niclas@niclas-Inspiron-7347:~$ expect vpn.sh 
spawn sudo openvpn /etc/openvpn/ovpn_udp/se250.nordvpn.com.udp.ovpn
[sudo] Passwort für niclas: 
Fri Jun 28 21:03:03 2019 OpenVPN 2.4.4 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on May 14 2019
Fri Jun 28 21:03:03 2019 library versions: OpenSSL 1.1.1  11 Sep 2018, LZO 2.08
Enter Auth Username: niclas.buerger@web.de
Enter Auth Password: niclas@niclas-Inspiron-7347:~$   

If I type the password normally it will be filled in with ******. So I really don't know why it doesn't work. The command is the same as the one with the username.

expect "[sudo] Passwort für niclas:"

Inside "" quote, [] will be act as tcl command subtitution syntax.

You need to use strict expect {} quite like:

expect {[sudo] Passwort für niclas:}

Or quote [] like:

expect "\[sudo\] Passwort für niclas:"

Or just use part of expected string:

expect "Passwort für niclas:"

Or just this:

expect "Passwort"

you need to have another line after send. this is working for me:

#!/usr/bin/expect -f

spawn openconnect ********

expect "Username:"

send -- "********\\r"

expect "Password:"

send -- "****\\r"

interact

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM