V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hanks315
V2EX  ›  Python

fabric 如何实现两级 ssh+切换超级用户的操作

  •  
  •   hanks315 ·
    hanks · 2015-02-06 09:13:22 +08:00 · 5110 次点击
    这是一个创建于 3339 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如:
    user A on localhost--->remote1, change to superuser B--->remote C, execute command

    试着在fabric里写:
    sudo('ssh remote c && command', sudo_user=superuser B)
    可以登入到remote C服务器里,但直接就卡在那,无法执行剩下的command

    有看到资料里写使用gateway,但切换超级用户不知道怎么操作.

    请问一下有什么好的实现方式? 谢谢
    第 1 条附言  ·  2015-02-09 13:35:26 +08:00
    可以直接在remote1上运行
    sudo('ssh remote C "your command", sudo_user=Superuser B)
    2 条回复    2015-02-06 23:20:40 +08:00
    saber000
        1
    saber000  
       2015-02-06 15:51:02 +08:00   ❤️ 1
    用两个fabfile分开B和C的操作,先把对C的操作从A扔到B上,在A中操纵B执行fab命令
    dongweiming
        2
    dongweiming  
       2015-02-06 23:20:40 +08:00   ❤️ 1
    你可以看原作者的:

    http://bitprophet.org/blog/2012/11/05/gateway-solutions/

    或者这样?

    # coding=utf-8

    from fabric.api import run, env
    from fabric.exceptions import NetworkError

    via = [('user1@server1:22', '123'),
    ('user2@server2:22', '456'),
    ('user3@serve3:22', '789')]

    try:
    for host, passwd in via:
    env.gateway = env.host_string
    env.host_string = host
    env.password = passwd
    run('id')

    except NetworkError as e:
    print e
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2873 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.