python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)

摘要:
password=passwd)33 sftp=paramik.SFTPClient.from_transport(t)34 files=sftp.listdir(remote_dir)35 36 37用于文件中的f:os.path.join(remote_dr,
 1 #-*- coding: utf-8 -*-
 2 #批量操作linux服务器(执行命令,上传,下载)
 3 #!/usr/bin/python
 4 import paramiko
 5 import datetime
 6 import os
 7 import threading
 8 def ssh2(ip,username,passwd,cmd):
 9     try:
10         paramiko.util.log_to_file('paramiko________.log')
11         ssh = paramiko.SSHClient()
12         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13         ssh.connect(ip,22,username,passwd,timeout=5)
14         for m in cmd:
15             stdin,stdout,stderr = ssh.exec_command(m)
16             #stdin.write("Y")   #简单交互,输入 ‘Y’
17             out = stdout.readlines()
18             # outerr = stderr.readlines()
19             #屏幕输出
20             for o in out:
21                 print o,
22             # for i in outerr:
23             #     print i,
24                 print '%s OK '%(ip)
25         ssh.close()
26     except :
27         print '%s Error '%(ip)
28 def download(ip, username, passwd, local_dir, remote_dir):
29    try:
30         paramiko.util.log_to_file('paramiko_download.log')
31         t = paramiko.Transport((ip,22))
32         t.connect(username=username,password=passwd)
33         sftp = paramiko.SFTPClient.from_transport(t)
34         files = sftp.listdir(remote_dir)
35 
36 
37         for f in files:
38             print ''
39             print '############################'
40             print 'Beginning to download file  from %s  %s ' % (ip, datetime.datetime.now())
41             print 'Downloading file:', os.path.join(remote_dir, f)
42             sftp.get(os.path.join(remote_dir, f), os.path.join(local_dir, f))#下载
43             print 'Download file success %s ' % datetime.datetime.now()
44             print ''
45             print '############################'
46         t.close()
47    except:
48         print "connect error!"
49 
50 
51 def upload(ip, username, passwd, local_dir, remote_dir):
52     try:
53         paramiko.util.log_to_file('paramiko_upload.log')
54         t = paramiko.Transport((ip, 22))
55         t.connect(username=username, password=passwd)
56         sftp = paramiko.SFTPClient.from_transport(t)
57         #files = sftp.listdir(remote_dir)
58         files = os.listdir(local_dir)
59         for f in files:
60             print ''
61             print '############################'
62             print 'Beginning to upload file  to %s  %s ' % (ip, datetime.datetime.now())
63             print 'Uploading file:', os.path.join(local_dir, f)
64             sftp.put(os.path.join(local_dir, f), os.path.join(remote_dir, f))#上传
65             print 'Upload file success %s ' % datetime.datetime.now()
66             print ''
67             print '############################'
68         t.close()
69     except:
70         print "connect error!"
71         print ip, "fail!"
72 
73 
74 if __name__=='__main__':
75 #  cmd = ['ls -lh /export/servers/mysql/log/mysql.log']#你要执行的命令列表
76     cmds=open("D:\batch\cmds.txt") #从文件读取命令
77     cmd=cmds.readlines()
78     cmds.close()
79     username = "root"  #用户名
80     #passwd = "xxxx"    #单台服务器时启用----------------------
81     #ip='192.168.12.19'  #单台服务器时启用----------------------
82     local_dir = "D:\batch\tmp"  #本地服务器目录
83     remote_dir = "/tmp/" #远程服务器目录
84     #threads = []   #多线程
85     print "Begin......"
86     hosts=open("D:\batch\list.txt") #本地服务器列表
87     host=hosts.readlines()
88     hosts.close()
89     for list in host:
90         line = list.split()
91         ip = line[0].strip()
92         pwd = line[1].strip()
93         # print ip,
94         # print pwd,
95         # b=threading.Thread(target=upload,args=(ip,username,pwd,local_dir,remote_dir))
96         # b.start()
97         a=threading.Thread(target=ssh2,args=(ip,username,pwd,cmd))
98         a.start()

免责声明:文章转载自《python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Unity中Instantiate物体失效问题pip download 只下载不安装命令的使用方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

zrender初步理解

很火的echarts底层也是采用了这个; 1. 底层是canvas画图,比较快相对于svg, 写自己类图包含hover等操作注意分层 2. canvas画布各个浏览器支持大小有限; 3. 实现画布无限大, 采用了group概念, 所有元素绑定group, 鼠标滚动计算group的position位置; 4. 画布上放大缩小则是采用了数学上的矩阵概念, 详情...

【转帖】在SQL Server中如何获得刚插入一条新记录的自动ID号

转自http://blog.csdn.net/wangji163163/archive/2008/05/09/2424191.aspx 在SQL Server中如何获得刚插入一条新记录的自动ID号收藏 ---------------------------------------------------------------  使用[IDENT_C...

Python接口自动化(三)post请求四种传送正文方式

HTTP协议规定POST提交的数据必须放在消息主题(entity-body)中,但协议并没有规定数据必须使用什么编码方式。 浏览器行为:Form表单提交 Http协议行为:Http1.1协议   我们知道,HTTP协议是以ASCII码传输,建立在TCP/IP协议之上的应用层规范。规范把HTTP请求分为三个部分:状态行、请求头、消息主体。 <meth...

h5页面利用canvas压缩图片并上传

由于现在手机拍摄的照片质量较高,为减轻服务器压力在上传图片时需要压缩后再进行上传。h5页面中压缩图片就需要用canvas来实现,通过固定canvas的宽高重绘图片,来达到压缩的目的。 <div style="margin:0 auto;60%;padding-top:80px;"> <input type="file" ac...

SQL server 主键自增ID 错乱

1.报错如下,插入数据时,报主键不满足约束,重复ID。   怀疑自增长ID 有问题,百度一下,可以查询和修改自增长ID的值。   //取最新自动增长值  select ident_current('表名')  然后使用 DBCC CHECKIDENT ('table_name', RESEED,new_reseed_value) 语句将该值指定为new_...

【深度学习系列】用Tensorflow实现经典CNN网络Vgg

  上周我们讲了经典CNN网络AlexNet对图像分类的效果,2014年,在AlexNet出来的两年后,牛津大学提出了Vgg网络,并在ILSVRC 2014中的classification项目的比赛中取得了第2名的成绩(第一名是GoogLeNet,也是同年提出的)。在论文《Very Deep Convolutional Networks for Large...