remote execute awk script

Hi gurus I need to execute awk command on remote machine and write output to stdout.

So far Ive tried:

ssh server "ifconfig | grep -A 1 "^eth" | awk '{if ($1~/^eth/) {temp=$1} else {print temp $0}}'"
ssh server "exec " < file_containing_above_command

But neither works
Without awk everything work, awk on local side also works. I thing problem will be somewhere with qoutes or escaping.
Thanks a lot

Yes, you need to escape the special characters (the $ in your case).
Or you could change your command like this:

ssh <hostname> /sbin/ifconfig | 
  awk '/^eth/ { 
    n = $1; getline; print n, $0 
    }'

To execute it with the local awk (if available).

To get everything executed remotely:

ssh <hostname> "/sbin/ifconfig | 
  awk '/^eth/ { 
    n = \$1; getline; print n, \$0 
    }'"

Nice works like a charm :slight_smile: is also possible to write it to one line ?

Yes, just remove the newline characters :slight_smile:

:slight_smile: thanks, - I did not expect that $ can be problem - I expected ' {} or ()

Now I am facing another problems:
I use ssh in script to access various systems (solaris, hp-ux, linux...) if I want remote use ifconfig somewhere it works somewhere not.

  • I found that that may be the PATH problem or something with sourcing and variables - I am not familiar with it and I am a little bit confused, but anyway it is recommend to use /sbin/ifconfig (as you did)
    OK but what if somewhere is ifconfig in another path ? Is possible to use classic ifconfig in script?

  • Second problem: Is possible to remote use bash script for loop on system which did not have bash ? Example is more than 1000 words:

#!/bin/bash
for hpux_server in `cat hpux_servers`
do
  output=`ssh $hpux_server "for lan in \$(/usr/sbin/lanscan | grep lan | awk '{print \$5}'); do /usr/sbin/ifconfig \$lan; done" | grep -v 127.0.0.1 | grep 'inet'`
  #       ssh hpux_server  "for lan in \$(/usr/sbin/lanscan | grep lan | awk '{print \$5}'); do /usr/sbin/ifconfig \$lan; done"
  echo -e "${hpux_server}:\n=============\n${output}\n=============\n=============\n" >> final_hpux_list
done

The commented line in above code works if it is not in bash script

  • Third problem: Is good practice to set environment variables when I log on server via script (to avoid expecting input) ? Is it necessary for script to have set environment variables or is there any better solution ?
ssh server "DISPLAY=:0; TERM=xterm; /sbin/ifconfig -a" | grep -v 127.0.0.1 | grep 'inet addr'

Many many thanks

No, as far as I know, ifconfig's position and syntax varies accross different operating systems,
so I would suggest to use a custom code for your environment.

Could you post the exact error message or explain what you mean by "not working"?

I'm not sure about this one. Some ssh client implementations support the -t/-T switch to enable/disable pseudo-tty allocation,
I don't know if setting the above mentioned variables has some impact.

Thank you for other replies :slight_smile:

[quote=radoulov;302453138]

Could you post the exact error message or explain what you mean by "not working"?

[quote]

ssh server  "for lan in \$(/bin/netstat -in | awk 'if (!(NR==1||NR==2)) {print \$1}'); do /sbin/ifconfig \$lan; done"

bash: !: event not found

I think problem will be with ! (bang or what is the name in English) I also tried escape it but when i did it then error is in awk not in bash

awk: line 1: syntax error at or near if

you must give a space after than ! and use and curly brackets for awk interpret :slight_smile:

ssh server "for lan in \$(/bin/netstat -in | awk '{if (! (NR==1||NR==2)) {print \$1}}'); do /sbin/ifconfig  \$lan ; done"
bash: !: event not found

You should also escape the bang (the ! character).

Thanks both :slight_smile: