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

通过 Paramiko 的 SSH 连接执行命令的结果如何实时输出?

  •  
  •   todayzhou · Feb 21, 2018 · 13958 views
    This topic created in 3182 days ago, the information mentioned may be changed or developed.
    import paramiko

    trans = paramiko.Transport(('x.x.x.x', 22))
    trans.connect(username='root', password='123123')
    ssh = paramiko.SSHClient()
    ssh._transport = trans
    stdin, stdout, stderr = ssh.exec_command('tcpdump -i any -nne -c 10')
    print(stdout.read().decode())
    ssh.close()

    这里打印出 stdout 的时候已经是命令执行的所有结果,如何每收到一次数据就打印出来,比如我执行的 tcpdump 命令,我想实现每捕获到一个报文就实时的显示出来,而不是等抓了 10 个以后再一起显示。
    7 replies  •  2018-09-09 22:37:50 +08:00
    dandogn
    dandogn
    Feb 22, 2018
    2
    用消息通道,提示你:invoke_shell()
    todayzhou
    todayzhou
    OP
    Feb 22, 2018
    3
    @ysc3839 谢谢,昨天我也自己看到了,测试了可以。
    todayzhou
    todayzhou
    OP
    Feb 22, 2018
    4
    @dandogn 谢谢你,invoke_shell 和我需要的还是有点区别。多谢!
    fanne
    fanne
    Sep 5, 2018
    5
    @todayzhou #4 大佬,这个问题你后来怎么解决了,我目前也遇到此问题,试了 stackoverflow 里的,貌似也不行
    fanne
    fanne
    Sep 5, 2018
    6
    @todayzhou #3 顺序错了,现在可以了
    fanne
    fanne
    Sep 9, 2018
    7
    @todayzhou #3 hello,你的中文怎么处理的?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4145 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 01:56 · PVG 09:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.