Bash - here document on RHEL 6.8 and 6.9 servers aren't working

H Forum,

I was thankful in getting help from this post that allowed me to connect to multiple severs at once using here documents to gather data into variables. But I've discovered that the same bash command that works on my RHEL 7 servers do not work on RHEL 6? What's strange about my problem is that I'm not getting an error either. The problem is that the variables return empty when I try the following code on RHEL 6.8 or 6.9 servers we manage:

IFS=$'\t' read FLAVOUR HOSTNAME ACTIVE_KERNEL INACTIVE_KERNEL UPTIME <<- EOFREAD
        $ssh -qo StrictHostKeyChecking=no -o ConnectTimeout=1 user@$InputIP sudo -u user sh -s <<- EOFSSH | tr $'\n' $'\t'
        exec bash
        cat /etc/redhat-release 2>/dev/null || lsb_release -a 2>/dev/null | grep Description | cut -f2
        awk -F= '/^NAME/{print $2}' /etc/os-release 2>/dev/null || grep Red |cat /etc/redhat-release 2>/dev/null || lsb_release -a 2>/dev/null | grep Description | cut -f2
        hostname
        uname -r
        rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \; || echo "Can't List Ubuntu Kernels"
        uptime | cut -d "," -f1
        exit
EOFSSH
        )
EOFREAD

I've thought of adding in the ability to determine if my server is RHEL 6 or 7 using code like the following:

python -mplatform | grep -qi red &&

but even if I could determine RHEL version I'm still not sure why my RHEL 6 servers do not return variable data back. I've tested this above code connecting to just one server to try and triage the issue. No errors are returned. the variable fields are just empty. Whereas on my RHEL 7 servers I return back variables with no issue. Could it be that how I use Here Documents just doesn't work with RHEL 6 servers? has anyone seen this before?

I've also tested connecting to a RHEL 6 server multiple times to get the data I need. what I mean by this is instead of connecting to a RHEL 6 server once and collecting data I've tested connecting to a RHEL 6 for each command I need so connecting multiple times and there are no issues with the above commands I'm issuing.

Perhaps I'll just need to identify RHEL 6 servers and gather their data differently than I do for RHEL 7 severs but before I do that I thought I would check this forum for any advice first.

Thank you.

This is the crux of it I think. On the first look at your code I was suspicious that the redirect of stderr on one of the commands was causing you to miss an error causing this. So some nice debugging on you part here allows us to rule this out.

What does concern me is the exec bash line. This is unusual and I'm having trouble understanding how the commands that follow make it to this shell.
I think another here-document may be called for!

Please try:

IFS=$'\t' read FLAVOUR HOSTNAME ACTIVE_KERNEL INACTIVE_KERNEL UPTIME <<- EOFREAD
        $ssh -qo StrictHostKeyChecking=no -o ConnectTimeout=1 user@$InputIP sudo -u user sh -s <<- EOFSSH | tr $'\n' $'\t'
        exec bash <<- EOFEXEC
        cat /etc/redhat-release 2>/dev/null || lsb_release -a 2>/dev/null | grep Description | cut -f2
        awk -F= '/^NAME/{print $2}' /etc/os-release 2>/dev/null || grep Red |cat /etc/redhat-release 2>/dev/null || lsb_release -a 2>/dev/null | grep Description | cut -f2
        hostname
        uname -r
        rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \; || echo "Can't List Ubuntu Kernels"
        uptime | cut -d "," -f1
        exit
        EOFEXEC
EOFSSH
        )
EOFREAD

[/quote]

1 Like

Plus, bash creates a subprocess (different from ksh) on the right side of a pipe, meaning any changes to variables in the statement on the right are not returned to the parent. This changed with bash 4.2, so it may be what you are seeing with v6 versus v7. Check the listpipe bash option help for more information. But I think ChublerXL has a solid answer.

PS:

EOFSSH
        )
EOFREAD

I do not see the the opening ( in your example. Generates an error for me.

1 Like

Why that exec bash at all? What's the user's default shell? And, why do you run sh -s as the ssh command? Use bash if need be, it offers -s as well, should that be necessary at all.

Thanks all for the suggestions. I've decided to split how I gather my information from my servers into if statements so that my here doc code gathers information from my RHEL 7.x and Ubuntu servers and my else will be to gather data from my RHEL 6.x servers by connecting to each RHEL 6.x server each time I need to gather data from a command. It's not efficient because it's slower to connect to my RHEL 6.x servers multiple times to issue the commands, but luckily I don't have many so this system works out for me.

Thanks!