简体   繁体   English

OSX bash和期望

[英]OSX bash & expect

I have a script that must perform a 'sudo' operation on each of a number of hosts. 我有一个脚本,必须在多个主机上的每个主机上执行“ sudo”操作。 I'm trying to get expect working so I only have to enter it once, and have run into a couple of issues. 我试图使工作正常,所以我只需要输入一次,就遇到了一些问题。

First, several examples suggest to use: 首先,建议使用几个示例:

/usr/bin/expect <<EOD
spawn ssh -t $host /usr/bin/sudo -v
expect "Password:"
send "$SUDOPASS\n"
EOD

However, as soon as I enter the second '<', all of the quoting gets screwed up, and my script will terminate with an unexpected end of file. 但是,一旦我输入第二个“ <”,所有的引用就搞砸了,我的脚本将以意外的文件结尾终止。 This is really strange, as I use the same mechanism with cat to write out files in other scripts. 这真的很奇怪,因为我对cat使用了相同的机制,以用其他脚本写出文件。

Other examples suggest: 其他示例表明:

/usr/bin/expect -c "
spawn ssh -t $host /usr/bin/sudo -v
expect "Password:"
send "$SUDOPASS\n"
"

Doing it this way (and adding -d to expect), I get errors about no tty and a repeat of my password with an 'n' appended to the end, so it isn't sending a newline (I've tried '\\r' as well), but instead appears to be escaping the 'n' 以这种方式进行操作(并在预期值上加上-d),我得到的错误信息是:没有tty并重复输入密码,并在末尾附加了'n',因此它没有发送换行符(我尝试过'\\ r”),但似乎转义了“ n”

Since most of the examples I'm finding are from Linux, I expect (ha, ha, pun not intended!) that they're using a GNU expect, and I'm using a BSD expect. 由于我发现的大多数示例都来自Linux,因此我希望(哈,哈,双关语不是故意的!)他们使用的是GNU期望,而我使用的是BSD期望。 I am reading through the man page, but the word "spawn" appears so many times, it could be quite a while before I stumble across the correct instance. 我正在阅读手册页,但是“ spawn”一词出现了很多次,可能要花相当长的时间才能偶然发现正确的实例。

Here's a result that seems to be closest to working: 这似乎是最接近工作的结果:

flamingo:~ jnojr$ Scripts/getinfo_expect.sh 
Assuming you have one 'sudo' password for all of your hosts, enter it now: 
expect version 5.45
spawn /usr/bin/ssh -t macbook /usr/bin/sudo -v
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {67358}

expect: does "" (spawn_id exp7) match glob pattern "Password:"? no
Password:
expect: does "Password:" (spawn_id exp7) match glob pattern "Password:"? yes
expect: set expect_out(0,string) "Password:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Password:"
send: sending "password\n" to { exp7 }
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = -c  argv[3] = 
    spawn /usr/bin/ssh -t macbook /usr/bin/sudo -v
    expect Password: { send password\n }  
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
sudo: no tty present and no askpass program specified[/CODE]

That's with leaving the '\\n' outside of the quotes, as in 就是将'\\ n'放在引号之外,例如

send "$SUDOPASS"\n

It seems to be mostly working, except skipping the '-t' option to ssh 除了跳过ssh的'-t'选项外,它似乎大部分都在工作

I second @BrianCain's comment that passwordless ssh w/ sudo would be a better solution. 我第二个@BrianCain的评论是,使用sudo密码ssh是更好的解决方案。 That said, let's try to help with your expect script and you can decide whether it continues to be too much of a headache. 就是说,让我们尝试帮助您的expect脚本,您可以决定它是否仍然令人头疼。

I believe you're going to want to use expect -f - (read the script from STDIN) when using the bash "here string" ( << ) to input the script to expect . 我相信您会在使用bash “ here string”( << )输入expect的脚本时使用expect -f - (从STDIN读取脚本)。 So: 所以:

/usr/bin/expect -f - <<EOD
spawn ssh -t $host /usr/bin/sudo -v
expect "Password:"
send "$SUDOPASS\n"
EOD

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

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