tail file from remote server and nslookup on local

Alex K.

I need a script for resolve hosts.

For now I run this script on local machine

for i in `tail -F access.log | awk '{print $8}' | awk '{gsub("http://|/.*","")}2' | awk '{gsub("http://|:.*","")}1' | grep -E -v "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"`
do 
   nslookup $i [dns-server_ip]; 
done > ips.txt

But i need tail file from some remote hosts and run nslookup on my local-machine and i don't have a clue how to do it.

Alex Stragies

Use ssh?

Most commands can be executed remotely by just prepending the ssh command, so replace tail -F access.log with ssh REMOTEHOST tail -F access.log ==>

ssh myUSER@myREMOTEHOST tail -F access.log |
awk '{print $8}' |
awk '{gsub("http://|/.*","")}2' | awk '{gsub("http://|:.*","")}1' |
grep -E -v "([0-9]{1,3}\.){3})[0-9]{1,3}" | while read i ;
do 
   nslookup $i $dnsServerIP; 
done > ips.txt

As @kasperd and @archemar also hinted, you could/should clean up that long pipeline. Here is my take:

ssh myUSER@myREMOTEHOST tail -F access.log |
awk '{$0=$8; gsub("https?://|[/:].*","")} !/([0-9]{1,3}.){1,3}[0-9]{1,3}/' |
while read i ; do
    nslookup $i $dnsServerIP; 
done > ips.txt

Explanation:

  • $0=$8. In your example output, the column with the URL you want is #8. This command overwrites $0 (the entire line) with only element $8, effectively throwing away the rest. This replaces '{print $8}'

  • gsub("https?://|[/:].*","")} replaces both of your gsub-calls with one covering all possibilities in one. In your code you also searched for "http://" twice, and did not match https.

  • !/([0-9]{1,3}.){1,3}[0-9]{1,3}/' replaces your separate grep call while using the exact same regex. It evaluates to true, when $0 is not a numeric IP, and will this implicitly {print $0}'. Slightly shorter h=$0;gsub(/[0-9.]/,"",h)} h has the same effect.

Note: The trailing 2 and 1 behind the } behind your gsub-calls evaluate to true, which in turn gets implicitly expanded to true {print $0}. This is how/why the last regex in my solution prints the non-matching line implicitly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to tail a remote Linux server log file to a local Windows machine

From Dev

FileZilla: Transfer file remote server to remote server from a local computer

From Dev

Write file from remote server to local directory?

From Dev

scp file to different user in the remote server from local

From Dev

Download file directly from URL to local folder or a folder in remote server

From Dev

How to copy a file from a remote server to a local machine?

From Dev

Printing a file from local computer using printer connected to remote server

From Dev

How to transfer file from remote server to local machine using with ssh

From Dev

Download file directly from URL to local folder or a folder in remote server

From Dev

Unable to copy a file from remote server to local machine

From Dev

How to copy file contents from remote server to local machine location?

From Dev

BULK INSERT data from local file to remote sql server

From Dev

how to tail into the last file on a remote server via ssh

From Dev

Transfer a file from remote server to remote server

From Dev

Transfer a file from remote server to remote server

From Dev

How do I run a executable .sh file on a remote server from local in its remote-existed path?

From Dev

copy local file to remote server in ubuntu terminal

From Dev

How to download last modified file from remote server to local using PuTTY batch file

From Dev

Query: rsync - Copying Local folder to Remote Server from Remote Server

From Dev

scp to local from a remote server that ssh's to another remote server

From Dev

Secure copying files from a remote server to local machine from a list in a text file

From Dev

Run local SQL script file from Windows Command Prompt to update remote SQL Server database

From Dev

How can I copy file from local server to remote with creating directories which absent via SSH?

From Dev

C# copy .csv file from local machine to remote postresql server

From Dev

Copy file from remote to local using ssh

From Dev

Copy file from local host to remote host

From Dev

Fail to scp file from remote to local

From Dev

Moving a file from local drive to a remote machine

From Dev

Python Move a File from Remote Machine to Local

Related Related

  1. 1

    How to tail a remote Linux server log file to a local Windows machine

  2. 2

    FileZilla: Transfer file remote server to remote server from a local computer

  3. 3

    Write file from remote server to local directory?

  4. 4

    scp file to different user in the remote server from local

  5. 5

    Download file directly from URL to local folder or a folder in remote server

  6. 6

    How to copy a file from a remote server to a local machine?

  7. 7

    Printing a file from local computer using printer connected to remote server

  8. 8

    How to transfer file from remote server to local machine using with ssh

  9. 9

    Download file directly from URL to local folder or a folder in remote server

  10. 10

    Unable to copy a file from remote server to local machine

  11. 11

    How to copy file contents from remote server to local machine location?

  12. 12

    BULK INSERT data from local file to remote sql server

  13. 13

    how to tail into the last file on a remote server via ssh

  14. 14

    Transfer a file from remote server to remote server

  15. 15

    Transfer a file from remote server to remote server

  16. 16

    How do I run a executable .sh file on a remote server from local in its remote-existed path?

  17. 17

    copy local file to remote server in ubuntu terminal

  18. 18

    How to download last modified file from remote server to local using PuTTY batch file

  19. 19

    Query: rsync - Copying Local folder to Remote Server from Remote Server

  20. 20

    scp to local from a remote server that ssh's to another remote server

  21. 21

    Secure copying files from a remote server to local machine from a list in a text file

  22. 22

    Run local SQL script file from Windows Command Prompt to update remote SQL Server database

  23. 23

    How can I copy file from local server to remote with creating directories which absent via SSH?

  24. 24

    C# copy .csv file from local machine to remote postresql server

  25. 25

    Copy file from remote to local using ssh

  26. 26

    Copy file from local host to remote host

  27. 27

    Fail to scp file from remote to local

  28. 28

    Moving a file from local drive to a remote machine

  29. 29

    Python Move a File from Remote Machine to Local

HotTag

Archive