Visit Our UNIX and Linux User Community

OSX bash & expect

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting OSX bash & expect
# 1  
Old 02-26-2013
Question OSX bash & expect

I have a script that must perform a 'sudo' operation on each of a number of hosts. 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"

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.

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'

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. 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.

Here's a result that seems to be closest to working:

flamingo:~ jnojr$ Scripts/ 
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
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

That's with leaving the '\n' outside of the quotes, as in
send "$SUDOPASS"\n

It seems to be mostly working, except skipping the '-t' option to ssh

Last edited by jnojr; 02-26-2013 at 02:13 PM..
# 2  
Old 02-26-2013
Maybe 'ssh -tt' for a better fake terminal ?
# 3  
Old 02-27-2013
Originally Posted by DGPickett
Maybe 'ssh -tt' for a better fake terminal ?
That isn't in the man page, but I tried it anyway. No difference Smilie

---------- Post updated 02-27-13 at 01:30 PM ---------- Previous update was 02-26-13 at 01:37 PM ----------

I found this link which gave me some useful info... removing preceding indentation from my expect lines got me going! However, now, lines where I echo text or variables interspersed with '/t' no longer contain tabs, but literal '/t' strings!
# 4  
Old 02-27-2013
I usually write expect using autoexpect. It is very fussy! Perhaps use quoted real tabs?
# 5  
Old 02-27-2013
Originally Posted by DGPickett
I usually write expect using autoexpect. It is very fussy! Perhaps use quoted real tabs?
The lines with the tabs are not in the expect portion. I did replace them with real tabs, and that works, but... jeez Louise! That isn't very maintainable!

Here's what I'm trying to get. In my non-expect version, I use:

  ssh $host ls /etc/cma.conf >/dev/null 2>&1
  if [ "$?" -eq 0 ]; then
    ssh -t $host 'sudo -v'
    HBSSVER=`ssh $host 'sudo cat /etc/cma.conf' | grep CMABuildNumber | cut -d'>' -f2 | cut -d '<' -f1`

That works... it returns a four-digit build number, which is exactly what I want. But that scrip prompts me for my sudo password once for each host it hits, which can be a pain. So in my expect version, I have:

  ssh $host ls /etc/cma.conf >/dev/null 2>&1
  if [ "$?" -eq 0 ]; then
/usr/bin/expect <<EOD
spawn ssh -t $host sudo -v
expect "Password:"
send "$SUDOPASS\r"
    HBSSVER=`ssh -t $host sudo cat /etc/cma.conf | grep CMABuildNumber | cut -d'>' -f2 | cut -d'<' -f1`

That is very literally the only difference between the two versions of my script. I am befuddled as to why that screws up tabs in other lines. Also, this way, my HBSSVER= line no longer works... it hangs at my cut statements. IIRC, that's why I was using 'sudo -v', so that the password could be entered and cached on the remote host, and the second line could just run without prompting.

And, yes, I know I technically could change permissions on the /etc/cma.conf file to no longer need sudo That isn't an option, either. If you've never dealt with HBSS, consider yourself lucky and just trust me Smilie
# 6  
Old 02-28-2013
Have you considered running these things on the root cron or a root service so they can be created with read permission to your id or your user's ids.

I guess tabs are meta to expect, and if they lose their quoting or escaping, they are just white space!

You should be able to send one block of code through, or on the command line of, one ssh session and get all your answers from the output on stdout and stderr.
echo '...'|ssh whoever@wherever -tt bash 2>&1 | sed '...'
ssh whoever@wherever -ntt '...' 2>&1 | while read l ; do ... done

BTW, the first actually becomes bash -c '...' on the remote side for non-trivial '...'

