python 远程执行命令、发布文件
最近有个需求,想获取部分服务器上运行了那些应用服务,一台台去看,太费劲了,参考牛人写了此脚本,后期再加上一个前端页面做一些简单的运维维护的工作,凑合着先用着,待完善,
注:此脚本依懒于安装fabric ,安装过程参考: http://5973819.blog.51cto.com/5963819/1532334
#!/usr/bin/env python
#coding:utf8
#####################################################
# Author: wangganyu188 wangganyu188@gmail.com
# Last modified: 2014-09-05
# Filename: sys_ops.py
##################################################### from fabric.api import env,run,put,get
from os import path
from re import findall
from sys import argv
from fabric.context_managers import hide
from time import sleep USER='root'
HOST,IP_LIST=[],[]
PORT=''
timeout = 1
CMD,getSRC,getDST,putSRC,putDST = '','','','','' for i in range(1,len(argv)+1):
#print i
if argv[i-1] == '-h' or len(argv) == 1:
print """
USAGE:
-c [cmd] The command you want the host(s) to run
-f [file] The file content multiple ip address you want to connect
-P [put] The local file that you want to upload to the remote host(s)
-G [get] The remote file that you want to download to the local host
-h [help] Print this help screen
""" if argv[i-1]=='-f':
if path.isfile('%s'%(argv[i])) == True:
file_list = open('%s'%(argv[i]),'r').readlines()
for line in file_list:
#print line
HOSTIP = line.split()[0]
HOSTPW = line.split()[1]
#print HOSTIP,'\n',HOSTPW
IP_LIST.append(HOSTIP)
env.password ='%s'%HOSTPW
#print IP_LIST,'\n',env.password
if argv[i-1] == '-c':
CMD = argv[i] if argv[i-1] == '-P':
p = src = argv[i].split(',')
putSRC = p[0]
putDST = p[1] if argv[i-1] == '-G':
g = src = argv[i].split(',')
getSRC = g[0]
getDST = g[1] else:
IP_PORT = []
if len(IP_LIST) != 0:
for ip in IP_LIST:
IP_PORT.append(ip + ':' + PORT) if CMD !='':
def command():
with hide('running'):
run("%s"%CMD) for ipport in IP_PORT:
env.host_string = ipport
print "Execute Command : \033[1;33;40m %s\033[0m at Host : \033[1;33;40m %s \033[0m" %(CMD,ipport.split(':')[0])
print "***************************************************************"
command()
print '***************************************************************' if putSRC and putDST != '':
def PUTupload():
with hide('running'):
put("%s"%(putSRC),"%s"%(putDST))
for ipport in IP_PORT:
env.host_string = ipport
print "PUT local file:\033[1;33;40m %s \033[0m to remote HOST:\033[1;33;40m %s\033[0m : \033[1;33;40m %s\033[0m" %(putSRC,ipport.split(':')[0],putDST)
print "*****************************************************************"
PUTupload()
print "*****************************************************************" if getSRC and getDST != '':
def GETdown():
with hide('running'):
get("%s"%(getSRC),"%s"%(getDST))
for ipport in IP_PORT:
env.host_string = ipport
print "GET remote file:\033[1;33;40m %s \033[0m from host :\033[1;33;40m %s\033[0m to local \033[1;33;40m %s\033[0m" %(getSRC,ipport.split(':')[0],getDST)
print "*****************************************************************"
GETdown()
print "*****************************************************************"
python 远程执行命令、发布文件的更多相关文章
- 远程执行命令和文件分发shell脚本
deploy.conf node01,all,other,datanode,journalnode,zookeeper, node02,all,other,datanode,journalnode,z ...
- python 远程执行命令
1.简单版 # coding: utf-8 import paramiko import re from time import sleep def ssh(): ssh = paramiko.SSH ...
- python 远程执行命令、上传、下载举例
使用python中的 paramiko 实现远程操作,需要安装 paramiko 模块. # vi pssh.py #!/usr/bin/python #coding=utf-8 ''' Create ...
- python远程执行命令
def run(): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ...
- Python Paramiko实现sftp文件上传下载以及远程执行命令
一.简介 Paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 安装模块 默认Python没有自带,需要手动安装: pip3 install par ...
- 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件
本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...
- python之实现批量远程执行命令(堡垒机)
python远程批量执行 我并不是一个专业的开发,我一直在学习linux运维,对于python也是接触不久,所以代码写的并不是很规范简洁. 前段时间一个同学找我一起做一个自动化运维平台,我对pytho ...
- 使用paramiko远程执行命令、下发文件
写部署脚本时,难免涉及到一些远程执行命令或者传输文件. 之前一直使用sh库,调用sh.ssh远程执行一些命令,sh.scp传输文件,但是实际使用中还是比较麻烦的,光是模拟用户登陆这一点,还需要单独定义 ...
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...
随机推荐
- Spring下使用开发webservice
依赖包 <!-- CXF Dependencies --> <dependency> <groupId>org.apache.cxf</groupId> ...
- sublime很常用快捷方式演示
来自为知笔记(Wiz)
- [转帖]学习一下centos7 新地方
总结的挺好 copy一下 慢慢学习: http://blog.itpub.net/312079/viewspace-2214440/ Centos7 单用户模式 centos7里不再有0-6启动级别 ...
- Find non-overlap jobs with max cost
Given a set of n jobs with [start time, end time, cost] find a subset so that no 2 jobs overlap and ...
- AJAX 跨域问题 php
原生ajax请求方式: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xxxx.com/demo/b/ ...
- Angular js Radio Button
症状: 绑定一个list radio button 老是只能绑定一行,纠结了很久 ,回家发现 原来是 name 用了同一个 ,坑啊,记录下 免得下次再犯. 之前的代码 <ul> & ...
- 【刷题】BZOJ 2346 [Baltic 2011]Lamp
Description 2255是一个傻X,他连自己家灯不亮了都不知道. 某天TZ大神路过他家,发现了这一情况, 于是TZ开始行侠仗义了. TZ发现是电路板的问题, 他打开了电路板,发现线路根本没有连 ...
- Spring点滴四:Spring Bean生命周期
Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...
- 解题:POI 2006 Periods of Words
题面 洛谷翻译有毒系列 正常人能看懂的题面:若$S$可以通过前缀$s$重复若干次(可重叠)来表示($s!=S$),则称$s$是$S$的一个循环串.求一个字符串所有前缀(包括本身)的最长循环串的长度之和 ...
- bzoj4427【Nwerc2015】Cleaning Pipes清理管道
题目描述 Linköping有一个相当复杂的水资源运输系统.在Linköping周围的出水点有一些水井.这些水通过管道输送到其它地点.每条管道是从某一个水井到城市的某个位置的直线管道. 所有管道在地下 ...