Use shell and Python to implement simple menu functions--Print current system state information

Source: Internet
Author: User

Brother I'm back again, haha · Haven't come to write a blog for a long time, now slowly make up the former! Recently learning Python, so you will update some of the problems encountered in learning.

Today's content is the function of a simple script written before the shell is implemented with Python: Select the corresponding menu item and print the status information of the current system.

No more wordy, on the code:

#!/bin/bash user  () {       echo  "User login record:"         echo  "' Last '"} ifcfg  () {         echo  "native NIC information is as follows:"          echo  "' ifconfig '"}  netstat  () {        echo  "number of concurrent requests for HTTP and their TCP connection status:"           echo  "' netstat -n | awk  '/^tcp/ {++s[$ Nf]} end {for (A in s)  print a, s[a]} ""  } Nfsstat  () {    echo  " NFS  Status:"     echo  "' NFSSTAT -CN '"  }  system  () {   echo  "  system version:"    echo  "' Lsb_release -a '"}  Disk  () {   echo  "  Disk partition usage:"    echo  "' Df -h '"}  IO  () { &Nbsp; echo  "  Disk I/O:"    echo  "' Iostat -dx '"} top  () {    echo  " CPU, Memory Usage:"    echo  "' vmstat 5 '"} ps3= "Please enter your choice:" A= " View Current User: View current NIC parameters: View http Concurrency: View NFS Status: System version: Disk partition usage: Disk IO:CPU Memory Usage: Exit script  && IFS=:   Select i in ${a};d o    case  $i  in            view current users)            user           ;;            view current NIC parameters)             Ifcfg          ;;            View http concurrency)             netstat          ;;            View NFS status)             Nfsstat          ;;            system version)             System          ;;            disk partition usage)             Disk          ;;            disk IO)             IO          ;;           CPU memory usage)             top         &nBSP;;;            exit script)             exit 0 ;;           *)            echo  "No Right Choice"; exit 1;;     esacdone

#!/usr/bin/env python# -*- coding:utf-8 -*-# filename: systemstatus.py  import os# swich case  Implementation # this class provides the functionality  we want. You only need to look at# this if you  want to know how this works. it only needs to be  Defined# once, no need to muck around with its internals.class  switch (object):     def __init__ (Self, value):         self.value = value        self.fall  = false     def __iter__ (self):          "" "Return the match method once, then stop" ""          yield self.match        raise stopiteration      def match (Self, *args):         "" "Indicate  Whether or not to enter a case suite "" "         if self.fall or not args:             return true        elif self.value  in args: # changed for v1.5, see below             self.fall = True             return True        else:             return false def print_menu () :     MENU&NBsp;= {        1:  "View Current User",         2:  "View current NIC parameters",        3:  "View http Concurrency",         4:  "View NFS Status",         5:  "System Version",        6:  "Disk partition usage",         7:  "Disk IO",        8:  " CPU memory Usage ",        9: " Print this menu "    }     print (' System Info View tool Menu: ')     #  print entire menu     for i  in menu:        print (I, menu[i])     The  returnlst = print_menu () print (LST) # while true  part implements a loop that never stops itself. However, it is possible to include the condition in the IF statement inside the loop, and invoke the break statement when the condition is satisfied. #  so that you can be earlyLoops are terminated anywhere inside the loop instead of just at the beginning (as in normal while loops). The # if/break  statement naturally divides the loop into two parts: the first part is responsible for initializing (in the normal while loop, this part needs to be repeated), and the second part uses the first part to initialize the good data when the loop condition #  is true. While true:    item = input (' Please enter the menu number (1-9, leave the program Blank): ')      If not item: break    for case in switch (item):         if case (' 1 '):             print (' Current user info: ')              Os.system (' last ')             break         if case (' 2 '):             print (' current NIC parameters: ')              Os.system (' ifconfig ')             break &nBsp;      if case (' 3 '):             print (' http Concurrency: ')              os.system ("netstat -n | awk  '/^tcp/ {++s[$NF]} end {for (a in  s)  print a, s[a] ")              break        if case (' 4 '):             print (' NFS Status: ')              os.system (' NFSSTAT -CN ')              break        if case (' 5 '):             print (' System version: ')              os.systeM (' lsb_release -a ')             break         if case (' 6 '):             print ("Disk partition usage")              os.system (' df -h ')             break         if case (' 7 '):             print (' Disk IO: ')              os.system (' iostat -dx ')              Break        if case (' 8 '):             print (' CPU Memory usage: ')              os.system (' VMsTat 5 ')             break         if case (' 9 '):             lst = print_menu ( )              print (LST)             break         if case ('):             print ("Exit!")             exit (1)

Reference: Python implements similar switch...case functionality

This article is from the "Centi.linux" blog, make sure to keep this source http://centilinux.blog.51cto.com/1454781/1785778

Use shell and Python to implement simple menu functions--Print current system state information

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.