天天看點

python下paramiko子產品學習之一:ssh登入和執行指令

    最近閑着學習python,看到有個paramiko子產品,貌似很強大,學着寫了個一個小程式,可以直接在window下登入到linux,執行并傳回執行結果。

    下面直接貼代碼:

  1. #!/usr/bin/env python 
  2. import paramiko 
  3. #hostname='192.168.0.102' 
  4. hostname='172.28.102.250' 
  5. username='root' 
  6. password='abc' 
  7. #port=22 
  8. if __name__=='__main__': 
  9.         paramiko.util.log_to_file('paramiko.log') 
  10.         s=paramiko.SSHClient() 
  11.         #s.load_system_host_keys() 
  12.         s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
  13.         s.connect(hostname = hostname,username=username, password=password) 
  14.         stdin,stdout,stderr=s.exec_command('ifconfig;free;df -h') 
  15.         print stdout.read() 
  16.         s.close() 
  1. s\501914252.HCA-FW9CX2X\Desktop\學習資料\python paramiko-ssh.py" 
  2. eth0      Link encap:Ethernet  HWaddr 00:24:01:01:EA:5C 
  3.           inet addr:172.32.34.240  Bcast:172.32.255.255  Mask:255.255.255.0 
  4.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  5.           RX packets:1428 errors:0 dropped:0 overruns:0 frame:0 
  6.           TX packets:2 errors:0 dropped:0 overruns:0 carrier:0 
  7.           collisions:0 txqueuelen:1000 
  8.           RX bytes:190928 (186.4 KiB)  TX bytes:128 (128.0 b) 
  9.           Interrupt:20 
  10. eth1      Link encap:Ethernet  HWaddr 00:1F:29:03:54:3D 
  11.           inet addr:172.28.102.250  Bcast:172.28.255.255  Mask:255.255.0.0 
  12.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  13.           RX packets:99698 errors:0 dropped:0 overruns:0 frame:0 
  14.           TX packets:12305 errors:0 dropped:0 overruns:0 carrier:0 
  15.           collisions:0 txqueuelen:0 
  16.           RX bytes:36420324 (34.7 MiB)  TX bytes:923630 (901.9 KiB) 
  17. lo        Link encap:Local Loopback 
  18.           inet addr:127.0.0.1  Mask:255.0.0.0 
  19.           UP LOOPBACK RUNNING  MTU:16436  Metric:1 
  20.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
  21.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
  22.           collisions:0 txqueuelen:0 
  23.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b) 
  24. peth1     Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  25.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  26.           RX packets:99686 errors:0 dropped:0 overruns:0 frame:0 
  27.           TX packets:12333 errors:0 dropped:0 overruns:0 carrier:0 
  28.           collisions:0 txqueuelen:1000 
  29.           RX bytes:36818715 (35.1 MiB)  TX bytes:1006230 (982.6 KiB) 
  30.           Interrupt:20 Memory:f3000000-f3010000 
  31. vif0.1    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  32.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  33.           RX packets:12305 errors:0 dropped:0 overruns:0 frame:0 
  34.           TX packets:99702 errors:0 dropped:0 overruns:0 carrier:0 
  35.           collisions:0 txqueuelen:0 
  36.           RX bytes:923630 (901.9 KiB)  TX bytes:36420811 (34.7 MiB) 
  37. vif1.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  38.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  39.           RX packets:5 errors:0 dropped:0 overruns:0 frame:0 
  40.           TX packets:85175 errors:0 dropped:542 overruns:0 carrier:0 
  41.           collisions:0 txqueuelen:500 
  42.           RX bytes:140 (140.0 b)  TX bytes:20377326 (19.4 MiB) 
  43. vif2.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  44.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  45.           RX packets:5 errors:0 dropped:0 overruns:0 frame:0 
  46.           TX packets:84859 errors:0 dropped:411 overruns:0 carrier:0 
  47.           collisions:0 txqueuelen:500 
  48.           RX bytes:140 (140.0 b)  TX bytes:20301953 (19.3 MiB) 
  49. vif3.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  50.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  51.           RX packets:5 errors:0 dropped:0 overruns:0 frame:0 
  52.           TX packets:84337 errors:0 dropped:546 overruns:0 carrier:0 
  53.           collisions:0 txqueuelen:500 
  54.           RX bytes:140 (140.0 b)  TX bytes:20174994 (19.2 MiB) 
  55. virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00 
  56.           inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0 
  57.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  58.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
  59.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
  60.           collisions:0 txqueuelen:0 
  61.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b) 
  62. xenbr1    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF 
  63.           UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1 
  64.           RX packets:86405 errors:0 dropped:0 overruns:0 frame:0 
  65.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
  66.           collisions:0 txqueuelen:0 
  67.           RX bytes:19498121 (18.5 MiB)  TX bytes:0 (0.0 b) 
  68.              total       used       free     shared    buffers     cached 
  69. Mem:       3359744     464724    2895020          0      31096     246296 
  70. -/+ buffers/cache:     187332    3172412 
  71. Swap:      5406712          0    5406712 
  72. Filesystem            Size  Used Avail Use% Mounted on 
  73. /dev/mapper/VolGroup00-LogVol00 
  74.                       221G   25G  185G  12% / 
  75. /dev/sda1              99M   22M   73M  23% /boot 
  76. tmpfs                 1.8G     0  1.8G   0% /dev/shm 
  77. none                  1.8G  152K  1.8G   1% /var/lib/xenstored