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
smallpython
V2EX  ›  Python

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

  •  
  •   smallpython · 2020-10-23 13:37:11 +08:00 · 1700 次点击
    这是一个创建于 1252 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在使用 scp 命令的时候, 需要在信任对方主机的时候输入 yes, 且之后还要输入密码

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

    比如需求是把 a.txt 复制到 100 台服务器上, 能够做成自动化的脚本吗? 程序自己输入 yes 及密码
    14 条回复    2020-10-23 15:46:22 +08:00
    gnozix
        1
    gnozix  
       2020-10-23 13:42:46 +08:00   ❤️ 1
    感觉你需要的是 ansible
    zydxn
        2
    zydxn  
       2020-10-23 13:44:43 +08:00   ❤️ 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  
       2020-10-23 13:46:00 +08:00   ❤️ 2
    用 expect 就行了

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

    这是 ssh 的例子
    huangmingyou
        4
    huangmingyou  
       2020-10-23 13:46:04 +08:00   ❤️ 1
    ssh 有参数忽略主机 key 认证,另外用密钥文件替代口令。
    wwqgtxx
        5
    wwqgtxx  
       2020-10-23 13:47:46 +08:00
    是可以的,不过你需要用 subprocess.Popen 去完成交互
    smallpython
        6
    smallpython  
    OP
       2020-10-23 13:49:21 +08:00
    @MoYi123
    @huangmingyou
    我想问的是有没有什么方法可以让程序自己输入, 而不是绕开输入, 因为如果绕开的话, 虽然功能解决了, 但是还是不知道怎么让 Python 自己去填写这个信息
    smallpython
        7
    smallpython  
    OP
       2020-10-23 13:53:41 +08:00
    @wwqgtxx 能给个具体例子吗?我自己测试 stdin.write()方法只有在本地命令才会生效, 涉及到这种两台机器交互的情况就没用了, 还是会让我手动输入密码
    tony9413
        8
    tony9413  
       2020-10-23 13:56:05 +08:00
    paramiko 正解,如果不会,可以试试 robot 封装的[SSHLibrary]( http://robotframework.org/SSHLibrary/)
    bairdshi
        9
    bairdshi  
       2020-10-23 15:26:16 +08:00
    我之前研究了许久 fanric 是最佳解 其他都是垃圾
    bairdshi
        10
    bairdshi  
       2020-10-23 15:26:36 +08:00
    @bairdshi fabric 不小心打错了
    bairdshi
        11
    bairdshi  
       2020-10-23 15:32:24 +08:00   ❤️ 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  
       2020-10-23 15:33:26 +08:00
    paramiko
    css3
        13
    css3  
       2020-10-23 15:33:45 +08:00
    把 a.txt 复制到 100 台服务器上, 这还不用 ansible ???
    CallMeReznov
        14
    CallMeReznov  
       2020-10-23 15:46:22 +08:00
    paramiko
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1071 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:12 · PVG 03:12 · LAX 12:12 · JFK 15:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.