Script for automatically connecting multiple machines and searching logs

Source: Internet
Author: User

The script for automatically connecting multiple machines and searching logs is deployed on 10 machines, and the corresponding log files are scattered on these 10 machines. If you want to search for these 10 log files, such as error messages, it is a common requirement. The following script does this. The general logic is very simple: pseudo-code: for hostname in (10 machines): ssh connects to a remote host to find matching lines from the log file and write them to the local file, but for me unfamiliar with shell programming, there are two difficulties: 1. during ssh, you do not need to manually enter passphase/password2. after you finish the current ssh session, start the next ssh session for a long time. google is lucky. The solution is as follows: 1. how does one manually enter passphase/password during ssh? Use ssh-agent! After the process is started, you only need to manually enter passphase once, then the ssh-agent will remember your passphase. When you connect to the remote machine, it will automatically read from the memory, no human input is required. Environment Settings: 1) generate public/private key pairs: $ ssh-keygen-t dsa 2) Put the generated public key (id_dsa.pub) on 10 remote machines $ scp. ssh/id_dsa.pub username @ hostname :. ssh/authorized_keys (10 times) 3) Make sure that the ssh-agent has been started. If not, start: $ ps aux | grep ssh-agent # check if it's running $ eval 'ssh-agent' # if not, start up the agent process 4) submit your passphase: $ ssh-add 5 to ssh-agent. Save the ssh-agent information to the variable to import the reference when executing the script: $ ssh-agent-s> ~ /. Bashrc 6) Remember to introduce the variable in the script, for example, $ source ~ /. Bashrc 2. How to enable the next ssh session after the current ssh session ends? <"EOF" EOF specific principles, which will be added later! At this point, the entire executable script will come out. Then write a line in crontab-e, so that it can easily collect error messages and send emails to me every day! The script is attached. It is really crude to write it. I don't need to be polite! By the way, if you have a more concise solution, please kindly advise! #! /Bin/bash source ~ /. Bashrc hostfile = "/home/bai/host_videoapi" # One Machine Line keyword = "fail @" # query keyword search_log = "videoapi. log "# The searched log file name outputfile =" error_videoapi.log "# output file # connect one by one and search for the error message while read line do vahost = $ line echo" --- hostname: "$ vahost" ----- ">>$ outputfile ssh $ vahost" find/home/videoapi/logs-type f-name $ search_log-exec grep-I $ keyword {}\; -print ">>$ outputfile <" EOF "EOF echo" --- finishing ----- "Done <$ hostfile # Check whether an error message exists. if yes, send an email. if no, done nothing if grep-q $ keyword $ outputfile then mail-s" errors in videoapi! "Bai@126.com <$ error_log else echo" it is a nice day! "Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.