LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-26-2017, 07:06 AM   #1
arunabh_biswas
Member
 
Registered: Jun 2006
Posts: 92

Rep: Reputation: 16
Scripting issue


Hi LQ 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".

Thanks in Advance.
 
Old 09-26-2017, 08:05 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
It will help a lot if you post script content between [code] [/code] tags so that the whitespace is preserved.

Then I would suggest that you remove all the references to grep, cut, and sed and keep everything within awk. That will be simpler and faster.

Which distro is this for, including version? Or are there different distros on the remote servers?
 
Old 09-26-2017, 11:31 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
this is what it looks like as a real script:

Code:
#!/bin/bash

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
...it's still horrible.
you should get rid of about 90% of the pipes and make this into a real shell script, otherwise it will be humanly impossible to troubleshoot
:-(

Last edited by ondoho; 09-26-2017 at 11:34 AM.
 
1 members found this post helpful.
Old 09-27-2017, 01:13 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A single line script?
Here is a multi-line script, that is complicated enough. The major part runs on the local host.
Code:
#!/bin/bash
for i in `cat server-list.txt`
do
  echo "+++++++++ $i +++++++++"
  ssh -qnx "$i" '/sbin/ifconfig' |
  while IFS="" read line
  do
    case $line in
    (lo*)
      if=""
    ;;
    ([a-z]*)
      if="${line%% *}"
      addr=""
    ;;
    (*\ inet*:*)
      addr="${line#* inet*:}"
      addr="${addr# }"
      addr="${addr%%[ /]*}"
      case $addr in
      (127.0.0.1|::1)
        addr=""
      ;;
      esac
    ;;
    esac
    if [ -n "$if" -a -n "$addr" ]
    then
      read ip hst junk < <(getent hosts "$addr")
      echo "$if $addr $hst"
      addr=""
    fi
  done
done
 
1 members found this post helpful.
Old 09-28-2017, 03:34 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ is that - a full handout (gasp)?
i hope op has the decency to say thanks.
 
Old 10-17-2017, 02:20 AM   #6
arunabh_biswas
Member
 
Registered: Jun 2006
Posts: 92

Original Poster
Rep: Reputation: 16
m really sorry guys for the delay.....

thanks four your inputs. i've tried those but that didn't work as expected. For this time, i executed the paste commands individually and later merged them... that was really hectic.

i m still looking for a working solution. It is not mandatory to use paste command, anything will work which can put the interface, ip address and fqdn of that ip in the same line with a space or tab between them.
 
Old 10-17-2017, 02:55 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ only came back to say thanks after it "didn't work"...
 
Old 10-17-2017, 03:07 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
What's wrong with my post#4?
Did you run it on the command line?
Do you get an error message? Does it get stuck?
What output do you get, and what do you expect?
 
Old 10-17-2017, 11:19 AM   #9
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
One problem with the original script is the use of single quotes.
Code:
ssh -q $i 'paste <(/sbin/ifconfig -a|egrep -i "eth|bond"|awk '{print $1}') ...
The single quote after awk doesn't start a quoted string, it ends the string which started with paste. And a backslash doesn't seem to protect a single quote, so that's not a solution.

Maybe something like this:
Code:
ssh -q $i 'paste <(/sbin/ifconfig -a | awk -v IGNORECASE=1 "/eth|bond/ \"{print \$1}\"") ...'
Although, MadeInGermany's approach of only running ifconfig once and processing the output on the local machine has a lot of merit. Also notice he used getent instead of nslookup.
 
Old 10-17-2017, 11:39 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by arunabh_biswas View Post
m really sorry guys for the delay.....

thanks four your inputs. i've tried those but that didn't work as expected. For this time, i executed the paste commands individually and later merged them... that was really hectic. i m still looking for a working solution. It is not mandatory to use paste command, anything will work which can put the interface, ip address and fqdn of that ip in the same line with a space or tab between them.
Ok...so why don't you then post what YOU have written/done/tried on your own to get this going??? As you were told before, we WILL NOT write your script for you. Show your own efforts, and we can help you work through problems. If you don't...there is not much we can do for you.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash scripting issue... L1nuxn00b703 Linux - Newbie 5 11-01-2013 02:18 PM
shell scripting issue izual Linux - Newbie 11 01-15-2012 11:01 AM
[SOLVED] scripting issue fernfrancis Linux - Newbie 6 04-25-2010 06:21 AM
Scripting issue.. szahri Linux - Newbie 3 03-22-2007 02:04 AM
Scripting issue. dolvmin Linux - Software 8 09-18-2003 10:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration