码迷,mamicode.com
首页 > 编程语言 > 详细

python获取linux中top信息

时间:2018-03-04 21:20:09      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:python获取linux中top信息

import os,time,sys
import paramiko,pexpect

获取日期格式:

def get_year_mon_day_hour_min_sec():
    time_array = time.localtime()
    result= "%s:%s:%s:%s:%s:%s" %(time_array.tm_year,time_array.tm_mon,time_array.tm_mday,time_array.tm_hour,time_array.tm_min,time_array.tm_sec)
    return result

文件格式:
flie = (ip.list) 192.168.47.196:root:123456:top -b -n 1:free -m:vmstat 1 1:


     print "开始执行命令。。。"
     try:
        file = open(‘ip.list‘)
        for line in file.readlines():
            ip = str(line.split(‘:‘)[0])
            username = str(line.split(‘:‘)[1])
            password = str(line.split(‘:‘)[2])
            cmds = (line.split(‘:‘)[3:-1])
            time_data = ""
            time_data = "######## ip: " + ip + "######"+ get_year_mon_day_hour_min_sec()+"===="
            s = paramiko.SSHClient()
            s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            s.connect(ip, 22, username, password)
            dd = ""
            cmm = ""
            for cmd in cmds:
                cmm = "commands :%s" % cmd
                stdin, stdout, sterr = s.exec_command(cmd)
                dd = stdout.read()
                with open("test.txt", "a+") as f:
                    f.write(cmm+"\n"+time_data+"\n"+dd)
            s.close()
        file.close()
        print "执行完毕!!"
     except:
        print "结果是否在列表中。。。"

结果:

commands :top -b -n 1
######## ip: 192.168.47.196######2018:3:4:16:9:46====
top - 03:52:39 up  9:05,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 110 total,   1 running, 108 sleeping,   1 stopped,   0 zombie
Cpu(s):  0.2%us,  0.1%sy,  0.0%ni, 99.5%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1914164k total,   814092k used,  1100072k free,   170248k buffers
Swap:  2064376k total,        0k used,  2064376k free,   491472k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      20   0  2900 1440 1216 S  0.0  0.1   0:01.44 init               
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd           
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.15 migration/0        
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.07 ksoftirqd/0        
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0        
    6 root      RT   0     0    0    0 S  0.0  0.0   0:01.12 watchdog/0         
    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.18 migration/1        
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1        
    9 root      20   0     0    0    0 S  0.0  0.0   0:00.10 ksoftirqd/1        
   10 root      RT   0     0    0    0 S  0.0  0.0   0:00.15 watchdog/1         
   11 root      20   0     0    0    0 S  0.0  0.0   0:02.38 events/0           
   12 root      20   0     0    0    0 S  0.0  0.0   0:07.81 events/1           
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup             
   14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper            
   15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns              
   16 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr          
   17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                 
   18 root      20   0     0    0    0 S  0.0  0.0   0:00.11 sync_supers        
   19 root      20   0     0    0    0 S  0.0  0.0   0:00.11 bdi-default        
   20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0      
   21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/1      
   22 root      20   0     0    0    0 S  0.0  0.0   0:00.25 kblockd/0          
   23 root      20   0     0    0    0 S  0.0  0.0   0:00.43 kblockd/1          
   24 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid             
   25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify       
   26 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug      
   27 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux            
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.03 ata_sff/0          
   29 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_sff/1          
   30 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd      
   31 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd              
   32 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kseriod            
   33 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md/0               
   34 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md/1               
   35 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md_misc/0          
   36 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md_misc/1          
   37 root      20   0     0    0    0 S  0.0  0.0   0:00.00 linkwatch          
   38 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd         
   39 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kswapd0            
   40 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd               
   41 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/0              
   42 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/1              
   43 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/0           
   44 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/1           
   49 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthrotld/0         
   50 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthrotld/1         
   51 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pciehpd            
   53 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kpsmoused          
   54 root      20   0     0    0    0 S  0.0  0.0   0:00.00 usbhid_resumer     
   85 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kstriped           
  162 root      20   0     0    0    0 S  0.0  0.0   0:00.01 scsi_eh_0          
  166 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1          
  176 root      20   0     0    0    0 S  0.0  0.0   0:00.68 mpt_poll_0         
  177 root      20   0     0    0    0 S  0.0  0.0   0:00.00 mpt/0              
  178 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_2          
  306 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kdmflush           
  307 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kdmflush           
  326 root      20   0     0    0    0 S  0.0  0.0   0:03.24 flush-253:0        
  327 root      20   0     0    0    0 S  0.0  0.0   0:02.66 jbd2/dm-0-8        
  328 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ext4-dio-unwrit    
  415 root      16  -4  2720 1008  364 S  0.0  0.1   0:01.14 udevd              
  764 root      20   0     0    0    0 S  0.0  0.0   0:32.91 vmmemctl           
  898 root      18  -2  2716 1000  360 S  0.0  0.1   0:00.00 udevd              
  941 root      20   0     0    0    0 S  0.0  0.0   0:00.00 jbd2/sda1-8        
  942 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ext4-dio-unwrit    
 1032 root      20   0     0    0    0 S  0.0  0.0   0:00.05 kauditd            
 1194 root      16  -4 12932  824  600 S  0.0  0.0   0:00.12 auditd             
 1219 root      20   0 37000 1548 1040 S  0.0  0.1   0:00.11 rsyslogd           
 1248 root      20   0  2268  628  444 S  0.0  0.0   0:04.43 irqbalance         
 1262 rpc       20   0  2576  792  592 S  0.0  0.0   0:00.06 rpcbind            
 1277 dbus      20   0  3100 1056  744 S  0.0  0.1   0:00.35 dbus-daemon        
 1287 root      20   0 32312 4416 3776 S  0.0  0.2   0:01.25 NetworkManager     
 1294 root      20   0  4868 2272 1912 S  0.0  0.1   0:00.08 modem-manager      
 1306 rpcuser   20   0  2840 1272  868 S  0.0  0.1   0:00.03 rpc.statd          
 1335 root      20   0 13332 2752 2084 S  0.0  0.1   0:00.01 cupsd              
 1365 root      20   0  7628  648  432 S  0.0  0.0   0:00.00 wpa_supplicant     
 1366 root      20   0  2020  592  508 S  0.0  0.0   0:00.00 acpid              
 1375 haldaemo  20   0 17764 4844 4064 S  0.0  0.3   0:00.35 hald               
 1376 root      20   0  4052 1128  964 S  0.0  0.1   0:00.00 hald-runner        
 1418 root      20   0  4124 1060  928 S  0.0  0.1   0:00.00 hald-addon-inpu    
 1422 haldaemo  20   0  3712 1008  892 S  0.0  0.1   0:00.00 hald-addon-acpi    
 1443 root      20   0 30040 1592 1220 S  0.0  0.1   0:00.87 automount          
 1503 root      20   0 51984 4840 1492 S  0.0  0.3   0:01.18 php-fpm            
 1504 www       20   0 51984 4208  860 S  0.0  0.2   0:00.00 php-fpm            
 1505 www       20   0 51984 4212  864 S  0.0  0.2   0:00.00 php-fpm            
 1513 root      20   0  7776  884  396 S  0.0  0.0   0:00.00 nginx              
 1518 www       20   0 18388  11m  628 S  0.0  0.6   0:02.46 nginx              
 1519 www       20   0 18388  11m  532 S  0.0  0.6   0:00.08 nginx              
 1526 root      20   0  8968  988  480 S  0.0  0.1   0:00.01 sshd               
 1540 root      20   0  6276 1392 1188 S  0.0  0.1   0:00.04 mysqld_safe        
 2080 mysql     20   0  212m  25m 5064 S  0.0  1.4   0:05.39 mysqld             
 2084 root      20   0  6672  940  824 S  0.0  0.0   0:00.00 abrtd              
 2092 root      20   0  7148 1196  636 S  0.0  0.1   0:00.06 crond              
 2103 root      20   0  2988  468  320 S  0.0  0.0   0:00.00 atd                
 2115 root      20   0  8808  564  272 S  0.0  0.0   0:00.02 certmonger         
 2128 root      20   0  2008  476  424 S  0.0  0.0   0:00.00 mingetty           
 2130 root      20   0  2008  476  424 S  0.0  0.0   0:00.00 mingetty           
 2132 root      20   0  2008  472  424 S  0.0  0.0   0:00.00 mingetty           
 2134 root      20   0  2008  480  424 S  0.0  0.0   0:00.00 mingetty           
 2136 root      20   0  2008  480  424 S  0.0  0.0   0:00.00 mingetty           
 2138 root      20   0  2008  480  424 S  0.0  0.0   0:00.00 mingetty           
 2139 root      18  -2  2716  980  340 S  0.0  0.1   0:00.00 udevd              
