Shell Exercises (ii)

Source: Internet
Author: User

Exercise 1: Counting Memory usage

Requirements: Write a script that calculates the amount of memory that is consumed by all processes in the Linux system. (Hint: use PS or top command)

Reference Answer:

#!/bin/bash# date:2018 February 6 # using top to calculate Echo-n "top statistics:"; top-bn1|sed ' 1,7 ' D|awk ' {(sum=sum+$6)}; END {print sum} ' # uses PS to calculate echo-n "PS statistic result:";p s aux|grep-v ' RSS ' |awk ' {(sum=sum+$6)}; END {print sum} ' # use for loop sum=0for men in ' ps aux|grep-v ' RSS ' |awk ' {print $6} ' do sum=$[$sum + $men]doneecho "(PS) the tot Al memory is $sum "K"


Exercise 2: Designing a Monitoring script

requirements: design a script that monitors the surviving state of a remote machine (assuming IP 123.23.11.21) and sends a message to yourself when it finds an outage

tip:  1. You can use the ping command    PING-C10 192.168.139.128
            2. The e-mail script can refer to mail.py
            3. The script can be turned into a dead loop, every 30s detection

Reference Answer:

#!/bin/bash# date:2018 February 6 ip= "192.168.139.128" email= "[email protected]" while:d o ping-c10 $ip >/dev/null 2>/de V/null if [$?-ne "0"];thenpython/root/shell/mail.py "$email" "$ip Down" "$ip are down ..." elseecho "Mail send fail! "Fisleep 30done

Mail Script mail.py

#!/usr/bin/env python#-*- coding: utf-8 -*-import os,sysreload (SYS) Sys.setdefaultencoding (' UTF8 ') import getoptimport smtplibfrom email. Mimetext import mimetextfrom email. Mimemultipart import mimemultipartfrom  subprocess import *def sendqqmail ( username,password,mailfrom,mailto,subject,content):    gserver =  ' smtp.163.com '     gport = 25    try:         msg = mimetext (Unicode (content). Encode (' Utf-8 '))          msg[' from '] = mailfrom        msg[' to '] =  mailto        msg[' reply-to '] = mailfrom         msg[' Subject '] = subject         smtp  = smtplib. SMTP (gserver, gport)         smtp.set_debuglevel (0)          smtp.ehlo ()         smtp.login ( Username,password)         smtp.sendmail (mailfrom, mailto,  Msg.as_string ())         smtp.close ()     except  Exception,err:        print  "send mail failed.  error: %s " % errdef main ():    to=sys.argv[1]     subject=sys.argv[2]    content=sys.argv[3]    sendqqmail (' [Email  protected] ', ' email Authorization Code ', ' [email protected] ', to,subject,content) if __name__ ==  "__main__ ":     main ()


Shell Exercises (ii)

Related Article

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.