paramiko 远程运行以及上传文件(批量运维)
来源:互联网 发布:阿金淘宝店 编辑:程序博客网 时间:2024/06/11 16:07
#!/usr/bin/env python
#coding = utf-8
'''
version : 2015-01-21
author : vdon
contact : qfang.inc
password.csv fied format:
ipaddress,username,password
'''
import csv,os,sys
import paramiko
class SshRemote(object):
def __init__(self,ipaddress,username,password,port=22):
self.__ipaddress = ipaddress
self.__username = username
self.__password = password
self.__port = port
def execCommand(self,command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.__ipaddress,self.__port,self.__username,self.__password)
stdin,stdout,stderr = ssh.exec_command(command)
log_file = open('remoteDeploy.log','ab+')
sys.stdout = log_file
print "\n--- --- Remote machine %s log --- --- \n" % self.__ipaddress
print stdout.readlines()
log_file.close()
ssh.close()
def sftpSend(self,local_path,remote_path):
trans = paramiko.Transport((self.__ipaddress,self.__port))
trans.connect(username = self.__username,password = self.__password)
sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put(local_path,remote_path)
trans.close()
def main():
path_password = "password.csv"
if not os.path.isfile(path_password):
print "Can't find --password.csv-- under path : %s" % os.getcwd()
exit()
reader = csv.reader(open(path_password,'rb'))
for line in reader:
if reader.line_num == 1:
continue
ipaddress = line[0]
username = line[1]
password = line[2]
remote = SshRemote(ipaddress,username,password)
local_path = "/opt/script/safeInit.py"
remote_path = "/tmp/safeInit.py"
remote.sftpSend(local_path,remote_path)
remote.execCommand('/usr/bin/python /tmp/safeInit.py')
remote.execCommand('/bin/mkdir -pv /opt/script')
local_path = "/opt/script/iptables.sh"
remote_path = "/opt/script/iptables.sh"
remote.sftpSend(local_path,remote_path)
if __name__ == '__main__':
main()
#coding = utf-8
'''
version : 2015-01-21
author : vdon
contact : qfang.inc
password.csv fied format:
ipaddress,username,password
'''
import csv,os,sys
import paramiko
class SshRemote(object):
def __init__(self,ipaddress,username,password,port=22):
self.__ipaddress = ipaddress
self.__username = username
self.__password = password
self.__port = port
def execCommand(self,command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.__ipaddress,self.__port,self.__username,self.__password)
stdin,stdout,stderr = ssh.exec_command(command)
log_file = open('remoteDeploy.log','ab+')
sys.stdout = log_file
print "\n--- --- Remote machine %s log --- --- \n" % self.__ipaddress
print stdout.readlines()
log_file.close()
ssh.close()
def sftpSend(self,local_path,remote_path):
trans = paramiko.Transport((self.__ipaddress,self.__port))
trans.connect(username = self.__username,password = self.__password)
sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put(local_path,remote_path)
trans.close()
def main():
path_password = "password.csv"
if not os.path.isfile(path_password):
print "Can't find --password.csv-- under path : %s" % os.getcwd()
exit()
reader = csv.reader(open(path_password,'rb'))
for line in reader:
if reader.line_num == 1:
continue
ipaddress = line[0]
username = line[1]
password = line[2]
remote = SshRemote(ipaddress,username,password)
local_path = "/opt/script/safeInit.py"
remote_path = "/tmp/safeInit.py"
remote.sftpSend(local_path,remote_path)
remote.execCommand('/usr/bin/python /tmp/safeInit.py')
remote.execCommand('/bin/mkdir -pv /opt/script')
local_path = "/opt/script/iptables.sh"
remote_path = "/opt/script/iptables.sh"
remote.sftpSend(local_path,remote_path)
if __name__ == '__main__':
main()
0 0
- paramiko 远程运行以及上传文件(批量运维)
- python下paramiko模块学习之三:上传批量文件到远程主机
- python paramiko模块实现远程文件上传文件, 执行命令
- Paramiko中上传远程文件方法步骤详解
- [Python/自动化运维] 利用Paramiko模块执行批量上传/下载/执行命令/查看服务器列表
- 【他山之石】批量部署远程服务器工具:Paramiko
- python下paramiko模块学习之四:从远程主机批量下载文件到本机
- python下paramiko模块学习之五:批量主机上传文件
- [迎风奔雨] [自动化运维脚本] paramiko 基于ssh实现文件上传和下载
- python使用paramiko和SCPClient进行linux远程控制和上传和下载文件
- 动态添加控件以及批量上传文件
- 【python】通过paramiko远程拷贝文件
- python fabric paramiko 文件上传 下载
- PHP 远程上传图片以及文件
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python用paramiko模块上传本地目录到远程目录
- python paramiko利用sftp上传目录到远程
- android批量文件上传(android批量图片上传)
- 某公司3531板子调试笔记--转载
- oracle-Oracle Partition 分区详细总结
- 如何在Mac OS X上安装 Ruby运行环境
- ubuntu12.04下ROR环境配置
- SecureCRT控制台连接MySQL的编码问题
- paramiko 远程运行以及上传文件(批量运维)
- 前台UI框架
- WineQQ2012 最新下载
- 22.链表中倒数第k个结点
- 1.机器学习 学习心得 - k 近邻算法
- 性能调优攻略
- 将一个数字转换为有千分位的格式
- rtsp流媒体服务器的搭建--转载
- UVA1377 Ruler bfs+状压搜索