Skip to content

Commit

Permalink
新增 dump_default_config 子命令, 为子命令添加示例, 将目标组的选取改为可选参数
Browse files Browse the repository at this point in the history
  • Loading branch information
souloss committed Oct 12, 2021
1 parent 555276d commit 2014faa
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions pypssh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/env python
import configparser
import re
import os
import sys
import platform
import pprint
Expand All @@ -18,7 +19,26 @@
import gevent
import json as jso


default_config = """
# 默认变量
[vars]
# 用户名
user=root
# 密码
password=root
# all 组主机列表
[all]
localhost
# g1 组主机列表
[g1]
localhost:22:root:root
# g1 组变量
[g1:vars]
password=root
"""
logging.basicConfig(level=logging.ERROR,format='%(asctime)s:%(name)s:%(levelname)s:%(message)s')
# logging.basicConfig(level = logging.DEBUG,format = '%(asctime)s:%(name)s:%(levelname)s:%(message)s')
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -108,13 +128,12 @@ def get_client():
@click.option('-u', '--username', type=str, help="ssh帐号", required=False)
@click.option('-p', '--password', type=str, help="ssh密码", required=False)
@click.option('-P','--port',type=int,help='ssh端口',default=22,required=False)
@click.argument('target', type=str, nargs=1, required=True)
# @click.argument('target', type=str, nargs=1, required=True)
@click.option('-g','--target',type=str,help='目标组',required=False)
def cli(inventory, debug, username, password, port, target):
"""
该脚本用于批量执行命令/脚本以及批量上传下载文件, 需要注意的是:
\n
- 上传下载文件不支持通配符,需要明确指定文件/目录
\n
该脚本用于批量执行命令/脚本以及批量上传下载文件, 需要注意的是:\n
- 上传下载文件不支持通配符,需要明确指定文件/目录\n
- 使用 test / prints 可以测试端口/ssh连通性和目标选取到的数据
"""
if debug:
Expand All @@ -130,6 +149,17 @@ def cli(inventory, debug, username, password, port, target):
logger.debug("Host Selected is %s" % repr(host_selected))


@cli.command()
def dump_default_config():
"""
打印选择到的主机信息
"""
os.makedirs("/etc/pypssh",exist_ok=True)
with open("/etc/pypssh/inventory.conf","w+") as file:
file.write(default_config)
pprint.pprint("默认配置写入 /etc/pypssh/inventory.conf 成功!")


@cli.command()
def prints():
"""
Expand All @@ -149,7 +179,9 @@ def prints():
)
def execute(command, sudo, json, view, template):
"""
为目标批量执行命令
为目标批量执行命令\n
example:\n
pypssh -g all execute -c "echo hello world"
"""
if json:
logging.disable(50)
Expand Down Expand Up @@ -190,6 +222,8 @@ def execute(command, sudo, json, view, template):
def put(local_file, remote_file):
"""
为目标批量上传文件
example:
pypssh -g all put /etc/yum.conf /etc/yum.conf
"""
client = get_client()
# greenlets = client.copy_file(local_file,remote_file,recurse=True)
Expand All @@ -202,7 +236,9 @@ def put(local_file, remote_file):
@click.argument('local_file', type=click.types.Path())
def pull(remote_file, local_file):
"""
为目标批量下载文件
为目标批量下载文件\n
example:\n
pypssh -g all pull /etc/yum.conf /etc/yum.conf
"""
client = get_client()
# greenlets = client.copy_remote_file(remote_file,local_file,recurse=True)
Expand All @@ -215,11 +251,13 @@ def pull(remote_file, local_file):
@click.option('--json', flag_value=True, type=bool, required=False)
@click.option('--ssh-test/--no-ssh-test', default=True, type=bool)
def test(timeout, json, ssh_test):
if json:
logging.disable(50)
"""
测试端口/ssh的连通性
测试端口/ssh的连通性\n
example:\n
pypssh -g all test
"""
if json:
logging.disable(50)
def _connect_test(host):
s = gevent.socket.socket(
gevent.socket.AF_INET, gevent.socket.SOCK_STREAM)
Expand Down Expand Up @@ -282,7 +320,9 @@ def _ssh_test(host):
@click.pass_context
def execfile(ctx, script_file, json, template, script_arg, env, attachment, workdir):
"""
使本地脚本文件批量下发到远程执行
使本地脚本文件批量下发到远程执行\n
example:\n
pypssh -g all execfile test.sh arg1
"""
if json:
logging.disable(50)
Expand All @@ -305,8 +345,11 @@ def execfile(ctx, script_file, json, template, script_arg, env, attachment, work

@cli.command()
def version():
"""
输出版本信息
"""
addr = "https://github.com/witchc/pypssh"
vno = "v0.0.9"
vno = "v0.1.0"
interrupt_version = "Python " + ' '.join(sys.version.split('\n'))
print(
"\n".join
Expand All @@ -317,5 +360,6 @@ def version():
f"发行版: {platform.platform()}"
])
)

if __name__ == '__main__':
cli()

0 comments on commit 2014faa

Please sign in to comment.