Run awk command on remote host

I have below command to check for error logs from last 24 hours from the file : /var/log/messages/ The command is working fine on the local host.

sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages | egrep -i "error|fail"

I want to run the above command on a remote host. I tried below command but it is not working. (Note :- I provided remote host ipaddress instead of "hostname" in the below command)

ssh hostname -t sudo 'awk -F - -vDT="\$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < \$1' /var/log/messages'
-bash: $1 /var/log/messages: No such file or directory

Kindly help.

Thanks
Rahul

Looks like the redirection is expanded locally, and "$1 /var/log/messages" is not an existing local file. I'm puzzled with your quoting and escaping - try again starting with a simplified command, and then increase its complexity.

Save the command in a file, say "mycommand"
Run it locally

sh mycommand arg1 arg2

Run it remotely

ssh hostname sh -s arg1 arg2 < mycommand

good way to shoot in your own knee.

ssh hostname sudo awk -F - -vDT=\"$(date -v-24H "+%b %_d %H:%M:%S")\" ' "DT<\$1 {print}" ' /var/log/messages

To avoid quoting nightmares I would use a here-doc like this:

ssh hostname -t <<"EOF"
  sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages
EOF

Hello agent.kgb,

I tried the following commands but it didn't worked :-

$ ssh hostname sudo awk -F - -vDT=\"$(date -v-24H "+%b %_d %H:%M:%S")\" ' "DT<\$1 {print}" ' /var/log/messages
date: invalid option -- 'v'
Try 'date --help' for more information.
sudo: sorry, you must have a tty to run sudo
$ ssh -t hostname sudo awk -F - -vDT=\"$(date -v-24H "+%b %_d %H:%M:%S")\" ' "DT<\$1 {print}" ' /var/log/messages
date: invalid option -- 'v'
Try 'date --help' for more information.
[sudo] password for oracle:
Jan 22 03:42:02 hostname rhsmd: In order for Subscription Manager to provide your system with updates, your system must be registered with the Customer Portal. Please enter your Red Hat login to ensure your system is up-to-date.
Jan 22 03:50:01 hostname systemd: Started Session 41416 of user root.
Jan 22 03:50:01 hostname systemd: Starting Session 41416 of user root.
Jan 22 04:00:01 hostname systemd: Started Session 41417 of user root.
Jan 22 04:00:01 hostname systemd: Starting Session 41417 of user root.
Jan 22 04:01:01 hostname systemd: Started Session 41418 of user root.
Jan 22 04:01:01 hostname systemd: Starting Session 41418 of user root.
Jan 22 04:10:01 hostname systemd: Started Session 41419 of user root.
Jan 22 04:10:01 hostname systemd: Starting Session 41419 of user root.
Jan 22 04:20:01 hostname systemd: Started Session 41420 of user root.
Jan 22 04:20:01 hostname systemd: Starting Session 41420 of user root.
Jan 22 04:30:01 hostname systemd: Started Session 41421 of user root.
Jan 22 04:30:01 hostname systemd: Starting Session 41421 of user root.
Jan 22 04:40:01 hostname systemd: Started Session 41422 of user root.

Kindly help.

Thanks
Rahul

---------- Post updated at 12:20 PM ---------- Previous update was at 12:16 PM ----------

Hello,

I tried the command but it didn't worked. Kindly help :-

$ cat command
sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages
$ ssh hostname sh -s < command
sudo: sorry, you must have a tty to run sudo
$ ssh -t hostname sh -s < command
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: sorry, you must have a tty to run sudo

Thanks
Rahul

---------- Post updated at 12:23 PM ---------- Previous update was at 12:20 PM ----------

Hello,

The command didn't worked. Kindly help :-

$ cat script.ksh
ssh hostname -t <<"EOF"
  sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages
EOF
$ ./script.ksh
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: sorry, you must have a tty to run sudo

The sudo errors are because ssh is objecting to -t and refusing to allocate a tty. Try -t -t perhaps to convince it to do so even when it thinks its wrong.

1 Like

If you change in /etc/sudoers

Defaults   requiretty

to

Defaults   !requiretty

then you do not need a tty for sudo.

This doesn't mean that things requiring a password won't still need one, of course, and it can't read a password without a tty.