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

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

  •  
  •   todayzhou · 2018-02-21 23:19:33 +08:00 · 12602 次点击
    这是一个创建于 2247 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2018-09-09 22:37:50 +08:00
    ysc3839
        1
    ysc3839  
       2018-02-22 02:53:30 +08:00 via Android
    dandogn
        2
    dandogn  
       2018-02-22 10:02:21 +08:00
    用消息通道,提示你:invoke_shell()
    todayzhou
        3
    todayzhou  
    OP
       2018-02-22 16:14:52 +08:00
    @ysc3839 谢谢,昨天我也自己看到了,测试了可以。
    todayzhou
        4
    todayzhou  
    OP
       2018-02-22 16:16:21 +08:00
    @dandogn 谢谢你,invoke_shell 和我需要的还是有点区别。多谢!
    fanne
        5
    fanne  
       2018-09-05 11:47:38 +08:00
    @todayzhou #4 大佬,这个问题你后来怎么解决了,我目前也遇到此问题,试了 stackoverflow 里的,貌似也不行
    fanne
        6
    fanne  
       2018-09-05 11:56:46 +08:00
    @todayzhou #3 顺序错了,现在可以了
    fanne
        7
    fanne  
       2018-09-09 22:37:50 +08:00
    @todayzhou #3 hello,你的中文怎么处理的?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5383 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.