-1

my script:

#!/bin/bash

ssh root@server_host '

cd /home

count=$(awk -v d1="$(date --date="now -5 minute" +"%d %b %Y %H:%M")" -v d2="$(date --date="now " +"%d %b %Y %H:%M")" '$0 > d1 && $0 < d2 || $0 ~ d2' xyz.log|grep "Error Message" |wc -l)

if [ $count -gt 0 ];then status=green else status=yellow fi

timestamp=$(date --date="now - 5 minute" +"%d %b %Y %H:%M")

comment=""

if [ $status == green ];then

comment=$(awk -v d1="$(date --date="now -5 minute" +"%d %b %Y %H:%M")" -v d2="$(date --date="now " +"%d %b %Y %H:%M")" '$0 > d1 && $0 < d2 || $0 ~ d2' xyz.log |grep "Error Message" |tail -1) echo "$(hostname),$status,$timestamp,$comment"

else

comment=$(awk -v d1="$(date --date="now -5 minute" +"%d %b %Y %H:%M")" -v d2="$(date --date="now " +"%d %b %Y %H:%M")" '$0 > d1 && $0 < d2 || $0 ~ d2' xyz.log | tail -1) echo "$(hostname),$status,$timestamp,$comment"

fi

'>/csvfiles/LogsReport.csv

1 Answers1

1

SSH won't be able to execute scripts like that. What you could do is

#!/bin/bash

ssh root@server_host > /csvfiles/LogsReport.csv <<EOF
cd /home
...
<and the rest of your script>
EOF

More info on this syntax at Here documents.

Kris
  • 21
  • 4
  • You still can't pass a complete script to SSH in quotes. You'll need to use a "Here document" as shown above, or put the lines in quotes in a separate script and execute that. – Kris Dec 29 '20 at 13:34
  • Hi, FYI I will be executing ssh root@host ' ' from a common root server access from where I can login to any server with username root, my script will be residing on common host only e.g my script run.sh is on host pq34 and I wanted to login to pq30 in my script and must save all the contents in a csv on pq34 only. We can do it by doing run.sh: ssh root@pq30 ' cd /home my script content #closing server logged in with ' '>>/csv/logsrepprt.csv #overwriting the content in csv – rohit shinde Dec 29 '20 at 13:43