{"id":111780,"date":"2024-12-14T06:37:57","date_gmt":"2024-12-13T22:37:57","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=111780"},"modified":"2024-12-14T06:37:57","modified_gmt":"2024-12-13T22:37:57","slug":"python-linux-information","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-linux-information.html","title":{"rendered":"\u7ec6\u8ff0\u4f7f\u7528 Python \u83b7\u53d6 Linux \u7cfb\u7edf\u4fe1\u606f"},"content":{"rendered":"
\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u63a2\u7d22\u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\u5de5\u5177\u6765\u68c0\u7d22Linux\u7cfb\u7edf\u5404\u79cd\u4fe1\u606f\u3002<\/p>\n
\u5f53\u6211\u63d0\u53caPython\uff0c\u6240\u6307\u7684\u5c31\u662fCPython 2(\u51c6\u786e\u7684\u662f2.7).\u6211\u4f1a\u663e\u5f0f\u63d0\u9192\u90a3\u4e9b\u76f8\u540c\u7684\u4ee3\u7801\u5728CPython 3 (3.3)\u4e0a\u662f\u4e0d\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u63d0\u4f9b\u4e00\u4efd\u89e3\u91ca\u4e0d\u540c\u4e4b\u5904\u7684\u5907\u9009\u4ee3\u7801\u3002\u8bf7\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86CPython,\u5728\u7ec8\u7aef\u4e0a\u8f93\u5165python\u6216\u8005python3\u56de\u8f66\uff0c\u7136\u540e\u4f60\u5728\u7ec8\u7aef\u4e0a\u5e94\u8be5\u80fd\u770b\u5230python\u7684\u63d0\u793a\u7b26(prompt)\u3002<\/p>\n
\u8bf7\u6ce8\u610f\uff0c\u6240\u6709\u7684\u7a0b\u5e8f\u5728\u5b83\u4eec\u7b2c\u4e00\u884c\u90fd\u662f#!\/usr\/bin\/env\/python\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u60f3\u8981Python\u7684\u89e3\u91ca\u5668\u6765\u6267\u884c\u8fd9\u4e9b\u811a\u672c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u4f60\u7684\u811a\u672c\u5177\u6709\u6267\u884c\u6027\uff0c\u8bf7\u4f7f\u7528chmod +x your-script.py\uff0c \u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528.\/your-script.py\u6765\u6267\u884c\u5b83\u4e86\uff08\u5728\u672c\u6587\u4e2d\u4f60\u5c06\u4f1a\u770b\u5230\u8fd9\u79cd\u65b9\u5f0f\uff09<\/p>\n
platform\u6a21\u5757\u5728\u6807\u51c6\u5e93\u4e2d\uff0c\u5b83\u6709\u5f88\u591a\u8fd0\u884c\u6211\u4eec\u83b7\u5f97\u4f17\u591a\u7cfb\u7edf\u4fe1\u606f\u7684\u51fd\u6570\u3002\u8ba9\u6211\u4eec\u8fd0\u884cPython\u89e3\u91ca\u5668\u6765\u63a2\u7d22\u5b83\u4eec\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\uff0c\u90a3\u5c31\u4eceplatform.uname()\u51fd\u6570\u5f00\u59cb\u5427\uff1a<\/p>\n
\r\n>>> import platform\r\n>>> platform.uname()\r\n('Linux', 'Fedora.echorand', '3.7.4-204.fc18.x86_64', '#1 SMP Wed Jan 23 16:44:29 UTC 2013', 'x86_64')\r\n<\/pre>\n\u5982\u679c\u4f60\u5df2\u77e5\u9053linux\u4e0a\u7684uname\u547d\u4ee4\uff0c\u90a3\u4e48\u4f60\u5c31\u4f1a\u8ba4\u51fa\u6765\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u8fd9\u4e2a\u547d\u4ee4\u7684\u4e00\u4e2a\u63a5\u53e3\u3002\u5728Python 2\u4e0a\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7cfb\u7edf\u7c7b\u578b(\u6216\u8005\u5185\u6838\u7248\u672c)\uff0c\u4e3b\u673a\u540d\uff0c\u7248\u672c\uff0c\u53d1\u5e03\u7248\u672c\uff0c\u673a\u5668\u7684\u786c\u4ef6\u4ee5\u53ca\u5904\u7406\u5668\u4fe1\u606f\u5143\u7ec4(tuple)\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u4e2a\u522b\u5c5e\u6027\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n
\r\n>>> platform.uname()[0]\r\n'Linux'\r\n<\/pre>\n\u5728Python 3\u4e0a\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\uff1a<\/p>\n
\r\n>>> platform.uname()\r\nuname_result(system='Linux', node='fedora.echorand',\r\nrelease='3.7.4-204.fc18.x86_64', version='#1 SMP Wed Jan 23 16:44:29\r\nUTC 2013', machine='x86_64', processor='x86_64')\r\n<\/pre>\n\u56e0\u4e3a\u8fd4\u56de\u7ed3\u679c\u662f\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\uff0c\u8fd9\u5c31\u53ef\u4ee5\u7b80\u5355\u5730\u901a\u8fc7\u540d\u5b57\u6765\u6307\u5b9a\u7279\u5b9a\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u5fc5\u987b\u8bb0\u4f4f\u4e0b\u6807\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n
\r\n>>> platform.uname().system\r\n'Linux'\r\n<\/pre>\nplatform\u6a21\u5757\u8fd8\u6709\u4e00\u4e9b\u4e0a\u9762\u5c5e\u6027\u7684\u76f4\u63a5\u63a5\u53e3\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n
\r\n>>> platform.system()\r\n'Linux'\r\n>>> platform.release()\r\n'3.7.4-204.fc18.x86_64'\r\n<\/pre>\nlinux_distribution()\u51fd\u6570\u8fd4\u56de\u7684\u6709\u5173\u4f60\u6240\u5728\u7684linux\u53d1\u5e03\u7248\u672c\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5728Fedora 18\u7cfb\u7edf\u4e0a\uff0c\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8fd4\u56de\u5982\u4e0b\u4fe1\u606f\uff1a<\/p>\n
\r\n>>> platform.linux_distribution()\r\n('Fedora', '18', 'Spherical Cow')\r\n<\/pre>\n\u8fd9\u4e2a\u8fd4\u56de\u7ed3\u679c\u4e2d\u5305\u542b\u4e86\u7248\u672c\u53d1\u5e03\u540d\uff0c\u7248\u672c\u4ee5\u53ca\u4ee3\u53f7\u5143\u7ec4\u3002\u7279\u5b9a\u7684Python\u7248\u672c\u652f\u6301\u7684\u53d1\u5e03\u7248\u672c\u4e0a\u53ef\u4ee5\u901a\u8fc7_supported_dists\u663e\u793a\u7684\u503c\u83b7\u5f97\u3002<\/p>\n
\r\n>>> platform._supported_dists\r\n('SuSE', 'Debian', 'fedora', 'redhat', 'CentOS', 'mandrake',\r\n'mandriva', 'rocks', 'slackware', 'yellowdog', 'Gentoo',\r\n'UnitedLinux', 'turbolinux')\r\n<\/pre>\n\u5982\u679c\u4f60\u7684linux\u53d1\u5e03\u7248\u672c\u4e0d\u5728\u5176\u4e2d\uff08\u6216\u8005\u5176\u4e2d\u4e4b\u4e00\u7684\u884d\u751f\u53d1\u884c\u7248\uff09\u3002\u90a3\u4e48\u4f60\u5f88\u53ef\u80fd\u8c03\u7528\u4e86\u4e0a\u9762\u8fd9\u4e2a\u51fd\u6570\u800c\u770b\u4e0d\u5230\u4efb\u4f55\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n
platform\u6a21\u5757\u7684\u6700\u540e\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c06\u4f1a\u770b\u770barchitecture()\u51fd\u6570\u3002\u5f53\u4f60\u65e0\u53c2\u7684\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u5305\u542b\u67b6\u6784\u4f4d\u6570\u4ee5\u53capython\u53ef\u6267\u884c\u7684\u683c\u5f0f\u7684\u5143\u7ec4\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n
\r\n>>> platform.architecture()\r\n('64bit', 'ELF')\r\n<\/pre>\n\u572832\u4f4d\u7684\u7cfb\u7edf\u4e0a\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\uff1a<\/p>\n
\r\n>>> platform.architecture()\r\n('32bit', 'ELF')\r\n<\/pre>\n\u5982\u679c\u4f60\u6307\u5b9a\u4e86\u7cfb\u7edf\u4e0a\u5176\u4ed6\u4efb\u4f55\u53ef\u6267\u884c\u7684\uff0c\u4f60\u90fd\u5c06\u4f1a\u83b7\u5f97\u76f8\u4f3c\u7684\u7ed3\u679c\uff0c\u5982\u540c\u8fd9\u6837\uff1a<\/p>\n
\r\n>>> platform.architecture(executable='\/usr\/bin\/ls')\r\n('64bit', 'ELF')\r\n<\/pre>\n\u9f13\u52b1\u63a2\u7d22platform\u6a21\u5757\u7684\u9664\u4e86\u8fd9\u4e9b\u7684\u5176\u5b83\u51fd\u6570\uff0c\u627e\u51fa\u4f60\u73b0\u5728\u8fd0\u884c\u7684Python\u7248\u672c\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u8fd9\u4e2a\u6a21\u5757\u662f\u5982\u4f55\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u7684\uff0c\u4f60\u53ef\u4ee5\u6df1\u5165\u67e5\u770bPYthon\u6e90\u7801\u76ee\u5f55\u4e0b\u7684Lib\/platform.py\u6587\u4ef6\u3002<\/p>\n
os\u548csys\u6a21\u5757\u4e5f\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e9b\u7cfb\u7edf\u5c5e\u6027\uff0c\u4f8b\u5982\u539f\u751f\u7684\u5b57\u8282\u5e8f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8d85\u8d8aPython\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u53bb\u63a2\u7d22\u4e00\u4e9b\u5728linux\u7cfb\u7edf\u901a\u8fc7proc\u548csysfs\u6587\u4ef6\u7cfb\u7edf\u4f7f\u4e4b\u8bbf\u95ee\u4fe1\u606f\u6210\u4e3a\u53ef\u80fd\u3002\u6ce8\u610f\u7684\u662f\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u6765\u8bbf\u95ee\u4fe1\u606f\u5c06\u4f1a\u5728\u4e0d\u540c\u7684\u786c\u4ef6\u67b6\u6784\u6709\u6240\u4e0d\u540c\u3002\u6240\u4ee5\u5728\u8bfb\u672c\u6587\u6216\u662f\u5199\u811a\u672c\u65f6\u8981\u65f6\u523b\u8bb0\u4f4f\u53ef\u4ee5\u8bd5\u56fe\u4ece\u8fd9\u4e9b\u6587\u4ef6\u83b7\u53d6\u4fe1\u606f\u3002<\/p>\n
CPU\u4fe1\u606f<\/strong><\/span><\/div>\n\/proc\/cpuinfo\u6587\u4ef6\u5305\u542b\u4e86\u4f60\u7684\u7cfb\u7edf\u5904\u7406\u5668\u5355\u5143\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u5c31\u662fpython\u7248\u7684linux\u547d\u4ee4cat \/proc\/cpuinfo\u6240\u505a\u7684\u4e8b\uff1a<\/p>\n
\r\n#! \/usr\/bin\/env python\r\n\"\"\" print out the \/proc\/cpuinfo\r\n file\r\n\"\"\"\r\n\r\nfrom __future__ import print_function\r\nwith open('\/proc\/cpuinfo') as f:\r\n for line in f:\r\n print(line.rstrip('\\n'))\r\n\r\n<\/pre>\n\u5f53\u4f60\u4f7f\u7528Python 2 \u6216\u8005 Python 3\u6267\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u4f60\u4f1a\u5728\u5c4f\u5e55\u4e0a\u770b\u5230\u6240\u6709\/proc\/cpuinfo\u7684\u5185\u5bb9\uff08\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u91cc\uff0crstrip()\u65b9\u6cd5\u7528\u6765\u5220\u9664\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\uff09<\/p>\n
\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u91cc\u5217\u4e3e\u4e86\u4f7f\u7528startwith()\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u663e\u793a\u4f60\u7684\u5904\u7406\u5668\u5355\u5143\u7684\u6a21\u5f0f\u3002<\/p>\n
\r\n#! \/usr\/bin\/env python\r\n\"\"\" Print the model of your\r\n processing units\r\n\"\"\"\r\n\r\nfrom __future__ import print_function\r\nwith open('\/proc\/cpuinfo') as f:\r\n for line in f:\r\n # Ignore the blank line separating the information between\r\n # details about two processing units\r\n if line.strip():\r\n\r\n if line.rstrip('\\n').startswith('model name'):\r\n model_name = line.rstrip('\\n').split(':')[1]\r\n print(model_name)\r\n<\/pre>\n\u5f53\u4f60\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u540e\uff0c\u4f60\u5e94\u8be5\u4f1a\u770b\u5230\u4f60\u7684\u6bcf\u4e2a\u5904\u7406\u5668\u5355\u5143\u7684\u6a21\u5f0f\u540d\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u5c31\u662f\u5728\u6211\u7535\u8111\u4e0a\u6240\u770b\u5230\u7684\u3002<\/p>\n
\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\n<\/pre>\n\u8fc4\u4eca\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u6709\u4e24\u79cd\u65b9\u5f0f\u6765\u627e\u51fa\u6211\u4eec\u6240\u4f7f\u7528\u7684\u7cfb\u7edf\u7684\u67b6\u6784\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\u662f\u6b63\u786e\u7684\uff0c\u4e24\u4e2a\u65b9 \u5f0f\u5b9e\u9645\u4e0a\u62a5\u544a\u4e86\u4f60\u7cfb\u7edf\u8fd0\u884c\u7684\u5185\u6838\u67b6\u6784\uff0c\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u7684\u7535\u8111\u662f64\u4f4d\u7684\uff0c\u4f46\u662f\u8fd0\u884c\u7684\u662f32\u4f4d\u7684\u5185\u6838\uff0c\u7136\u540e\u4e0a\u9762\u7684\u65b9\u6cd5\u8fd8\u662f\u5c06\u4f1a\u663e\u793a\u4e3a32\u4f4d\u7684\u67b6\u6784\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ece\/proc\/cpuinfo\u6240\u5217\u4e3e\u7684\u6807\u5fd7\u4e2d\u67e5\u627elm\u6807\u5fd7\uff0c\u6765\u627e\u5230\u4f60\u7684\u7535 \u8111\u7684\u771f\u5b9e\u7684\u67b6\u6784\u3002lm\u6807\u5fd7\u4ee3\u8868\u4e86\u957f\u6a21\u5f0f\uff0c\u53ea\u670964\u4f4d\u67b6\u6784\u7684\u624d\u4f1a\u663e\u793a\u5b83\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u5c06\u4f1a\u6307\u5bfc\u4f60\u600e\u6837\u505a\uff1a<\/p>\n
\r\n#! \/usr\/bin\/env python\r\n\"\"\" find the real bit architecture\r\n\"\"\"\r\nfrom __future__ import print_function\r\nwith open('\/proc\/cpuinfo') as f:\r\n for line in f:\r\n # Ignore the blank line separating the information between\r\n # details about two processing units\r\n if line.strip():\r\n if line.rstrip('\\n').startswith('flags') \\\r\n or line.rstrip('\\n').startswith('Features'):\r\n if 'lm' in line.rstrip('\\n').split():\r\n print('64-bit')\r\n else:\r\n print('32-bit')\r\n<\/pre>\n\u5982\u6211\u4eec\u6240\u770b\u5230\u90a3\u6837\uff0c\u8bfb\u53d6\/proc\/cpuinfo\u6587\u4ef6\u4ee5\u53ca\u4f7f\u7528\u7b80\u5355\u6587\u672c\u5904\u7406\u6280\u672f\u5c31\u53ef\u4ee5\u83b7\u5f97\u6211\u4eec\u8981\u67e5\u627e\u7684\u6570\u636e\u662f\u53ef\u80fd\u7684\u3002\u4e3a\u4e86\u7ed9\u5176\u4ed6\u7a0b\u5e8f\u66f4\u597d\u7684\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\uff0c\u4e00\u4e2a\u66f4\u597d\u7684\u4e3b\u610f\u5c31\u662f\u4f7f\/proc\/cpuinfo\u7684\u5185\u5bb9\u6210\u4e3a\u6807\u51c6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8b6c\u5982\u5b57\u5178(dictionary)\u3002\u8fd9\u4e2a\u6ce8\u610f\u5f88\u7b80\u5355\uff1a\u5982\u679c\u4f60\u67e5\u770b\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u5bf9\u4e8e\u6bcf\u4e2a\u5904\u7406\u5668\u5355\u5143\uff0c\u90fd\u6709\u597d\u4e9b\u952e\u503c\u5bf9(\u5728\u5148\u524d\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5370\u4e86\u6bcf\u4e2a\u5904\u7406\u5668\u7684\u6a21\u578b\u540d\uff0c\u5373\u6a21\u578b\u540d\u5c31\u662f\u5173\u952e\u5b57)\u3002\u4e0d\u540c\u7684\u5904\u7406\u5668\u5355\u5143\u7684\u4fe1\u606f\u53ef\u4ee5\u4f7f\u7528\u7a7a\u767d\u884c\u9694\u5f00\u3002\u6784\u9020\u4e00\u4e2a\u5b57\u5178\u6570\u636e\u7ed3\u6784\u5305\u542b\u6bcf\u4e2a\u5904\u7406\u5668\u5355\u5143\u7684\u5173\u952e\u5b57\u662f\u5f88\u7b80\u5355\u7684\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u5173\u952e\u5b57\uff0c\u5bf9\u4e8e\u5904\u7406\u5668\u5355\u5143\u7684\u503c\u90fd\u5728\/proc\/cpuinfo\u6587\u4ef6\u4e2d\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u4f1a\u6307\u5bfc\u4f60\u600e\u4e48\u505a\u3002<\/p>\n
\r\n#!\/usr\/bin\/env\/ python\r\n\"\"\"\r\n\/proc\/cpuinfo as a Python dict\r\n\"\"\"\r\nfrom __future__ import print_function\r\nfrom collections import OrderedDict\r\nimport pprint\r\ndef cpuinfo():\r\n ''' Return the information in \/proc\/cpuinfo\r\n as a dictionary in the following format:\r\n cpu_info['proc0']={...}\r\n cpu_info['proc1']={...}\r\n '''\r\n cpuinfo=OrderedDict()\r\n procinfo=OrderedDict()\r\n nprocs = 0\r\n with open('\/proc\/cpuinfo') as f:\r\n for line in f:\r\n if not line.strip():\r\n # end of one processor\r\n cpuinfo['proc%s' % nprocs] = procinfo\r\n nprocs=nprocs+1\r\n # Reset\r\n procinfo=OrderedDict()\r\n else:\r\n if len(line.split(':')) == 2:\r\n procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()\r\n else:\r\n procinfo[line.split(':')[0].strip()] = ''\r\n return cpuinfo\r\nif __name__=='__main__':\r\n cpuinfo = cpuinfo()\r\n for processor in cpuinfo.keys():\r\n print(cpuinfo[processor]['model name'])\r\n<\/pre>\n\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86OrderedDict(\u6709\u5e8f\u5b57\u5178)\u800c\u4e0d\u662f\u5e38\u89c4\u7684\u5b57\u5178\uff0c\u80fd\u591f\u4f7f\u7528\u952e\u503c\u6709\u5e8f\u7684\u5b58\u50a8\u5728\u6587\u4ef6\u91cc\u3002\u6240\u4ee5\uff0c\u7b2c\u4e00\u4e2a\u5904\u7406\u5668\u5355\u5143\u7684\u6570\u636e\u4e4b\u540e\u5c31\u662f\u7b2c\u4e8c\u4e2a\u5904\u7406\u5668\u5355\u5143\u7684\u6570\u636e\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fc7\u6ee4\u5668\u6765\u8fc7\u6ee4\u4f60\u6240\u67e5\u627e\u7684\u4fe1\u606f\uff08\u5982\u540c\u5728if __name__ == \u2018__main__\u2019\u5757\u4e2d\u6f14\u793a\u7684\u90a3\u6837\uff09\u3002\u4e0a\u9762\u7684\u7a0b\u5e8f\u6bcf\u6b21\u6267\u884c\u540e\u90fd\u4f1a\u6253\u5370\u6bcf\u4e2a\u5904\u7406\u5668\u5355\u5143\u7684\u6a21\u578b\u540d\uff08\u5982\u901a\u8fc7cpuinfo[processor]['model name']\u8bed\u53e5\u8868\u660e\u7684\u90a3\u6837\uff09<\/p>\n
\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\nIntel(R) Core(TM) i7-3520M CPU @ 2.90GHz\r\n<\/pre>\n\u5185\u5b58\u4fe1\u606f<\/strong><\/span><\/div>\n\u548c\/proc\/cpuinfo\u76f8\u4f3c\uff0c\u6587\u4ef6\/proc\/meminfo\u5305\u542b\u4e86\u4f60\u7535\u8111\u7684\u4e3b\u5b58\u7684\u4fe1\u606f\u3002\u4e0b\u9762\u7684\u8fd9\u4e2a\u7a0b\u5e8f\u521b\u5efa\u4e86\u4e00\u4e2a\u4f7f\u7528\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u586b\u5145\u7684\u5b57\u5178\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\nfrom __future__ import print_function\r\nfrom collections import OrderedDict\r\ndef meminfo():\r\n ''' Return the information in \/proc\/meminfo\r\n as a dictionary '''\r\n meminfo=OrderedDict()\r\n with open('\/proc\/meminfo') as f:\r\n for line in f:\r\n meminfo[line.split(':')[0]] = line.split(':')[1].strip()\r\n return meminfo\r\nif __name__=='__main__':\r\n #print(meminfo())\r\n meminfo = meminfo()\r\n print('Total memory: {0}'.format(meminfo['MemTotal']))\r\n print('free memory: {0}'.format(meminfo['MemFree']))\r\n<\/pre>\n\u50cf\u5148\u524d\u7684\uff0c\u901a\u8fc7\u5b83\u7684\u5173\u952e\u5b57\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u4efb\u4f55\u4f60\u67e5\u8be2\u7684\u6307\u5b9a\u4fe1\u606f(\u5728if __name__==__main__\u5757\u4e2d\u6709\u6240\u8868\u793a)\u3002\u5f53\u4f60\u6267\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u4f60\u8be5\u4f1a\u770b\u5230\u50cf\u4e0b\u9762\u7c7b\u4f3c\u7684\u8f93\u51fa\uff1a<\/p>\n
\r\nTotal memory: 7897012 kB\r\nFree memory: 249508 kB\r\n<\/pre>\n\u7f51\u7edc\u7edf\u8ba1\u4fe1\u606f<\/strong><\/span><\/div>\n\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f1a\u63a2\u7d22\u6211\u4eec\u7535\u8111\u7cfb\u7edf\u7684\u7f51\u7edc\u8bbe\u5907\u3002\u6211\u4eec\u5c06\u4f1a\u83b7\u5f97\u7cfb\u7edf\u7684\u7f51\u7edc\u63a5\u53e3\uff0c\u4ee5\u53ca\u5f53\u7cfb\u7edf\u91cd\u542f\u4e4b\u540e\u901a\u8fc7\u5b83\u4eec\u6570\u636e\u53d1\u9001\u548c\u63a5\u53d7\u6570\u636e\u7684\u4fe1\u606f\u3002 \/proc\/net\/dev\u6587\u4ef6\u8ba9\u8fd9\u4e9b\u4fe1\u606f\u53ef\u7528\u3002\u5982\u679c\u4f60\u68c0\u67e5\u4e86\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4f60\u5c31\u4f1a\u6ce8\u610f\u5230\u5934\u4e00\u4e24\u884c\u5305\u542b\u4e86\u5934\u4fe1\u606f\u7b49\u7b49\uff0c\u8fd9\u4e2a\u6587\u4ef6\u7b2c\u4e00\u5217\u662f\u7f51\u7edc\u63a5\u53e3\u540d\uff0c\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5217\u663e\u793a\u4e86\u63a5\u6536\u548c\u53d1\u9001\u7684\u5b57\u8282\u6570\u4fe1\u606f\uff08\u4f8b\u5982\u603b\u53d1\u9001\u5b57\u8282\u6570\uff0c\u5305\u6570\uff0c\u9519\u8bef\u7b49\u7b49\uff09\u3002\u8fd9\u91cc\u6211\u4eec\u6240\u611f\u5174\u8da3\u7684\u5c31\u662f\u4ed6\u54e6\u96be\u8fc7\u4e0d\u540c\u7684\u7f51\u7edc\u8bbe\u5907\u63d0\u53d6\u51fa\u603b\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u6570\u636e\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u600e\u4e48\u4ece\/proc\/net\/dev\u6587\u4ef6\u4e2d\u63d0\u53d6\u51fa\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\nfrom __future__ import print_function\r\nfrom collections import namedtuple\r\ndef netdevs():\r\n ''' RX and TX bytes for each of the network devices '''\r\n with open('\/proc\/net\/dev') as f:\r\n net_dump = f.readlines()\r\n device_data={}\r\n data = namedtuple('data',['rx','tx'])\r\n for line in net_dump[2:]:\r\n line = line.split(':')\r\n if line[0].strip() != 'lo':\r\n device_data[line[0].strip()] = data(float(line[1].split()[0])\/(1024.0*1024.0),\r\n float(line[1].split()[8])\/(1024.0*1024.0))\r\n return device_data\r\nif __name__=='__main__':\r\n netdevs = netdevs()\r\n for dev in netdevs.keys():\r\n print('{0}: {1} MiB {2} MiB'.format(dev, netdevs[dev].rx, netdevs[dev].tx)) \r\n<\/span><\/pre>\n
\u5f53\u4f60\u8fd0\u884c\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4e0b\u9762\u8fd9\u4e2a\u8f93\u51fa\u5c31\u4f1a\u663e\u793a\u4ece\u4f60\u6700\u8fd1\u91cd\u542f\u4e4b\u540e\u7f51\u7edc\u8bbe\u5907\u603b\u63a5\u6536\u548c\u53d1\u9001\u7684\u6570\u636e\uff0c\u5355\u4f4d\u4e3a\u5146\u3002<\/p>\n
\r\nem1: 0.0 MiB 0.0 MiB\r\nwlan0: 2651.40951061 MiB 183.173976898 MiB\r\n<\/pre>\n\u4f60\u53ef\u4ee5\u4f7f\u7528\u6301\u4e45\u7684\u6570\u636e\u5b58\u50a8\u673a\u5236\u6765\u8fde\u63a5\uff0c\u6765\u5199\u51fa\u81ea\u5df1\u7684\u6570\u636e\u4f7f\u7528\u76d1\u63a7\u7a0b\u5e8f\u3002<\/p>\n
\u8fdb\u7a0b\u4fe1\u606f<\/strong><\/span><\/div>\n\/proc\u76ee\u5f55\u5305\u542b\u4e86\u6240\u6709\u6b63\u8fd0\u884c\u7684\u8fdb\u7a0b\u76ee\u5f55\u3002\u8fd9\u4e9b\u76ee\u5f55\u7684\u540d\u5b57\u548c\u8fdb\u7a0b\u7684\u6807\u8bc6\u7b26\u662f\u4e00\u6837\u7684\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u904d\u5386\/proc\u76ee\u5f55\u4e0b\u90a3\u4e9b\u4f7f\u7528\u6570\u5b57\u4f5c\u4e3a\u5b83\u4eec\u7684\u540d\u5b57\u7684\u76ee\u5f55\uff0c\u4f60\u5c31\u4f1a\u83b7\u5f97\u6240\u6709\u73b0\u5728\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u5217\u8868\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2dprocess_list()\u51fd\u6570\u8fd4\u56de\u6240\u6709\u73b0\u5728\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u7684\u6807\u8bc6\u7b26\u5217\u8868\u3002\u5f53\u4f60\u6267\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u540e\uff0c\u8fd9\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u5c31\u662f\u5728\u7cfb\u7edf\u4e0a\u8fd0\u884c\u7684\u603b\u8fdb\u7a0b\u6570\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\n\"\"\"\r\n List of all process IDs currently active\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport os\r\ndef process_list():\r\n pids = []\r\n for subdir in os.listdir('\/proc'):\r\n if subdir.isdigit():\r\n pids.append(subdir)\r\n return pids\r\nif __name__=='__main__':\r\n pids = process_list()\r\n print('Total number of running processes:: {0}'.format(len(pids)))\r\n<\/pre>\n\u4e0a\u9762\u7684\u7a0b\u5e8f\u5f53\u6267\u884c\u540e\u4f1a\u663e\u793a\u548c\u4e0b\u9762\u7c7b\u4f3c\u7684\u8f93\u51fa\uff1a<\/p>\n
Total number of running processes:: 229<\/p>\n
\u6bcf\u4e2a\u8fdb\u7a0b\u76ee\u5f55\u5305\u542b\u4e86\u4e00\u4e9b\u5176\u4ed6\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u5982\u8fdb\u7a0b\u547d\u4ee4\u7684\u8c03\u7528\uff0c\u5b83\u6b63\u4f7f\u7528\u7684\u5171\u4eab\u5e93\u4ee5\u53ca\u5176\u5b83\u7684\u3002<\/p>\n
\u5757\u8bbe\u5907<\/strong><\/span><\/div>\n\u4e0b\u4e00\u4e2a\u7a0b\u5e8f\u901a\u8fc7\u8bfbsysfs\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u5217\u51fa\u6240\u6709\u5757\u8bbe\u5907\u3002\u4f60\u7cfb\u7edf\u4e2d\u7684\u5757\u8bbe\u5907\u53ef\u4ee5\u4ece\/sys\/block\u76ee\u5f55\u4e2d\u627e\u5230\u3002\u56e0\u6b64\u53ef\u80fd\u4f1a\u6709\/sys\/block\/sda\u3001\/sys\/block\/sdb\u7b49\u8fd9\u6837\u7684\u76ee\u5f55\u3002\u4e3a\u4e86\u83b7\u53d6\u6240\u6709\u8fd9\u4e9b\u8bbe\u5907\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\/sys\/block\u76ee\u5f55\u8fdb\u884c\u626b\u63cf\u63d0\u53d6\u611f\u5174\u8da3\u7684\u5757\u8bbe\u5907\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\n\"\"\"\r\nread block device data from sysfs\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport glob\r\nimport re\r\nimport os\r\n# Add any other device pattern to read from\r\ndev_pattern = ['sd.*','mmcblk*']\r\ndef size(device):\r\n nr_sectors = open(device+'\/size').read().rstrip('\\n')\r\n sect_size = open(device+'\/queue\/hw_sector_size').read().rstrip('\\n')\r\n # The sect_size is in bytes, so we convert it to GiB and then send it back\r\n return (float(nr_sectors)*float(sect_size))\/(1024.0*1024.0*1024.0)\r\ndef detect_devs():\r\n for device in glob.glob('\/sys\/block\/*'):\r\n for pattern in dev_pattern:\r\n if re.compile(pattern).match(os.path.basename(device)):\r\n print('Device:: {0}, Size:: {1} GiB'.format(device, size(device)))\r\nif __name__=='__main__':\r\n detect_devs()\r\n<\/pre>\n\u5982\u679c\u4f60\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u4e0b\u8ff0\u7c7b\u4f3c\u7684\u8f93\u51fa\uff1a<\/p>\n
\r\nDevice:: \/sys\/block\/sda, Size:: 465.761741638 GiB\r\n\r\nDevice:: \/sys\/block\/mmcblk0, Size:: 3.70703125 GiB\r\n<\/pre>\n\u5f53\u6211\u8fd0\u884c\u8be5\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u6709\u4e2aSD\u5185\u5b58\u5361\u63d2\u5728\u7535\u8111\u4e0a\uff0c\u56e0\u6b64\u4f60\u4f1a\u770b\u5230\u7a0b\u5e8f\u68c0\u6d4b\u5230\u4e86\u5b83\u3002\u4f60\u4e5f\u53ef\u4ee5\u6269\u5c55\u8be5\u7a0b\u5e8f\u8bc6\u522b\u5176\u5b83\u5757\u8bbe\u5907\uff08\u6bd4\u5982\u865a\u62df\u786c\u76d8\uff09\u3002<\/p>\n
\u5efa\u7acb\u547d\u4ee4\u884c\u5b9e\u7528\u5de5\u5177<\/strong><\/span><\/div>\nlinux\u4e2d\u547d\u4ee4\u884c\u4f7f\u7528\u5de5\u5177\u662f\u65e0\u6240\u4e0d\u5728\u7684[@Lesus \u6ce8\uff1a\u66fe\u6709\u4eba\u8bf4\u8fc7\uff1alinux\u6ca1\u6709\u4e86\u547d\u4ee4\u884c\u5c31\u662f\u4e2a\u6e23\u3002\uff0c\u5b83\u5141\u8bb8\u4eba\u4e48\u6307\u5b9a\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5b9a\u5236\u7a0b\u5e8f\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002argparse\u6a21\u5757\u5c31\u63d0\u4f9b\u4e86\u548clinux\u547d\u4ee4\u884c\u5b9e\u7528\u5de5\u5177\u7c7b\u4f3c\u7684\u63a5\u53e3\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u7a0b\u5e8f\u5982\u4f55\u83b7\u5f97\u7cfb\u7edf\u4e0a\u7684\u6240\u6709\u7528\u6237\u4ee5\u53ca\u6253\u5370\u5b83\u4eec\u7684\u767b\u5f55Shell(\u4f7f\u7528\u4e86pwd\u6807\u51c6\u5e93\u6a21\u5757)\uff1a<\/p>\n
\r\n#!\/usr\/bin\/env python\r\n\"\"\"\r\nPrint all the users and their login shells\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport pwd\r\n# Get the users from \/etc\/passwd\r\ndef getusers():\r\n users = pwd.getpwall()\r\n for user in users:\r\n print('{0}:{1}'.format(user.pw_name, user.pw_shell))\r\nif __name__=='__main__':\r\n\r\n getusers()\r\n\r\n<\/pre>\n\u5f53\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u4e4b\u540e\uff0c\u5b83\u4f1a\u6253\u5370\u7cfb\u7edf\u4e0a\u6240\u6709\u7684\u7528\u6237\u4ee5\u53ca\u4ed6\u4eec\u767b\u5f55shell\u540d\u3002<\/p>\n
\u73b0\u5728\uff0c\u4f60\u60f3\u8981\u7a0b\u5e8f\u7684\u7528\u6237\u80fd\u591f\u9009\u62e9\u662f\u5426\u60f3\u770b\u7cfb\u7edf\u7528\u6237(\u50cfdaemon, apache)\u3002\u6211\u4eec\u6269\u5c55\u524d\u9762\u7684\u4ee3\u7801\uff0c\u7b2c\u4e00\u6b21\u4f7f\u7528argparse\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e2a\u7279\u6027\uff0c\u5982\u4e0b\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\n\"\"\"\r\nUtility to play around with users and passwords on a Linux system\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport pwd\r\nimport argparse\r\nimport os\r\ndef read_login_defs():\r\n uid_min = None\r\n uid_max = None\r\n if os.path.exists('\/etc\/login.defs'):\r\n with open('\/etc\/login.defs') as f:\r\n login_data = f.readlines()\r\n for line in login_data:\r\n if line.startswith('UID_MIN'):\r\n uid_min = int(line.split()[1].strip())\r\n if line.startswith('UID_MAX'):\r\n uid_max = int(line.split()[1].strip())\r\n return uid_min, uid_max\r\n# Get the users from \/etc\/passwd\r\ndef getusers(no_system=False):\r\n uid_min, uid_max = read_login_defs()\r\n if uid_min is None:\r\n uid_min = 1000\r\n if uid_max is None:\r\n uid_max = 60000\r\n users = pwd.getpwall()\r\n for user in users:\r\n if no_system:\r\n if user.pw_uid >= uid_min and user.pw_uid < = uid_max:\r\n print('{0}:{1}'.format(user.pw_name, user.pw_shell))\r\n else:\r\n print('{0}:{1}'.format(user.pw_name, user.pw_shell))\r\nif __name__=='__main__':\r\n parser = argparse.ArgumentParser(description='User\/Password Utility')\r\n parser.add_argument('\u2013no-system', action='store_true',dest='no_system',\r\n default = False, help='Specify to omit system users')\r\n args = parser.parse_args()\r\n getusers(args.no_system)\r\n<\/pre>\n\u4f7f\u7528--help\u9009\u9879\u6267\u884c\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u4f60\u4f1a\u770b\u5230\u53cb\u597d\u7684\u5e2e\u52a9\u4fe1\u606f\uff1a\u53ef\u9009\u9879\u4ee5\u53ca\u5b83\u4eec\u7684\u4f5c\u7528\u3002<\/p>\n
\r\n$ .\/getusers.py --help\r\nusage: getusers.py [-h] [--no-system]\r\nUser\/Password Utility\r\noptional arguments:\r\n -h, --help show this help message and exit\r\n --no-system Specify to omit system users\r\n<\/pre>\n\u4e0a\u9762\u7a0b\u5e8f\u4f7f\u7528\u7684\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
\r\n$ .\/getusers.py --no-system\r\ngene:\/bin\/bash\r\n\r\n\u5f53\u4f60\u4f20\u5165\u4e00\u4e2a\u975e\u6cd5\u7684\u53c2\u6570\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u4f1a\u53d1\u7262\u9a9a(\u62a5\u9519)\r\n\r\n$ .\/getusers.py --param\r\nusage: getusers.py [-h] [--no-system]\r\ngetusers.py: error: unrecognized arguments: --param\r\n<\/pre>\n\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u7b80\u5355\u7684\u7406\u89e3\u4e86\u5982\u4f55\u4f7f\u7528argparse\u6a21\u5757\u3002parser = argparse.ArgumentParser(description=\"User\/Password Utility\")\u8bed\u53e5\u521b\u5efa\u4e86\u4e00\u4e2a\u5e26\u8bf4\u660e\u7a0b\u5e8f\u662f\u505a\u4ec0\u4e48\u7684\u53ef\u9009\u63cf\u8ff0\u7684ArgumentParser\u5bf9\u8c61\uff0c<\/p>\n
\u7136\u540e,\u6211\u4eec\u6dfb\u52a0\u53c2\u6570\u3002\u6211\u4eec\u60f3\u8981\u7a0b\u5e8f\u80fd\u591f\u8bc6\u522b\u63a5\u4e0b\u6765\u8fd9\u6761\u8bed\u53e5 add_argument()\u3002parser.add_argument('--no-system', action='store_true', dest='no_system', default = False, help='Specify toomit system users')\u3002\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u5f53\u7cfb\u7edf\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u4f7f\u7528\u7740\u5c06\u8981\u63d0\u4f9b\u8fd9\u4e2a\u53c2\u6570\u7684\u540d\u79f0\uff0c\u63a5\u4e0b\u6765\u7684\u53c2\u6570acton=store_true\u8868\u660e\u5b83\u662f\u4e00\u4e2a\u5e03\u5c14\u9009\u62e9\u3002\u90a3\u5c31\u662f\u8bf4,\u5b83\u771f\u6216\u5047\u5f71\u54cd\u7a0b\u5e8f\u7684\u67d0\u4e9b\u884c\u4e3a\u3002dest\u4e3a\u53ef\u5b9a\u5236\u5316\u53c2\u6570\uff0c\u5b83\u7684\u503c\u53ef\u4ee5\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u4f7f\u7528\u3002\u5047\u5982\u8fd9\u4e2a\u503c\u7528\u6237\u4e0d\u63d0\u4f9b\uff0c\u8fd9\u4e2a\u503c\u9ed8\u8ba4false\u3002\u6700\u540e\u7684\u53c2\u6570\u7a0b\u5e8f\u663e\u793a\u7684\u5e2e\u52a9\u4fe1\u606f\u3002\u6700\u540e\uff0c\u53c2\u6570\u88ab\u89e3\u6790\u901a\u8fc7args=parser.parse_args()\u65b9\u6cd5\u3002\u4e00\u65e6\u89e3\u6790\u65b9\u6cd5\u88ab\u505a,\u7528\u6237\u9009\u9879\u7684\u503c\u80fd\u591f\u88ab\u6293\u53d6\u5230\u901a\u8fc7\u76f8\u5e94\u7684\u8bed\u6cd5\u53c2\u6570option_dest\uff0c\u5f53\u4f60\u914d\u7f6e\u53c2\u6570\u7684\u65f6\u5019\uff0coption_dest\u662f\u4e00\u4e2a\u4f60\u6307\u5b9a\u7684\u76ee\u7684\u53d8\u91cf\u3002getusers(args.no_system)\u8fd9\u6761\u8bed\u53e5\u4f7f\u7528\u7528\u6237\u63d0\u4f9b\u53c2\u6570\u7684\u503c\u5c06\u4f1a\u56de\u8c03getusers()\u65b9\u6cd5\u3002<\/p>\n
\u4e0b\u9762\u7684\u7a0b\u5e8f\u5c55\u793a\u4e86\u5982\u4f55\u6307\u5b9a\u975e\u5e03\u5c14\u7c7b\u578b\u7684\u9009\u9879\u3002\u8be5\u7a0b\u5e8f\u662f\u5bf9\u7b2c6\u4e2a\u7a0b\u5e8f\u7684\u91cd\u5199\uff0c\u9644\u52a0\u4e86\u4e00\u4e2a\u9009\u9879\u7528\u4e8e\u6307\u5b9a\u4f60\u611f\u5174\u8da3\u7684\u7f51\u7edc\u8bbe\u5907\u3002<\/p>\n
\r\n#!\/usr\/bin\/env python\r\nfrom __future__ import print_function\r\nfrom collections import namedtuple\r\nimport argparse\r\ndef netdevs(iface=None):\r\n ''' RX and TX bytes for each of the network devices '''\r\n with open('\/proc\/net\/dev') as f:\r\n net_dump = f.readlines()\r\n device_data={}\r\n data = namedtuple('data',['rx','tx'])\r\n for line in net_dump[2:]:\r\n line = line.split(':')\r\n if not iface:\r\n if line[0].strip() != 'lo':\r\n device_data[line[0].strip()] = data(float(line[1].split()[0])\/(1024.0*1024.0),\r\n float(line[1].split()[8])\/(1024.0*1024.0))\r\n else:\r\n if line[0].strip() == iface:\r\n device_data[line[0].strip()] = data(float(line[1].split()[0])\/(1024.0*1024.0),\r\n float(line[1].split()[8])\/(1024.0*1024.0)) \r\n return device_data\r\nif __name__=='__main__':\r\n parser = argparse.ArgumentParser(description='Network Interface Usage Monitor')\r\n parser.add_argument('-i','--interface', dest='iface',\r\n help='Network interface')\r\n args = parser.parse_args()\r\n netdevs = netdevs(iface = args.iface)\r\n for dev in netdevs.keys():\r\n print('{0}: {1} MiB {2} MiB'.format(dev, netdevs[dev].rx, netdevs[dev].tx))\r\n<\/pre>\n\u5f53\u4f60\u4e0d\u5e26\u4efb\u4f55\u53c2\u6570\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e0e\u4e4b\u524d\u7684\u7248\u672c\u5b8c\u5168\u4e00\u81f4\u3002\u7136\u540e\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u611f\u5174\u8da3\u7684\u7f51\u7edc\u8bbe\u5907\u3002\u4f8b\u5982\uff1a<\/p>\n
\r\n$ .\/net_devs_2.py\r\nem1: 0.0 MiB 0.0 MiB\r\nwlan0: 146.099492073 MiB 12.9737148285 MiB\r\nvirbr1: 0.0 MiB 0.0 MiB\r\nvirbr1-nic: 0.0 MiB 0.0 MiB\r\n$ .\/net_devs_2.py --help\r\nusage: net_devs_2.py [-h] [-i IFACE]\r\nNetwork Interface Usage Monitor\r\noptional arguments:\r\n -h, --help show this help message and exit\r\n -i IFACE, --interface IFACE\r\n Network interface\r\n$ .\/net_devs_2.py -i wlan0\r\nwlan0: 146.100307465 MiB 12.9777050018 MiB\r\n<\/pre>\n\u811a\u672c\u7684\u7cfb\u7edf\u8303\u56f4\u53ef\u7528\u6027<\/strong><\/span><\/div>\n\u5728\u672c\u6587\u7684\u5e2e\u52a9\u4e0b\uff0c\u4f60\u53ef\u80fd\u5df2\u7ecf\u53ef\u4ee5\u5199\u4e00\u4e2a\u6216\u591a\u4e2a\u6709\u7528\u7684\u811a\u672c\uff0c\u5c31\u50cf\u5176\u5b83linux\u547d\u4ee4\u4e00\u6837\uff0c\u4f60\u60f3\u8981\u6bcf\u5929\u90fd\u4f7f\u7528\u5b83\u4eec\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u5c06\u811a\u672c\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u7684\uff0c\u7136\u540e\u4e3a\u811a\u672c\u8bbe\u7f6e\u4e00\u4e2aBASH\u522b\u540d\u3002\u4f60\u4e5f\u53ef\u4ee5\u79fb\u9664.py\u6269\u5c55\u540d\uff0c\u7136\u540e\u5c06\u811a\u672c\u653e\u5728\u8bf8\u5982\/usr\/local\/sbin\u8fd9\u6837\u7684\u6807\u51c6\u4f4d\u7f6e\u3002<\/p>\n
\u5176\u5b83\u6709\u7528\u7684\u6807\u51c6\u5e93\u6a21\u7ec4<\/strong><\/span><\/div>\n\u9664\u4e86\u672c\u6587\u4e2d\u5df2\u7ecf\u63d0\u5230\u7684\u6807\u51c6\u5e93\u6a21\u7ec4\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u5b83\u6709\u7528\u7684\u6807\u51c6\u6a21\u7ec4\uff1asubprocess\u3001ConfigParser\u3001readline\u548ccurses\u3002<\/p>\n
\u63a5\u4e0b\u6765\u505a\u4ec0\u4e48\uff1f<\/strong><\/span><\/div>\n\u5728\u8fd9\u4e2a\u9636\u6bb5\uff0c\u4f9d\u9760\u4f60\u81ea\u5df1\u4f7f\u7528Python\u7684\u7ecf\u9a8c\uff0c\u63a2\u7d22Linux\u5185\u90e8\uff0c\u4f60\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u4efb\u4e00\u65b9\u5f0f\u3002\u5982\u679c\u4f60\u66fe\u7ecf\u9700\u8981\u5199\u5f88\u591ashell\u811a\u672c\/\u547d\u4ee4\u6d41\u6c34\u7ebf\u6765<\/p>\n
\u63a2\u7d22Linux\u5185\u90e8\uff0c\u90a3\u4e48\u8bd5\u4e00\u4e0bPython\u3002\u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u7f16\u5199\u6267\u884c\u5f88\u591a\u4efb\u52a1\u7684\u5b9e\u7528\u7a0b\u5e8f\u811a\u672c\uff0c\u90a3\u4e48\u8bd5\u4e00\u4e0bPython\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u4f7f\u7528Python\u5728Linux\u4e0a\u522b\u5199\u5176\u5b83\u76ee\u7684\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u8bd5\u4e00\u4e0b\u7528Python\u63a2\u7d22Linux\u5185\u90e8\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linuxde.net\/2013\/08\/14969.html<\/a><\/p>\n