首页 > OOP语言 > 其它 > 正文

python中paramiko模块入门 一:ssh登录和执行命令
2013-07-10 00:00:00   来源:www.bianceng.cn   评论:0 点击:

暂时无描述信息

最近闲着学习python,看到有个paramiko模块,貌似很强大,学着写了个一个小程序,可以直接在window下登录到linux,执行并返回执行结果。

下面直接贴代码:

#!/usr/bin/env python    
import paramiko    
        
#hostname='192.168.0.102'    
hostname='172.28.102.250'
username='root'
password='abc'
        
#port=22    
if __name__=='__main__':    
        paramiko.util.log_to_file('paramiko.log')    
        s=paramiko.SSHClient()    
        #s.load_system_host_keys()    
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
        s.connect(hostname = hostname,username=username, password=password)    
        stdin,stdout,stderr=s.exec_command('ifconfig;free;df -h')    
        print stdout.read()    
        s.close()

下面看下效果,呵呵:

s\501914252.HCA-FW9CX2X\Desktop\学习资料\python paramiko-ssh.py"    
eth0      Link encap:Ethernet  HWaddr 00:24:01:01:EA:5C
          inet addr:172.32.34.240  Bcast:172.32.255.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1428 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:190928 (186.4 KiB)  TX bytes:128 (128.0 b)    
          Interrupt:20
        
eth1      Link encap:Ethernet  HWaddr 00:1F:29:03:54:3D
          inet addr:172.28.102.250  Bcast:172.28.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:99698 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12305 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:36420324 (34.7 MiB)  TX bytes:923630 (901.9 KiB)    
        
lo        Link encap:Local Loopback    
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)    
        
peth1     Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:99686 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12333 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:36818715 (35.1 MiB)  TX bytes:1006230 (982.6 KiB)    
          Interrupt:20 Memory:f3000000-f3010000    
        
vif0.1    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:12305 errors:0 dropped:0 overruns:0 frame:0
          TX packets:99702 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:923630 (901.9 KiB)  TX bytes:36420811 (34.7 MiB)    
        
vif1.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:85175 errors:0 dropped:542 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:140 (140.0 b)  TX bytes:20377326 (19.4 MiB)    
        
vif2.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:84859 errors:0 dropped:411 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:140 (140.0 b)  TX bytes:20301953 (19.3 MiB)    
        
vif3.0    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:84337 errors:0 dropped:546 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:140 (140.0 b)  TX bytes:20174994 (19.2 MiB)    
        
virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)    
        
xenbr1    Link encap:Ethernet  HWaddr FE:FF:FF:FF:FF:FF    
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:86405 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:19498121 (18.5 MiB)  TX bytes:0 (0.0 b)    
        
             total       used       free     shared    buffers     cached    
Mem:       3359744     464724    2895020          0      31096     246296
-/+ buffers/cache:     187332    3172412
Swap:      5406712          0    5406712
Filesystem            Size  Used Avail Use% Mounted on    
/dev/mapper/VolGroup00-LogVol00    
                      221G   25G  185G  12% /
/dev/sda1              99M   22M   73M  23% /boot    
tmpfs                 1.8G     0  1.8G   0% /dev/shm    
none                  1.8G  152K  1.8G   1% /var/lib/xenstored

代码很简单,大家有兴趣,可以试验下,希望对您的学习和工作有帮助。

本文出自 “你是路人甲还是霍元甲” 博客,请务必保留此出处http://world77.blog.51cto.com/414605/668107

相关热词搜索:

上一篇:python中paramiko模块入门 二:利用配置文件登录批量主机
下一篇:如何将社交网络添加到网站

分享到: 收藏