2

I am trying to grep the third octet in IP address to an tap device on remote machine.

ssh -t user@host "/sbin/ifconfig tap0 | grep "inet" | /usr/bin/awk -F'[: ]+' '{ print $4 }' | awk -F'[.]' '{print $3}'"

I am resulting this:

inet addr:10.22.66.77  Bcast:10.22.66.255  Mask:255.255.255.0

When i run the command on the remote machine it shows 66

How to make it working with ssh -t?

1 Answers1

2

Sometimes using perl is simpler:

ssh -t user@host "/sbin/ifconfig tap0" | perl -n -e 'if (/inet\saddr:\d+\.\d+\.(\d+)/) { print "$1\n"}'

it runs regular expression pattern on the local machine match on the third octet following addr: and this is then printed via $1

The pattern match is run on the local machine to avoid problems with escaping " (In your example code the " in the grep inet seems to terminate the ssh...)

monok
  • 494
  • 5
  • 16