最近闲着学习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
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索bytes
, broadcast
, vif
, tx
, running
, error 34
, FF
, overrun
errors
python paramiko模块、python paramiko、python paramiko 安装、python paramiko 下载、python paramiko 详解,以便于您获取更多的相关知识。