17357 root      20   0 11912 3520 2760 S  0.0  0.2   0:00.08 sshd               
17359 root      20   0  6876 1804 1468 S  0.0  0.1   0:00.02 bash               
17959 root      20   0  2836 1272  988 S  0.0  0.1   0:00.00 dhclient           
18005 smmsp     20   0 12612 1764  684 S  0.0  0.1   0:00.00 sendmail           
18006 root      20   0 12988 2004  740 S  0.0  0.1   0:01.19 sendmail           
18107 root      20   0  2020  480  412 T  0.0  0.0   0:00.00 vmstat             
18757 root      20   0 12044 3524 2768 S  0.0  0.2   0:00.05 sshd               
18759 root      20   0  2712  984  768 R  0.0  0.1   0:00.00 top                

commands :free -m
######## ip: 192.168.47.196######2018:3:4:16:9:46====
             total       used       free     shared    buffers     cached
Mem:          1869        795       1074          0        166        479
-/+ buffers/cache:        148       1720
Swap:         2015          0       2015
commands :vmstat 1 1
######## ip: 192.168.47.196######2018:3:4:16:9:46====
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1100048 170256 491472    0    0     6     5   29   42  0  0 99  0  0   
commands :top -b -n 1 
######## ip: 192.168.41.11######2018:3:4:16:9:47====
top - 16:09:36 up 34 days, 23:46,  0 users,  load average: 0.00, 0.00, 0.00
Tasks: 100 total,   1 running,  99 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4056492k total,  1399408k used,  2657084k free,   246380k buffers
Swap:        0k total,        0k used,        0k free,   496020k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      20   0 19352 1540 1236 S  0.0  0.0   0:00.68 init               
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd           
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.46 migration/0        
    4 root      20   0     0    0    0 S  0.0  0.0   0:01.10 ksoftirqd/0        
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0          
    6 root      RT   0     0    0    0 S  0.0  0.0   0:02.20 watchdog/0         
    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.22 migration/1        
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/1          
    9 root      20   0     0    0    0 S  0.0  0.0   0:01.24 ksoftirqd/1        
   10 root      RT   0     0    0    0 S  0.0  0.0   0:01.72 watchdog/1         
   11 root      20   0     0    0    0 S  0.0  0.0   1:41.08 events/0           
   12 root      20   0     0    0    0 S  0.0  0.0   2:25.67 events/1           
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events/0           
   14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events/1           
   15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_long/0      
   16 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_long/1      
   17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_power_ef    
   18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_power_ef    
   19 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup             
   20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper            
   21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns              
   22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr          
   23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                 
   24 root      20   0     0    0    0 S  0.0  0.0   0:07.93 sync_supers        
   25 root      20   0     0    0    0 S  0.0  0.0   0:00.13 bdi-default        
   26 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0      
   27 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/1      
   28 root      20   0     0    0    0 S  0.0  0.0   0:02.26 kblockd/0          
   29 root      20   0     0    0    0 S  0.0  0.0   0:01.05 kblockd/1          
   30 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid             
   31 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify       
   32 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug      
   33 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux            
   34 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_sff/0          
   35 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_sff/1          
   36 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd      
   37 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd              
   38 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kseriod            
   39 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md/0               
   40 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md/1               
   41 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md_misc/0          
   42 root      20   0     0    0    0 S  0.0  0.0   0:00.00 md_misc/1          
   43 root      20   0     0    0    0 S  0.0  0.0   0:00.00 linkwatch          
   46 root      20   0     0    0    0 S  0.0  0.0   0:00.61 khungtaskd         
   47 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kswapd0            
   48 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd               
   49 root      39  19     0    0    0 S  0.0  0.0   0:03.93 khugepaged         
   50 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/0              
   51 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/1              
   52 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/0           
   53 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/1           
   60 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthrotld/0         
   61 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthrotld/1         
   63 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kpsmoused          
   64 root      20   0     0    0    0 S  0.0  0.0   0:00.00 usbhid_resumer     
   65 root      20   0     0    0    0 S  0.0  0.0   0:00.00 deferwq            
   97 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kdmremove          
   98 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kstriped           
  280 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0          
  281 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1          
  330 root      20   0     0    0    0 S  0.0  0.0   0:00.00 virtio-blk         
  401 root      20   0     0    0    0 S  0.0  0.0   0:08.18 jbd2/vda1-8        
  402 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ext4-dio-unwrit    
  500 root      16  -4 10796  916  348 S  0.0  0.0   0:00.05 udevd              
  663 root      20   0     0    0    0 S  0.0  0.0   0:00.00 virtio-net         
  684 root      20   0     0    0    0 S  0.0  0.0   0:00.00 vballoon           
  843 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kauditd            
  846 root      20   0  1488  628  544 S  0.0  0.0  14:07.76 aliyun-service     
  886 root      20   0     0    0    0 S  0.0  0.0   0:00.06 ib_addr            
  891 root      20   0     0    0    0 S  0.0  0.0   0:00.00 infiniband/0       
  892 root      20   0     0    0    0 S  0.0  0.0   0:00.00 infiniband/1       
  901 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ib_mcast           
  906 root      20   0     0    0    0 S  0.0  0.0   0:00.00 iw_cm_wq           
  911 root      18  -2 10792  892  328 S  0.0  0.0   0:00.00 udevd              
  912 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ib_cm/0            
  913 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ib_cm/1            
  918 root      20   0     0    0    0 S  0.0  0.0   0:00.00 rdma_cm            
  939 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ipoib_flush        
 1062 root      20   0     0    0    0 S  0.0  0.0   0:08.54 flush-252:0        
 1123 root      20   0  245m 5192 1120 S  0.0  0.1   0:06.21 rsyslogd           
 1145 nscd      20   0  615m 1516 1012 S  0.0  0.0   0:14.69 nscd               
 1203 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cqueue             
 1226 root      20   0 66256 1200  468 S  0.0  0.0   0:01.14 sshd               
 1237 ntp       20   0 26600 1688 1188 S  0.0  0.0   0:11.36 ntpd               
 1257 exim      20   0 93688 1896  868 S  0.0  0.0   0:00.33 exim               
 1283 root      20   0  114m 1288  672 S  0.0  0.0   0:16.22 crond              
 1339 root      20   0  4060  548  472 S  0.0  0.0   0:00.00 mingetty           
 1341 root      20   0  4060  544  472 S  0.0  0.0   0:00.00 mingetty           
 1343 root      20   0  4060  548  472 S  0.0  0.0   0:00.00 mingetty           
 1345 root      20   0  4060  544  472 S  0.0  0.0   0:00.00 mingetty           
 1347 root      20   0  4060  548  472 S  0.0  0.0   0:00.00 mingetty           
 1349 root      20   0  4060  544  472 S  0.0  0.0   0:00.00 mingetty           
 1350 root      18  -2 10792  880  316 S  0.0  0.0   0:00.00 udevd              
 1396 root      20   0 11340 1400 1140 S  0.0  0.0   0:00.00 mysqld_safe        
 1548 mysql     20   0 1694m 458m 7928 S  0.0 11.6   6:17.59 mysqld             
 4810 root      20   0 24608 2664 2040 S  0.0  0.1   1:45.30 AliYunDunUpdate    
 4840 root      20   0  121m  13m 8460 S  0.0  0.3   7:47.17 AliYunDun          
23353 root      20   0  100m 4384 3360 S  0.0  0.1   0:00.02 sshd               
23355 gstudent  20   0  100m 1836  812 S  0.0  0.0   0:00.00 sshd               
23356 gstudent  20   0 15024 1088  828 R  0.0  0.0   0:00.00 top                

commands :free -m
######## ip: 192.168.41.11######2018:3:4:16:9:47====
             total       used       free     shared    buffers     cached
Mem:          3961       1366       2594          0        240        484
-/+ buffers/cache:        641       3319
Swap:            0          0          0
commands :vmstat 1 1
######## ip: 192.168.41.11######2018:3:4:16:9:47====
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 2656960 246380 496020    0    0     0     1    1    5  0  0 100  0  0  

python获取linux中top信息

标签:python获取linux中top信息

原文地址:http://blog.51cto.com/357712148/2082807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!