Dummy script assistance

Hi,
I am new in ksh scripting and if anyone can help that would be great.

I'm writing a script which will SSH to several machines and then would append a certain file from a NAS to the /etc/sudoers file
the problem i am having is after the script connects to a certain server the commands are not done on the remote machine but on the machine i run the script from.

the script is:

#!/bin/ksh
SERVERS=`cat servers.log`
for i in $SERVERS
do
  ssh -n -T $i
  echo "i'm on $i"
  cp /etc/sudoers /etc/sudoers.bkp
  cat /system_kits/tmp/gal/lsof >> /etc/sudoers.gal
  echo "==========Giving lsof sudo permission on  $i ===================="
done

what am I doing wrong? how make so that the commands will run on the remote machine?

Regards,
Gal Uzan

ssh doesn't magically take the lines below it because it's not part of your shell. If you ran 'cat' you wouldn't expect it to eat the lines below it, you have to redirect whatever you want into it. You have to treat ssh like any other program.

Also, you have a useless use of cat and useless use of backticks in there. It's a bad habit since it's dangerous -- there's a limit to the size of a shell variable which can be surprisingly small on some shells and systems. Use 'while read' instead, it's also more efficient.

while read LINE
do
        ssh -T username@"$LINE" <<"EOF"
                echo "I'm on $HOSTNAME"
                cp /etc/sudoers /etc/sudoers.bkp
                cat /system_kits/tmp/gal/lsof >> /etc/sudoers.gal
                echo "==========Giving lsof sudo permission on  $HOSTNAME ===================="
EOF
done < servers.log

The last EOF needs to be on the beginning of the line. HOSTNAME is a special variable which the host ought to set for you.

1 Like

excuse me if my questions are novice

when using

while read line

the loop will read each line seperatley and insert the line into the $LINE variable? And this will read the list from servers.log specified at the end of the loop?

Also to confirm I understood from google -

<<"EOF"

means that the loop will run on the specific line untill it reads EOF as input...correct?

Yep.

Yep.

Nope. That's a here-document. Try typing this in your terminal:

$ cat <<"EOF"
This
is
some
text
that
I
am
typing
EOF

This
is
some
text
that
I
am
typing
$

It feeds the text inside <<"EOF" ... EOF into the standard input of the program. That's one way to feed the lines into ssh. Another would be echo or printf, and so forth.

1 Like

So 'EOF' can be anything from 'bla' to 'gal' ?

Yes, EOF can be anything.

Also note that the quotes are important.

VAR=5

$ cat <<OMG
VAR=${VAR}
OMG

VAR=5

$ cat <<"WTF"
VAR=${VAR}
WTF

VAR=${VAR}

$

Putting the first one in quotes causes it not to substitute variables inside.

So the quotes keep the variables to be the same as I defined on my terminal before I ran the script?

The quotes do exactly what I showed above. <<EOF will substitute variables before it sends it to the server, so scripts written in it won't work right unless you escape every $ like \$. <<"EOF" won't substitute anything, it'll send the text raw, so scripts will work but you won't get any variables from your local system.