#!/usr/bin/python2
# coding:utf-8
import paramiko
import sys
class Linux(object):
# 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
def __init__(self, ip, username, password, timeout=30):
self.ip = ip
self.username = user
self.password = passwd
self.timeout = timeout
# transport和chanel
self.t = ''
self.chan = ''
# 链接失败的重试次数
self.try_times = 3
def ssh_cmd(self, cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip,22,self.username,self.password,timeout=5)
stdin,stdout,stderr = ssh.exec_command(cmd)
print(stdout.read())
# print(' ===== :: %s OK\n'%(ip))
ssh.close()
except Exception,e:
print('%s Error:: %s \n' %(ip, e))
def sftp_put(self, localfile, remotefile):
t = paramiko.Transport(sock=(self.ip, 22))
t.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(localfile, remotefile)
t.close()
if __name__ == "__main__":
args = sys.argv[1:]
print "args === ", args
ip_str = '10.33.81.'
user = 'root'
passwd = 'apoidea_ceph'
remotefile = r'/usr/local/bin/daemo.py'
localfile = r'/data/ansible/ansible_test/daemo.py'
num = 1
for i in range(160,172):
ip = ip_str + str(i)
print num,"=====",ip
num += 1
host = Linux(ip, user, passwd)
if args[0] == "copy":
host.sftp_put(localfile,remotefile)
elif args[0] == "cmd":
cmd = args[1]
host.ssh_cmd(cmd)