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

Python paramiko 怎么输入交互式的密码?

  •  
  •   DAPTX4869 · 2018-12-20 17:55:37 +08:00 · 4905 次点击
    这是一个创建于 1943 天前的主题,其中的信息可能已经有所发展或是发生改变。

    要对华为的 IBMC 更改密码,官方文档只给了这么一个流程 Fr1TgA.jpg

    https://s1.ax1x.com/2018/12/20/Fr1TgA.jpg

    搜索了下交互输入就只有这个 s.exec_command('su -root') stdin.write('password') stdin.flush()

    自己参考改了就是各种没反应... 或者报错

    求教

    第 1 条附言  ·  2018-12-21 16:59:33 +08:00
    ![Fs3SSJ.jpg]( https://s1.ax1x.com/2018/12/21/Fs3SSJ.jpg)

    最后用以上方法解决,不过这 API 有点看不懂...
    到底是 send 里面开始输入
    还是在 while 循环里面开始输入,
    试了下把 while 里面的拆出来拼接到 send 下面就不能交互了......

    各位老哥的推荐都看了来着...
    不过要 linux 环境我就没法折腾了...
    第 2 条附言  ·  2018-12-21 17:00:10 +08:00

    Fs3SSJ.jpg

    最后用以上方法解决,不过这 API 有点看不懂... 到底是 send 里面开始输入 还是在 while 循环里面开始输入, 试了下把 while 里面的拆出来拼接到 send 下面就不能交互了......

    各位老哥的推荐都看了来着... 不过要 linux 环境我就没法折腾了...

    DAPTX4869
        1
    DAPTX4869  
    OP
       2018-12-20 17:58:43 +08:00
    下班跑路顺手顶一下
    xpresslink
        2
    xpresslink  
       2018-12-20 18:47:26 +08:00   ❤️ 1
    调用 expect 来交互
    littlewey
        3
    littlewey  
       2018-12-20 18:53:26 +08:00 via iPhone   ❤️ 1
    pyexpect

    调 shell 用 sshpass
    DAPTX4869
        4
    DAPTX4869  
    OP
       2018-12-20 19:05:42 +08:00
    谢楼上各位,明天试试
    leoleoasd
        5
    leoleoasd  
       2018-12-20 19:12:53 +08:00
    少个\n 吧...
    DAPTX4869
        6
    DAPTX4869  
    OP
       2018-12-20 19:17:27 +08:00
    @leoleoasd 你说的这可能性我也试了,凉凉
    ThirdFlame
        7
    ThirdFlame  
       2018-12-20 19:31:33 +08:00   ❤️ 1
    passwd 修改密码可以这样,你可以参照修改下试试
    stdin, stdout, stderr = ssh_client.exec_command("passwd", timeout = 10)
    stdin.write("{0}\n{1}\n{1}\n".format(old_password, new_password))
    out,err = stdout.read(),stderr.read()
    DAPTX4869
        8
    DAPTX4869  
    OP
       2018-12-20 20:11:10 +08:00
    @ThirdFlame 好的,我明天测试
    4linuxfun
        9
    4linuxfun  
       2018-12-20 20:29:19 +08:00
    可以试试 fabric,又 watcher,可以类似 pexpect 进行正则匹配后输入信息
    DAPTX4869
        10
    DAPTX4869  
    OP
       2018-12-20 20:57:01 +08:00
    @4linuxfun 现在还没涉及到你所说的功能...能输入就 ko 了
    julyclyde
        11
    julyclyde  
       2018-12-21 13:43:26 +08:00
    stdin 和 tty 不同的
    ssh 是从 tty 读取密码的
    DAPTX4869
        12
    DAPTX4869  
    OP
       2018-12-21 17:04:44 +08:00
    @julyclyde 萌新不太清楚这个,学习了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5028 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:50 · PVG 17:50 · LAX 02:50 · JFK 05:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.