使用paramiko在windows下批量登陆linux服务器

###环境:
windows8.1 X86_64

###被测试的机器:
192.168.100.228,192.168.100.240

###脚本如下:

#!/bin/env python
import paramiko
import os
import datetime
from ConfigParser import ConfigParser

ConfigFile = 'ipaddr.txt'
config = ConfigParser()
config.read(ConfigFile)
hostname1=''.join(config.get('IP','ipaddr'))
address=hostname1.split(';')
print address
username = 'root'
password = '123456'
port = 22
if __name__=="__main__":
    for ip in address:
        paramiko.util.log_to_file('paramiko-ssh1.log')
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=ip,username=username,password=password)
        stdin,stdout,stderr=ssh.exec_command('ifconfig eth0;free -m')
        print stdout.read()
        ssh.close()
<!--more-->

###测试:
D:\Learning materials\python 项目\paramiko>python paramiko-ssh1.py
[‘192.168.100.228’, ‘192.168.100.240’]
eth0 Link encap:Ethernet HWaddr 00:0C:29:B0:1E:F0
inet addr:192.168.100.228 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12387067 errors:0 dropped:0 overruns:0 frame:0
TX packets:394545 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1454341229 (1.3 GiB) TX bytes:204703155 (195.2 MiB)

         total       used       free     shared    buffers     cached
Mem:           988        782        206          0        139        237
-/+ buffers/cache:        404        584
Swap:         1023         12       1011

eth0      Link encap:Ethernet  HWaddr 00:0C:29:F5:25:81
          inet addr:192.168.100.240  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef5:2581/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14497175 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4952252 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1252369013 (1.1 GiB)  TX bytes:333399775 (317.9 MiB)

         total       used       free     shared    buffers     cached
Mem:           356        347          9          0         31        109
-/+ buffers/cache:        206        150
Swap:         1023          6       1017
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!