0

I am trying to create output which keeps hostname,ssh protocol and root login information with bash script.

And I would like to do it with developing functions. I developed .sh but it does not work.

Where is the problem in this sh?

Server Version Red Hat 7

my expected output should be;

xyz|hostname|Protocol X|Root Access Denied

And I would like to start to output "xyz" in order to pars my out but.

 #!/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" ]]; then
                       echo  $host
               else
                       echo "Root Access Denied"
 fi
 }

                                 }


 tmpfile=$( host )

    {
         host "$tmpfile"
         protocol "$tmpfile"
         rootlogin "$tmpfile"
 } > fonk.out

 rm -f "$tmpfile"
  • 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 '18 at 6:29
0

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
  • thank you for your help and the information you provide. – 1010111100011 Aug 15 '18 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.