#!/usr/bin/python
#-*- coding: utf-8 -*-
import paramiko
import datetime
import os,tab,sys
from multiprocessing import Process, Pool
host_list = (
(‘192.168.1.127‘,‘root‘,‘123456‘),
(‘192.168.1.137‘,‘root‘,‘123456‘),
(‘192.168.1.143‘,‘root‘,‘123456‘),
)
def ssh_run(host_info,cmd):
try:
ip,username,password = host_info
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,password,timeout=5)
stdin,stdout,stderr = ssh.exec_command(cmd)
cmd_result = stdout.read(),stderr.read()
print ‘\033[32;1m正在执行命令:%s------主机名-------%s-----------\033[0m‘ %(cmd, ip),username
for line in cmd_result:
print line,
ssh.close()
except :
print "\033[31;1m连接失败----------%s-----------\033[0m"%ip
def get(host_info,local_dir,remote_dir):
try:
ip,username,password = host_info
t = paramiko.Transport((ip,22))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
files = sftp.listdir(remote_dir)
for f in files:
print "-"*30
print ‘\033[31;1m正在下载文件 %s: %s: \033[0m‘ % (ip, datetime.datetime.now())
print ‘\033[31;1m文件名:\033[0m‘,f
sftp.get(os.path.join(remote_dir, f), os.path.join(local_dir, f))
print ‘\033[32;1m下载成功 %s:\033[0m ‘ % datetime.datetime.now()
print "-"*30
t.close()
except:
print "\033[31;1m连接失败----------%s-----------\033[0m"%ip
def put(host_info,local_dir, remote_dir):
try:
ip,username,password = host_info
t = paramiko.Transport((ip, 22))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
files = os.listdir(local_dir)
for f in files:
print "-"*30
print ‘\033[31;1m正在上传文件到:\033[0m %s %s ‘ % (ip, datetime.datetime.now())
print ‘\033[31;1m文件名:\033[0m‘, f
sftp.put(os.path.join(local_dir, f), os.path.join(remote_dir, f))
print ‘\033[32;1m文件上传成功\033[0m %s ‘ % datetime.datetime.now()
print ‘-‘*30
t.close()
except:
print "\033[31;1m连接失败----------%s-----------\033[0m"%ip
#执行命令函数
def send_cmd():
cmd = raw_input("CMD:")
p = Pool(processes=2)
result_list = []
for h in host_list:
result_list.append(p.apply_async(ssh_run,[h,cmd]) )
for res in result_list:
res.get()
#执行发送文件函数
def send_file():
local_dir = raw_input(‘本地目录:‘)
remote_dir =raw_input(‘远程目录:‘)
p = Pool(processes=2)
result_list = []
for h in host_list:
result_list.append(p.apply_async(put,[h,local_dir,remote_dir]))
for res in result_list:
res.get()
def get_file():
local_dir = raw_input(‘本地目录‘)
remote_dir =raw_input(‘远程目录‘)
p = Pool(processes=2)
result_list = []
for h in host_list:
result_list.append(p.apply_async(get,[h,local_dir,remote_dir]))
for res in result_list:
res.get()
msg = """\033[31;1m
1.执行命令
2.发送文件
3.下载文件
0.退出
\033[0m
"""
while True:
try:
print msg
Choose = raw_input(‘\033[34;1m选择操作:\033[0m‘)
if int(len(Choose))==0:continue
if int(Choose) == 1:send_cmd()
if int(Choose) == 2:send_file()
if int(Choose) == 3:get_file()
if int(Choose) == 0:
print "By......"
sys.exit()
except KeyboardInterrupt:
continue
执行效果如下
本文出自 “服务器” 博客,请务必保留此出处http://zhangfang2012.blog.51cto.com/6380212/1577354
原文地址:http://zhangfang2012.blog.51cto.com/6380212/1577354