为了账号安全,请及时绑定邮箱和手机立即绑定

Python Fabric ssh 配置解读

标签:
Python

 

Fabric 2.4简介:

Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH, yielding useful Python objects in return.

简单说就是一个基于 ssh 执行远程 shell 命令返回一个 python 对象的一个 python 库。

Fabric 的大部分配置都是对 ssh 的配置, 而它的 ssh 协议是通过另一个 开源库 Paramiko 实现, 所以最终这些配置都会转换成 Paramiko 的 ssh 配置。

先看下官方的例子:

result = Connection('web1').run('hostname')

建立一个到 web1 这台服务器的一个 ssh 连接, 然后执行 hostname , 返回一个这次连接执行结果 python 对象。

这里就来到了本文的主题, 他是怎样连接对应的 host 也就是 Web1 的。

每一个 Fabric 的 Connection 都有一个 SSHClient 实例(Paramiko 中实现), 然后通过这个实例来连接上 Web1, 所以就需要 fabric 来提供这个实例连接所需要的必要参数, 这个是 SSHClient 连接的参数。

def connect(
        self,
        hostname,        port=SSH_PORT,
        username=None,
        password=None,
        pkey=None,
        key_filename=None,
        timeout=None,
        allow_agent=True,
        look_for_keys=True,
        compress=False,
        sock=None,
        gss_auth=False,
        gss_kex=False,
        gss_deleg_creds=True,
        gss_host=None,
        banner_timeout=None,
        auth_timeout=None,
        gss_trust_dns=True,
        passphrase=None,
    ):

Connection 是继承自 invoke 中的 Context, 就字面意思来说是提供这个 SSHClient 连接的上下文, 也就是 SSHClient 所需的参数。

Connection 中上下文中的参数又会通过 Fabric Config来提供, Connection 实例创建的时候会生成 Fabric 自己的 Config 或将外部传入 config 对象转换成 Fabric Config 对象。

Fabric Config 有一个对使用者来说比较有用的一个静态方法,

 @staticmethod
    def global_defaults():
        defaults = InvokeConfig.global_defaults()
        ours = {            # New settings
            "connect_kwargs": {},            "forward_agent": False,            "gateway": None,            "load_ssh_configs": True,            "port": 22,            "run": {"replace_env": True},            "runners": {"remote": Remote},            "ssh_config_path": None,            "tasks": {"collection_name": "fabfile"},            # TODO: this becomes an override/extend once Invoke grows execution
            # timeouts (which should be timeouts.execute)
            "timeouts": {"connect": None},            "user": get_local_user(),
        }
        merge_dicts(defaults, ours)        return defaults

这个其实是 Fabric 对 InvokeConfig 默认配置的一个扩展, 具体的可以看代码, 我们可通过覆盖这个方法, 来实现我们自己的默认或者说全局配置, 然后把这个配置传给 Connection, 为 Connection 中的连接提供必要的参数。

from fabric import Config as FabricConfigfrom invoke.config import merge_dictsclass MyConfig(FabricConfig):    @staticmethod
    def global_defaults():
        defaults = FabricConfig.global_defaults()
        my = {            "connect_kwargs": {"password":"123456"},            "port": 22,            "ssh_config_path": "./ssh_config",            "load_ssh_configs": False,            "user": "root",
        }
        merge_dicts(defaults, my)        return defaults

几个参数的意义:

  • connect_kwargs: 为 ssh connect 提供中的参数

  • port: ssh 连接的端口

  • ssh_config_path: ssh_config 的路径, 配置后只会读取这个路径 ssh_config 的配置, fabric 扩展的参数

  • load_ssh_configs: 没有配置 ssh_config_path 的时候, 是否读取系统和用户配置

  • user: 连接的用户

这些都是默认配置, 优先级是最低的, ssh_config_path 中配置的优先级会覆盖掉默认默认配置。 所以官方例子中 Web1 的配置来源可能有三种,ssh_config_path 中的配置, 系统的 ssh_config(/etc/ssh_config), 以及用户的 ssh_config(~/.)。

一个值得一提的点是:
当传入的 config 是一个对象实例的时候, 是不会再创建 Config 对象的,也就是说可以很多个 Connection 共用一个 Config, 这在连接比较多的服务器的时候很有用。

原文出处:https://www.cnblogs.com/nowg/p/9936701.html  

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消