Run awk command on remote host


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run awk command on remote host
# 1  
Old 01-24-2017
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.

Code:
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)

Code:
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
# 2  
Old 01-24-2017
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.
# 3  
Old 01-24-2017
Save the command in a file, say "mycommand"
Run it locally
Code:
sh mycommand arg1 arg2

Run it remotely
Code:
ssh hostname sh -s arg1 arg2 < mycommand

# 4  
Old 01-24-2017
good way to shoot in your own knee.
Code:
ssh hostname sudo awk -F - -vDT=\"$(date -v-24H "+%b %_d %H:%M:%S")\" ' "DT<\$1 {print}" ' /var/log/messages

# 5  
Old 01-24-2017
To avoid quoting nightmares I would use a here-doc like this:

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

# 6  
Old 01-25-2017
Quote:
Originally Posted by agent.kgb
good way to shoot in your own knee.
Code:
ssh hostname sudo awk -F - -vDT=\"$(date -v-24H "+%b %_d %H:%M:%S")\" ' "DT<\$1 {print}" ' /var/log/messages

Hello agent.kgb,

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

Code:
$ 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 ----------



Quote:
Originally Posted by MadeInGermany
Save the command in a file, say "mycommand"
Run it locally
Code:
sh mycommand arg1 arg2

Run it remotely
Code:
ssh hostname sh -s arg1 arg2 < mycommand

Hello,

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

Code:
$ 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 ----------

Quote:
Originally Posted by Chubler_XL
To avoid quoting nightmares I would use a here-doc like this:

Code:
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,

The command didn't worked. Kindly help :-

Code:
$ 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

# 7  
Old 01-25-2017
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.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

Execute command on remote host via ssh

How should i make the following code working #!/bin/bash INPUTFILE="test.txt" while read STRING; do IP=`host -t A $STRING | awk '{print $NF}'` HOSTNAME=`ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no $IP "hostname"` echo $HOSTNAME > out.txt done < $INPUTFILE At this moment while... (3 Replies)
Discussion started by: urello
3 Replies

3. Shell Programming and Scripting

Run script on remote host

Hi friends, I have two servers. Server A and B. I want to run one script on server A by logging in to server B. Can anyone provide me code for this.? I tried it by using following ssh username@serverA ./script Then it prompt me the password. I give correct password of the server A. but it... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

4. Shell Programming and Scripting

Pause processes in remote host and resume execution in another remote host

Hi, Given addresses of 2 remote machines, using a shell script is it possible to get the state of running processes in "src" stop all the processes in "src" exit out of "src" ssh into "dest" resume the state of executing processes captured in step 1 in "dest" Assumption: "src" is... (3 Replies)
Discussion started by: Saeya Darsan
3 Replies

5. Shell Programming and Scripting

Until string from remote command equals value run remote command

I solved my issue by using the following code #!/bin/bash function GET_STATUS { #values Active Passive Failed ssh -a localhost '/home/user/fakecommand.sh' } STATE="unknown" until ] do echo $STATE sleep 5 STATUS=`GET_STATUS` echo $STATUS | grep Active &&... (1 Reply)
Discussion started by: $scipt_Kid
1 Replies

6. Shell Programming and Scripting

How to run a shell script on a remote host using ftp

Hi, is there a way I can run a shell script through ftp on a remote host? The remote host doesn't have ssh running so I can't use ssh. (7 Replies)
Discussion started by: mrskittles99
7 Replies

7. Shell Programming and Scripting

HTML Code to Run a Script from Remote Unix Host

Hi All, Noticed few posts around this but coudnt get exatcly what i wanted. Thanks for your help again. I have a script running on a remote machine and i normally ssh from putty and run the script manually. Is there anyway that i can write an HTML Code with a button so taht when I Click... (1 Reply)
Discussion started by: robinbannis
1 Replies

8. UNIX for Dummies Questions & Answers

Run a script on remote host

Hi, I wish to run a script located on a remote host machineB from machineA. I am using ssh and running the below on machineA. However, the ssh does not seem to work and freezes at ssh -l wlsadmin machineB -v Sun_SSH_1.1.2, SSH protocols 1.5/2.0, OpenSSL 0x0090704f debug1: Reading... (9 Replies)
Discussion started by: shifahim
9 Replies

9. Shell Programming and Scripting

command to check whether the remote host is up or not

Hi, My script needs to check whether the remote host is up or not. If it is up i need to start few servers in that host or else just a notification should be sent that the remote host is down? Could someone help me out in this? Regards Arun (4 Replies)
Discussion started by: arunkumarmc
4 Replies

10. Shell Programming and Scripting

Run a shell script from one host which connext to remote host and run the commands

I want to write a script which would run from one host say A and connect to other remote host B and then run rest of commands in that host. I tried connecting from A host to B with SSH but after connecting to host B it just getting me inside Host B command prompt. Rest of the script is not running... (6 Replies)
Discussion started by: SN2009
6 Replies
Login or Register to Ask a Question