Reading file from remote host?

flamingo:~ jnojr$ ssh -t macbook sudo -v
Password:
Connection to macbook closed.
flamingo:~ jnojr$ ssh -t macbook sudo cat /etc/cma.conf | grep CMABuildNumber
	<CMABuildNumber>2918</CMABuildNumber>
Connection to macbook closed.
flamingo:~ jnojr$ ssh -t macbook sudo -k
Connection to macbook closed.
flamingo:~ jnojr$ ssh -t macbook sudo cat /etc/cma.conf | grep CMABuildNumber

And here it sits until I Ctrl-C it.

The problem is, doing this inside of a loop for a number of hosts means I need to enter my password over and over and over again. I can use expect to feed the password to 'sudo -v', but it doesn't persist outside of the expect section. Inside of expect, supposedly there are variables like $expect_out(buffer) or $expect_out(0,string) but I'm still trying to figure out how to use them.

How can I get my above result without changing anything on the remote host?

How about this:

#!/bin/bash

read -sp "Remote passwd: " PASS

for host in macbook tleepermacbook
do
    ssh -tt $host sudo grep CMABuildNumber /etc/cma.conf << EOF
$PASS
EOF
done
1 Like

Because that will echo the value I want to stout, which doesn't do me any good... the value needs to be available to the script to echo out later. Every other value, which does not require root access, is loaded into a variable. There may be some other mechanism that can accomplish the same end, so I don't want to be fixated on variables.

ETA: You got me steered in the right direction, though! I wound up with:

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

It's messy and kludgy, but it works!

This should work without the need to create a dummy /tmp/hbss.sh script:

if ssh $host [ -f /etc/cma.conf ]
then
    HBSSVER=$(ssh -tt $host sudo grep CMABuildNumber /etc/cma.conf <<EOS 2> /dev/null | awk -F'[<>]' '{ print $3}'
$SUDOPASS
EOS
)
else
    echo "Cannot contact $host or /etc/cma.conf does not exist"
fi