Scripting issue

Hi Experts,

I'm stuck with a single liner bash script. Need your help to fix it.

Let me brief you that what is my expectations from this script.

I need the interfaces configured for a list of servers, their respective IP address and FQDN in a output file. The output should be in the same line.

Here is the script which I'm running it from a jump server having ssh access with pasword.

for i in `cat server-list.txt`;do echo "+++++++++ $i +++++++++";ssh -q $i 'paste <(/sbin/ifconfig -a|egrep -i "eth|bond"|awk '{print $1}') <(/sbin/ifconfig -a|egrep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d ':' -f 2) <(/sbin/ifconfig -a|egrep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d ':' -f 2|xargs -n1 nslookup | grep -v nameserver | cut -f 2 | grep name | cut -f 2 -d "=" | sed 's/ //')';echo "========================="; done > output.txt

Output should look like this -

+++++++++ name of server1 +++++++++
eth1 <ip-of-eth1> <fqdn>
eth3 <ip-of-eth3> <fqdn>
eth4 <ip-of-eth4> <fqdn>
=========================
+++++++++ name of server2 +++++++++
eth1 <ip-of-eth1> <fqdn>
eth3 <ip-of-eth3> <fqdn>
eth4 <ip-of-eth4> <fqdn>
=========================
+++++++++ name of server3 +++++++++
eth1 <ip-of-eth1> <fqdn>
eth2 <ip-of-eth2> <fqdn>
=========================

When I execute it, it gives "paste: /dev/fd/63" No such file or directory".

It is not mandatory to use paste command, anything will work which can put the interface (eth0, ethx, bondx etc) ip address:wall and fqdn of that ip in the same line with a space or tab between them.

Thanks in Advance.

:wall::wall::wall:

I don't suppose redirection works inside ssh. Try this:

ssh -q $i "eth=$(/sbin/ifconfig -a|egrep -i 'eth|bond'|awk '{print $1}'); ip=$(/sbin/ifconfig -a|egrep 'inet addr'|grep -v '127.0.0.1'|awk '{print $2}'|cut -d':' -f2); hst=$(host $ip | awk '{print $NF}'); echo $eth $ip $hst";

If you want somebody to help you, please ...

  1. use CODE-Tags
  2. format your code(structure, intendation, comments) so it is easier to understand
1 Like

Welcome arun_adm

I think your so-called one-liner is not. It is multiple commands (one of them over-complex) joined on a single line, making it hard to read.

Here it is written more sensibly (with a few tweaks too):-

while read target_server
do
   echo "+++++++++ ${target_server} +++++++++"
   ssh -q ${target_server} 'paste \
         <(/sbin/ifconfig -a|egrep -i "eth|bond"|awk '{print $1}') \
         <(/sbin/ifconfig -a|egrep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d ':' -f 2) \
         <(/sbin/ifconfig -a|egrep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d ':' -f 2| \
            xargs -n1 nslookup | grep -v nameserver | cut -f 2 | grep name | cut -f 2 -d "=" | sed 's/ //')'
   echo "========================="
done < server-list.txt > output.txt

I have used a meaningful variable name rather than just i and read the file into a while loop. It is clearer to read and the input does not risk get eaten up by the commands within the loop.

The line ending \ can have nothing after it. It means that the new-line is ignored, so on execution it is joined up but means it is somewhat easier to read.

I would suggest you are trying to do too much in one step with pipes from the output to grep, then awk, then cut, then xargs, then cut, then cut, then grep, then cut and finally sed :eek:

A tool like awk can do all of this (although it might need to call out to nslookup) which will neaten your code too.

Can you explain what you are actually trying to achieve? I think I've got it, but if I have misinterpreted, I don't want to write it out and have you think that I've got it.

Thanks, in advance,
Robin