推荐学习书目
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
smallpython
V2EX  ›  Python

如何使用 Python 的 subprocess 模块调用系统命令 scp?

  •  
  •   smallpython · Oct 23, 2020 · 2772 views
    This topic created in 2051 days ago, the information mentioned may be changed or developed.
    在使用 scp 命令的时候, 需要在信任对方主机的时候输入 yes, 且之后还要输入密码

    请问 python 能够模拟键盘输入这两个信息吗?

    比如需求是把 a.txt 复制到 100 台服务器上, 能够做成自动化的脚本吗? 程序自己输入 yes 及密码
    14 replies    2020-10-23 15:46:22 +08:00
    gnozix
        1
    gnozix  
       Oct 23, 2020   ❤️ 1
    感觉你需要的是 ansible
    Tsukihime
        2
    Tsukihime  
       Oct 23, 2020   ❤️ 1
    之前用 paramiko 做过类似的事情

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname, ssh_port, username=username, password=password)
    transport = client.get_transport()
    sftpClient = paramiko.SFTPClient.from_transport(transport)
    # 传输
    sftpClient.put(localpath, remotepath, callback=your_call_back)
    client.close()
    MoYi123
        3
    MoYi123  
       Oct 23, 2020   ❤️ 2
    用 expect 就行了

    #!/usr/bin/expect
    spawn ssh root@{host} -p {port}
    expect "{host}'s password: "
    send "{password}"
    interact

    这是 ssh 的例子
    huangmingyou
        4
    huangmingyou  
       Oct 23, 2020   ❤️ 1
    ssh 有参数忽略主机 key 认证,另外用密钥文件替代口令。
    wwqgtxx
        5
    wwqgtxx  
       Oct 23, 2020
    是可以的,不过你需要用 subprocess.Popen 去完成交互
    smallpython
        6
    smallpython  
    OP
       Oct 23, 2020
    @MoYi123
    @huangmingyou
    我想问的是有没有什么方法可以让程序自己输入, 而不是绕开输入, 因为如果绕开的话, 虽然功能解决了, 但是还是不知道怎么让 Python 自己去填写这个信息
    smallpython
        7
    smallpython  
    OP
       Oct 23, 2020
    @wwqgtxx 能给个具体例子吗?我自己测试 stdin.write()方法只有在本地命令才会生效, 涉及到这种两台机器交互的情况就没用了, 还是会让我手动输入密码
    tony9413
        8
    tony9413  
       Oct 23, 2020
    paramiko 正解,如果不会,可以试试 robot 封装的[SSHLibrary]( http://robotframework.org/SSHLibrary/)
    bairdshi
        9
    bairdshi  
       Oct 23, 2020
    我之前研究了许久 fanric 是最佳解 其他都是垃圾
    bairdshi
        10
    bairdshi  
       Oct 23, 2020
    @bairdshi fabric 不小心打错了
    bairdshi
        11
    bairdshi  
       Oct 23, 2020   ❤️ 1
    from fabric import task, Connection
    from invoke.tasks import call

    # r represents raspberry pi
    name_ip_mapping = {"r1": "192.168.122.3", "r2": "192.168.122.2",
    "r3": "192.168.122.7", "camera": "192.168.122.55"}

    ip_name_mapping = {v: k for k, v in name_ip_mapping.items()}


    @task
    def scp1(c):
    """
    copy scripts to raspberry pi 1
    """
    c = Connection(name_ip_mapping["r1"], user='pi', connect_kwargs={'password': 'xxxx'})
    c.put('r1/main.py', "r1")
    c.put('r1/config.py', "r1")
    c.put('r1/part.py', "r1")
    c.put('r1/logger.py', "r1")
    wangyzj
        12
    wangyzj  
       Oct 23, 2020
    paramiko
    css3
        13
    css3  
       Oct 23, 2020
    把 a.txt 复制到 100 台服务器上, 这还不用 ansible ???
    CallMeReznov
        14
    CallMeReznov  
       Oct 23, 2020
    paramiko
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2880 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 59ms · UTC 01:55 · PVG 09:55 · LAX 18:55 · JFK 21:55
    ♥ Do have faith in what you're doing.