Hello all,
This bellow code works. I'm just trying to find a simplified way to achieve this. I'm sure there is an easier way and it must be to simple for me to find.
Verify that the OS version is 6.1. If not exit the script. Of course if i just put a && exit 1 at the end of the ssh command it returns true so the script exits.
Thanks.
#!/bin/bash
OSVER=$(ssh -l root server1 "awk '\$7 != 6.1 {print \"NOTGOOD\"}' /etc/redhat-release")
if [ "$OSVER" == NOTGOOD ]; then
echo "ERROR: This release is for RHEL 6.1"
exit 1
fi
OSVER=$(ssh -l root server1 " grep -q '6.1' /etc/redhat-release && echo OK || echo NOTOK")
[ "$OSVER" = "NOTOK" ] && echo 'bad OS version ' && exit 1
Is this what you mean? Using OK, NOTOK or words like that as returns from ssh calls is very clear and easy to maintain.
Do you really need root to read /etc/redhat-release?? This seems dangerous.
You can use and check the return code instead of backticking text:
if ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" /etc/redhat-release
then
echo "Found version 6.1"
else
echo "Not version 6.1"
fi
of course i don't need root for that. That was mainly for testing purposes. Good catch tho
What i'm trying to do is get rid of the if loop.
[test the remote host for good version] - [good do nothing/not good exit]
I don't even need to manage the return code. Just exit if it doesn't find 6.1
A cleaner way than with an if loop.
Something like that....
[[ ssh -l user server1 "awk '\$7 !=6.1 {print \"NOTOK\"}' /etc/redhat-release" ]] && exit
what is an "if loop"?
You can use my example directly since the logic for 'if program ; then ; else somethingelse ; fi' is the same logic for 'program || somethingelse'
ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" || exit 1
i get the logic behind your example but the exit code (on the remote host) somehow doesn't get back to the shell (on the local host).
It hangs and of course when i kill it it gets exit again.
Replacing the || exit 1 by || echo test from your example returns me
# ssh -l user server1 "awk '\$7 == /6.1/ { exit 0 } END { exit 1 }'" || echo test
ctrl-c
Killed by signal 2.
test
bash#
Somehow the filename got left off of that. awk was trying to read data from standard input.
ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" /etc/redhat-release || exit 1
I'm not sure how i missed the filename ....
Anyhow ... since the echo is ran on both execution it looks like the first part of the cmd never gets to be successfull or that echo would never gets executed on the redhat 6.1 server.
#redhat 6.1
ssh -l user server1 awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" /etc/redhat-release || echo $?
1
#redhat 5.5
ssh -l root server2 awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" /etc/redhat-release || echo $?
1
I was only following your own code, since you're the one that actually has redhat servers to test with.
Could you post the contents of the relevant /etc/redhat-release's then?
The only pertinent information is the redhat version.
Thanks.
GOOD
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.1 (Santiago)
BAD
cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.5 (Tikanga)
in that case:
ssh username@server grep -q "'6\.1'" /etc/redhat-release || exit 1
1 Like
Works perfectly. So simple yet i was so far away. Go Canada!!