1

I am trying to create output from my Bash script which includes hostname, SSH protocol, and root login information.

I would like to do it using Bash functions. I have a .sh file but it does not work. Where is the problem in this code?

Server Version: Red Hat 7

The expected output would be:

xyz|hostname|Protocol X|Root Access Denied

I would like to begin the output with "xyz" in order to later parse my output.

#!/bin/bash
     
host(){
         local tmpfile=$(mktemp)
         hostname > "$tmpfile"
         printf '%s' "$tmpfile"
}
     
protocol(){
        local infile="$1"
     
        cat /etc/ssh/sshd_config  | grep Protocol
}
     
rootlogin(){
        local infile="$1"
     
        if [[ $(sudo cat /etc/ssh/sshd_config | grep -i "PermitRootLogin yes" | grep -v "#PermitRootLogin yes") = "PermitRootLogin yes" ]];      
                echo  $host
        else
                echo "Root Access Denied"
        fi
}
}

tmpfile=$( host )
{
        host "$tmpfile"
        protocol "$tmpfile"
        rootlogin "$tmpfile"
} > fonk.out
     
rm -f "$tmpfile"
1
  • 1
    You never mentioned in what way the script does not work, but you have at least one syntax error. The line that ends in Protocol} needs to look either like Protocol; } or the } should go on the next line. You also do some unnecessary grepping to test if PermitRootLogin is set (you could anchor the pattern to the start with ^PermitRootLogin yes). But the main thing looks like a syntax error typo.
    – Kusalananda
    Aug 15, 2018 at 6:29

1 Answer 1

1

The poor indentation and layout of your script muddles the question, but the basic answer is

printf '%s|%s|%s|%s\n' "$(field1)" "$(field2)" "$(field3") "$(field4)"

Refactored into this, and with indentation etc cleaned up, your script becomes

#!/bin/bash

host(){
    hostname
}

protocol(){
    # avoid useless use of cat; get rid of unused parameter
    # ... do you need sudo here too?
    grep Protocol /etc/ssh/sshd_config
}

rootlogin(){
    # straighten out massive spaghetti pretzel; remove unused parameter
    # ... can you avoid sudo cat here?
    if sudo cat /etc/ssh/sshd_config |
        grep -v "#PermitRootLogin yes" |
        grep -i -q "PermitRootLogin yes"
    then
        # Fix quoting
        echo "$host"
    else
        echo "Root Access Denied"
    fi
 }

printf '%s|%s|%s|%s\n' "$1" "$(host)" "$(protocol)" "$(rootlogin)" >fonk.out    

The last line is somewhat speculative; your current script doesn't seem to print the first field at all, and it's not clear what it's supposed to contain.

This no longer uses a temporary file, but one antipattern in your attempt was to create the temp file in an unrelated function. When you really do need a temp file, it would probably be a good idea to create it separately, then use it as a parameter everywhere. Like

tmp=$(mktemp) || exit
# arrange for temp file to be removed in case of errors or signals, too
trap 'rm -f "$tmp"' EXIT
trap 'exit' ERROR HUP QUIT TRAP TERM

function1 "$tmp"
function2 "$tmp"
: etc
1
  • thank you for your help and the information you provide. Aug 15, 2018 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .