#!/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 executing command:%s------host name-------%s-----------\033[0m '% (cmd, IP), username
For line in Cmd_result:
Print line,
Ssh.close ()
Except:
Print "\033[31;1m Connection failed----------%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
Print "-" *30
print ' \033[31;1m downloading file%s:%s: \033[0m '% (IP, Datetime.datetime.now ())
print ' \033[31;1m file name: \033[0m ', F
Sftp.get (Os.path.join (Remote_dir, F), Os.path.join (Local_dir, F))
print ' \033[32;1m download successful%s:\033[0m '% Datetime.datetime.now ()
Print
Print "-" *30
T.close ()
Except
Print "\033[31;1m Connection failed----------%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
Print "-" *30
print ' \033[31;1m uploading file to: \033[0m%s%s '% (IP, Datetime.datetime.now ())
print ' \033[31;1m file name: \033[0m ', F
Sftp.put (Os.path.join (Local_dir, F), Os.path.join (Remote_dir, F))
print ' \033[32;1m file uploaded successfully \033[0m%s '% Datetime.datetime.now ()
Print
print '-' *30
T.close ()
Except
Print "\033[31;1m Connection failed----------%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 (' Local directory: ')
Remote_dir =raw_input (' Remote directory: ')
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 (' local directory ')
Remote_dir =raw_input (' Remote directory ')
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. Execution of commands
2. Sending files
3. Download the file
0. Exit
\033[0m
"""
While True:
Try
Print msg
Choose = raw_input (' \033[34;1m Select operation: \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
The execution effect is as follows
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/81/wKiom1RpdB7w84rNAAIZlOSj2ec019.jpg "title=" 1.png " alt= "Wkiom1rpdb7w84rnaaizlosj2ec019.jpg"/>
This article is from the "Server" blog, so be sure to keep this source http://zhangfang2012.blog.51cto.com/6380212/1577354
Python batch execution command send file