简体   繁体   English

Bash 脚本。 输入用户名和密码

[英]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.不幸的是 spawn 没有调用我的脚本文件。

I created both scripts and then created them with chmod +x filename.sh made it executable.我创建了两个脚本,然后使用chmod +x filename.sh创建它们使其可执行。

vpn.sh 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启动vpn.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.我在终端上运行 ./vpn,然后执行 startvpn。 This establishes a connection with Openvpn.这将建立与 Openvpn 的连接。

Then vpn.sh should enter the Sudo password and then my username and password for NordVpn.然后 vpn.sh 应该输入 Sudo 密码,然后是我的 NordVpn 用户名和密码。

Strangely enough, startvpn never runs.奇怪的是,startvpn 从不运行。

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 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.在 "" 引号内, [] 将作为 tcl 命令替换语法。

You need to use strict expect {} quite like:您需要像这样使用严格的 expect {}:

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 #!/usr/bin/expect -f

spawn openconnect ********生成 openconnect ********

expect "Username:"期待“用户名:”

send -- "********\\r"发送 -- "********\\r"

expect "Password:"期待“密码:”

send -- "****\\r"发送 -- "****\\r"

interact相互影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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