简体   繁体   English

期望命令未在 ubuntu 焦点中传递 env 变量

[英]expect command is not passing env variable in ubuntu focal

I'm using expect (v5.45.4-2build1) to automate generation of client certs using easy-rsa in ubuntu focal.我正在使用expect (v5.45.4-2build1) 在 ubuntu 焦点中使用 easy-rsa 自动生成客户端证书。 As per easy-rsa, to customize cert validity we need to pass EASYRSA_CERT_EXPIRE variable.根据 easy-rsa,要自定义证书有效性,我们需要传递EASYRSA_CERT_EXPIRE变量。 But for some reason, my expect script is not able to fetch this variable.但由于某种原因,我的期望脚本无法获取此变量。 Here's the code for script.exp:这是 script.exp 的代码:

#!/usr/bin/expect -f

set timeout -1
#exp_internal 1

set MY_PASSPHRASE [lindex $argv 0];
set USERNAME [lindex $argv 1];
set ttl [lindex $argv 2];

send "export EASYRSA_CERT_EXPIRE=$ttl\r"
spawn /bin/bash

set MSG "Executing script.exp for $USERNAME and $ttl";
send "echo 'INFO $MSG'>> /var/log/app/my_app.log\r"

send "cd /etc/openvpn/easy-rsa\r"

send "export EASYRSA_CERT_EXPIRE=$ttl\r"

send "./easyrsa gen-req $USERNAME\r"
expect "*Enter PEM pass phrase:"
send -- "$MY_PASSPHRASE\r"
expect "*Verifying - Enter PEM pass phrase:"
send -- "$MY_PASSPHRASE\r"
expect "*Common Name *$USERNAME*"
send -- "\r"
expect "*Keypair and certificate request completed*"
send -- "\r"
exit 0

Strangely, this works fine if i run the script via expect script.exp .奇怪的是,如果我通过expect script.exp运行脚本,这可以正常工作。 But I want to use this in my perl script and it is not working from the perl script.但我想在我的 perl 脚本中使用它,它在 perl 脚本中不起作用。

This is how I'm calling my expect script:这就是我调用期望脚本的方式:

my $cmd_out_csr = system("expect script.exp $my_passphrase $username $ttl");

Just running, expect script.exp $my_passphrase $username $ttl from cmd works fine.刚刚运行, expect script.exp $my_passphrase $username $ttl工作正常。 Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

  1. use the perl system command with a list of arguments:使用带有 arguments 列表的 perl system命令:

     system('expect', 'script.exp', $my_passphrase, $username, $ttl);
  2. when creating the variable in the remote shell, send quotes around the value:在远程 shell 中创建变量时,在值周围发送引号:

     send "export EASYRSA_CERT_EXPIRE=\"$ttl\"\r"

These two things will protect the value of $ttl if it contains whitespace.如果$ttl包含空格,这两件事将保护它的值。


Another thought: expect (via Tcl) can cd and set environment variables.另一个想法:expect(通过 Tcl)可以cd和设置环境变量。 Instead of spawning a bash shell, spawn easyrsa directly:不要产生 bash shell,而是直接产生easyrsa:

#!/usr/bin/expect -f

set timeout -1
#exp_internal 1

lassign $argv MY_PASSPHRASE USERNAME ttl

set f [open /var/log/app/my_app.log a]
puts $f "INFO Executing [info script] for $USERNAME and $ttl"
close $f

cd /etc/openvpn/easy-rsa
set env(EASYRSA_CERT_EXPIRE) $ttl

spawn ./easyrsa gen-req $USERNAME

expect "*Enter PEM pass phrase:"
send -- "$MY_PASSPHRASE\r"
expect "*Verifying - Enter PEM pass phrase:"
send -- "$MY_PASSPHRASE\r"
expect "*Common Name *$USERNAME*"
send -- "\r"
expect "*Keypair and certificate request completed*"
send -- "\r"
expect eof

puts "Done."

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

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