I am not a big fan of the here document '<<' ; there are places it is great, like plain text lists, but for shell it has less control than "echo '...' |ssh bash", where exactly what you say goes down the pipe unprocessed to a remote shell. If you need something on this side that ' blocks, slip out to ", do it , then out of " and back to '. If you want that same control over on the remote shell, ' becomes '"'"' (single-double-single-double-single). No surprises, no extra quote stripping. I try to stay away from '\' for similar reasons. They disappear with little warning.
# 7  
Old 02-28-2013
Unfortunately, what I'm doing is more of an "on demand" type of thing. If I absolutely had to, I could use a cron job, but that's a last resort.

In any case, someone in another thread gave me the hint that pushed me over the edge...

  ssh $host ls /etc/cma.conf >/dev/null 2>&1
  if [ "$?" -eq 0 ]; then
    if [ -f /tmp/ ]; then rm -f /tmp/; fi
cat >/tmp/ <<EOF
ssh -tt $host sudo grep CMABuildNumber /etc/cma.conf <<EOS
    chmod 755 /tmp/
    HBSSVER=`/tmp/ | grep CMABuildNumber | cut -d'>' -f2 | cut -d'<' -f1`
    #if [ -f /tmp/ ]; then rm -f /tmp/; fi

It is a lot uglier than I'd like, but it works.

Ahh, the mysteries of bash!

Previous Thread | Next Thread
Test Your Knowledge in Computers #490
Difficulty: Medium
Java source code is compiled into Java bytecode which is actual machine code.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Bash script + expect

im very happy to back for this forum I have servers with alias of double dns extentions: sample: I need to login to that severs and executing the set of commands if failed then try to login via... (0 Replies)
Discussion started by: prakash0106
0 Replies

2. Shell Programming and Scripting

Automated scp using shell & expect

Hi All, I have written a script to automate scp of files. Most of the times it works fine except few cases. I want your help and suggestions to fix these failures. I have used expect & shell to do the automated scp. Below is code $ cat scp.ksh #!/bin/ksh inputfile=$1 fdest_sid=$2... (8 Replies)
Discussion started by: veeresh_15
8 Replies

3. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/ <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/ #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

4. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

open application with spaces in name [bash][OSX]

Hi guys, I'm new here and new to shell scripting so don't be hard on me I'm trying to create a bash script to restart a process by name in Mac OSX. I have no problem killing the application, the problem comes when launching it again. I managed to store the path in a variable lets say ... (8 Replies)
Discussion started by: jonathanwiesel
8 Replies

6. Shell Programming and Scripting

expect in bash script

Hi, I'm writing a shell script that calls a few commands that prompt the user for two simple yes/no questions. if the answers are consistent (the first is a yes, the second is a no), what would my expect script look like? Google is only giving me answers for scripts where I telnet or ssh. right now... (3 Replies)
Discussion started by: js741
3 Replies

7. Shell Programming and Scripting

SSH Script is sticking, & sign not doing what I would expect

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/s for remsys in trumpetsnail angel delphin... (3 Replies)
Discussion started by: LRoberts
3 Replies

8. OS X (Apple)

bash script for dseditgroups in OSX

Hi there, Hope this isn't too complex of a script to try to make but this is what I'm trying to do. Create a new group and add local users that aren't admin to the group. So let's say the new group is 'cats' and 'dog' is the only admin on the machine. Let's say most machines only have one... (4 Replies)
Discussion started by: stop.the.stupid
4 Replies

9. Shell Programming and Scripting

#!/bin/bash and #1bin/sh command not found error on mac osx terminal/shell script

i am having a weird error on mac os x running some shell scripts. i am a complete newbie at this and this question concerns 2 scripts. one of which a friend of mine wrote ( a couple weeks ago and it's been running fine on another machine. then last week i wrote and it... (2 Replies)
Discussion started by: danpaluska
2 Replies

10. UNIX for Dummies Questions & Answers

Upgrading bash on Darwin (osx)

Hi, I have installed bash 3.2 via darwin ports, however when I try and change the shell i.e. chsh -s /opt/local/bin/bash is says its a non-standard shell? but if i run ./bash i get a new bash prompt with version 3.2? Thanks (3 Replies)
Discussion started by: c19h28O2
3 Replies

Featured Tech Videos