最近有个需求,想获取部分服务器上运行了那些应用服务,一台台去看,太费劲了,参考牛人写了此脚本,后期再加上一个前端页面做一些简单的运维维护的工作,凑合着先用着,待完善,
注:此脚本依懒于安装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 远程执行命令、发布文件的更多相关文章

  1. 远程执行命令和文件分发shell脚本

    deploy.conf node01,all,other,datanode,journalnode,zookeeper, node02,all,other,datanode,journalnode,z ...

  2. python 远程执行命令

    1.简单版 # coding: utf-8 import paramiko import re from time import sleep def ssh(): ssh = paramiko.SSH ...

  3. python 远程执行命令、上传、下载举例

    使用python中的 paramiko 实现远程操作,需要安装 paramiko 模块. # vi pssh.py #!/usr/bin/python #coding=utf-8 ''' Create ...

  4. python远程执行命令

    def run(): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ...

  5. Python Paramiko实现sftp文件上传下载以及远程执行命令

    一.简介 Paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 安装模块 默认Python没有自带,需要手动安装: pip3 install par ...

  6. 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件

    本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...

  7. python之实现批量远程执行命令(堡垒机)

    python远程批量执行 我并不是一个专业的开发,我一直在学习linux运维,对于python也是接触不久,所以代码写的并不是很规范简洁. 前段时间一个同学找我一起做一个自动化运维平台,我对pytho ...

  8. 使用paramiko远程执行命令、下发文件

    写部署脚本时,难免涉及到一些远程执行命令或者传输文件. 之前一直使用sh库,调用sh.ssh远程执行一些命令,sh.scp传输文件,但是实际使用中还是比较麻烦的,光是模拟用户登陆这一点,还需要单独定义 ...

  9. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

随机推荐

  1. 简单Profibus/DP实验系统的组建

    转自:http://www.dndev.com/Profibus/profibustr/system_4.html 引言: 为了让更多刚接触到Profibus系统的朋友能对Profibus的网络架构及 ...

  2. PHP 更改session存储方式为Redis

    前言: 服务器默认的session存放方式是file.当客户端发送请求带有PHPSESSID时是顺序的去比对session存储文件,如果有5000个session文件,那就有可能需要比对4998次那么 ...

  3. 评论各组alpha发布

    单纯从用户和体验者的角度来评价. 天天向上组的连连看游戏和新锋组的俄罗斯方块游戏,从alpha发布的成果完成度来看,两个游戏现在都可以玩,但连连看的完成度更高,可选背景,可选音乐.俄罗斯方块还有其他界 ...

  4. PAT 1034 有理数四则运算

    https://pintia.cn/problem-sets/994805260223102976/problems/994805287624491008 本题要求编写程序,计算2个有理数的和.差.积 ...

  5. sql 设计规范

    设计规范: 1.采用有意义的字段名 尽可能地把字段描述的清楚些.当然,也别做过头了,比如CustomerShippingAddressStreetLine1虽然很富有说明性,但没人愿意键入这么长的名字 ...

  6. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  7. 静态属性加载到jvm时候就存放在数据共享区,而不是等new后出现

    静态属性加载到jvm时候就存放在数据共享区,而不是等new后出现.他的生命周期是 jvm结束 才会消失,一般的方法属性是对象结束后 就会消失.

  8. python使用selenium、PhantomJS获得网站cookie信息#windows

    首先python安装selenium,命令行中输入 pip install selenium 在执行代码如下代码时出现错误 driver=webdriver.PhantomJS() 错误如下 sele ...

  9. Cure HDU - 5879(预处理+技巧)

    Cure Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. yii2 查询数据库语法

    $query0 = ImGroupUser::find()->where(['gid'=>'56680dfc60b215d62104a4d8'])->select('user_cli ...