#!/usr/bin/env python # -*- coding: utf-8 -*- import cmd class Cli(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.prompt = "> " self.intro = " \n 'help'" self.doc_header =" ( 'help __')" def do_hello(self, args): """hello - 'hello world' """ print "hello world" def default(self, line): print " " if __name__ == "__main__": cli = Cli() try: cli.cmdloop() except KeyboardInterrupt: print " ..."
$ ./cli.py 'help' > help ( 'help __') =========================================================================== hello help > help hello hello - 'hello world' > hello hello world > ...
#!/usr/bin/env python # -*- coding: utf-8 -*- import cmd import os class Cli(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.prompt = "> " self.intro = " \n 'help'" self.doc_header =" ( 'help __')" def do_show_cpu(self, args): """show_cpu - """ os.system("sar 2") def do_show_mem(self, args): """show_mem - RAM""" os.system("free") def do_show_disk(self, args): """show_disk - """ os.system("df -h") def do_show_net(self, args): """show_net - """ os.system("/sbin/ifconfig") os.system("/sbin/route -n") def do_show_log(self, args): """show_log - """ os.system("sudo tail -f /var/log/messages") def default(self, line): print " " def emptyline(self): pass if __name__ == "__main__": cli = Cli() try: cli.cmdloop() except KeyboardInterrupt: print " ..."
# adduser user --shell /usr/local/bin/cli.py ... root@laptop:~# su - user 'help' > ? ( 'help __') =========================================================================== help show_cpu show_disk show_log show_mem show_net > show_cpu Linux 3.5.0-17-generic (laptop) 04/03/2013 _x86_64_ (4 CPU) 02:38:03 PM CPU %user %nice %system %iowait %steal %idle 02:38:05 PM all 0.63 0.00 0.25 0.13 0.00 98.99 02:38:07 PM all 1.00 0.00 0.25 0.25 0.00 98.50 > show_mem total used free shared buffers cached Mem: 3911236 2123408 1787828 0 124156 994752 -/+ buffers/cache: 1004500 2906736 Swap: 4393980 0 4393980 >
Source: https://habr.com/ru/post/175321/
All Articles