简体   繁体   English

如何提取远程VM的操作系统版本并将其设置为远程VM上的环境变量,还可以在expect脚本中进一步使用?

[英]How can I extract the OS version of a remote VM and set it as an environment variable on the remote VM and also to use further in the expect script?

I am trying to fetch the OS version of a remote VM and add it as an environment variable on the same vm and also use the fetched value further on the host vm from where the expect script is being executed. 我试图获取远程VM的OS版本并将其作为环境变量添加到同一个vm上,并且还在执行expect脚本的主机vm上进一步使用获取的值。

I am trying to export the value of the fetched value. 我试图导出获取值的值。 But the variable is not being set. 但是变量没有被设定。

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r"
expect -re $prompt

I am seeing below issue: 我看到下面的问题:

 can't read "vers": no such variable
    while executing
"send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r""

Value is set into the variable on the remote vm and I should be able to use it in the expect script. 值被设置到远程虚拟机上的变量中,我应该能够在期望脚本中使用它。

There are a couple of problems here. 这里有几个问题。 First, recall that expect uses the same syntax as sh for variable expression. 首先,回想一下, expect对变量表达使用与sh相同的语法。 That is, if I set a variable in an expect script: 也就是说,如果我在expect脚本中设置一个变量:

set somevar "somevalue"

I can print it out by running: 我可以通过运行打印出来:

puts "$somevar"

This means that when you have a command like this... 这意味着当你有这样的命令时......

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers`\r"

...then expect will attempt to expand the $vers variable before sending that text to the connected process. ...然后期望在将该文本发送到连接的进程之前尝试扩展$vers变量。 The error you're seeing comes from expect ; 你看到的错误来自于expect ; we can reproduce it like this: 我们可以像这样重现它:

expect1.7> puts "$var_that_does_not_exist"
can't read "var_that_does_not_exist": no such variable

You will need to escape the $ in your string, like this: 您需要在字符串中转义$ ,如下所示:

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo \$vers`\r"

That will send the intended command to your shell. 这会将预期的命令发送到你的shell。


I'm suspicious that there may be a second problem: you're setting the variable vers to the output of: 我怀疑可能存在第二个问题:您将变量vers设置为以下输出:

rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$' && echo $vers

It's not clear why you've got that echo $vers at the end. 目前尚不清楚为什么你最后会得到那个echo $vers Since $vers isn't set when you run this command, it's just going to add a blank line to the output. 由于运行此命令时未设置$vers ,因此它只是在输出中添加一个空行。 I believe you want simply: 我相信你想简单地说:

send "export vers=`rpm -q --queryformat '%{RELEASE}' rpm | grep -o '.$'`"

暂无
暂无

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

相关问题 用于登录远程 VM 并从远程 VM 运行命令的 Shell 脚本 - Shell script to login to remote VM and run commands from remote VM 如何使用Expect使用脚本进行远程部署 - How to use expect for remote deployment using script 我该如何在Expect脚本中的if条件中使用变量? - How can I use a variable in if condition in expect script? 如何ssh到远程ubuntu vm并将shell从csh更改为bash - How to ssh to remote ubuntu vm and change the shell from csh to bash 如何通过SSH运行Shell脚本,以使远程计算机的环境与本地计算机的环境相似? - How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer? 如何使用expect和bash脚本在远程服务器中运行本地脚本 - how to run a local script in remote server using expect and bash script 如何在bash脚本中编写期望代码段以运行远程命令? - How to write expect snippet in bash script to run remote command? 如何通过ssh从远程服务器运行expect脚本? - How to run expect script from remote server via ssh? 如果远程交换机在期望脚本中没有响应,如何继续下一个命令 - How to continue with next command if the remote switch is not responding in an expect script 如何从本地计算机上的远程主机上特定(但可变)目录中执行脚本? - How can I execute a script from my local machine in a specific (but variable) directory on a remote host?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM