简体   繁体   English

OSX在Expect脚本中使用变量

[英]OSX use a variable in Expect script

Using OSX I'd like to access $(pbpaste) within Expect. 我想使用OSX在Expect中访问$(pbpaste)。

So for example this alias connects to a copied ip address (assuming I copied it before executing it)- leaving me to enter the password: 因此,例如,该别名连接到复制的IP地址(假设我在执行之前复制了它)-留下我输入密码:

alias s='ssh $(pbpaste) -l root'

And this script connects me to the specific host listed in the script entering the password for me: 这个脚本将我连接到脚本中列出的特定主机,并为我输入密码:

#!/usr/bin/expect
spawn  ssh hostname -l username
expect "password:"
send "secret\n"
interact

I would like to incorporate the $(pbpaste) into the expect script so that the copied host is accessed and the password is entered for me too. 我想将$(pbpaste)合并到Expect脚本中,以便访问复制的主机并也为我输入密码。 How? 怎么样?

On a side note, I'm aware of ssh keys, and thats its generally poor form to put passwords in plain text. 附带一提,我知道ssh密钥,这就是将密码以纯文本格式放置的普遍形式。 In my situation this is unimportant and won't help me - the utility of getting the above EXPECT script working with $(pbpaste) variable as the hostname is more important to me. 在我的情况下,这并不重要,也无济于事-使上述EXPECT脚本与$(pbpaste)变量一起使用的实用程序对我来说更为重要,因为主机名。

Additionally, I would like to be able to pass the host as an argument instead of $(pbpaste) too. 另外,我也希望能够将主机作为参数而不是$(pbpaste)传递。 This is secondary to the original question though- one script for each? 这是原始问题的第二要点-每个问题一个脚本? Or a default of $(pbpaste) if no argument for host is provided? 如果没有提供host参数,则默认为$(pbpaste)? Which ever is easier to write/ understand. 哪个更容易编写/理解。

I can't test it, but this should work: 我无法测试,但这应该可以工作:

spawn ssh [exec pbpaste] -l username

Or with a command line argument: 或使用命令行参数:

#!/usr/bin/expect
if {$argc == 0} {
  set hostname [exec pbpaste]
} else {
  set hostname [lindex $argv 0]
}
spawn ssh $hostname -l username

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

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