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

paramiko connect 时候遇到的问题。

  •  
  •   fxxkgw · 2016-10-25 17:13:38 +08:00 · 3910 次点击
    这是一个创建于 2741 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果 Linux 服务器密码过期了, ssh 时候会有下面提示:

    You are required to change your password immediately (password aged)
    WARNING: Your password has expired.
    You must change your password now and login again!
    Changing password for user XX.
    Changing password for XX.
    (current) UNIX password: ###卡在这里等着输入密码
    
    当用 python 的 paramiko 模块通过用户名 密码登录时
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(host,port,user,pwd)###这个地方并没有任何报错
    s.exec_command(cmd, get_pty=True, timeout=500) #这个地方因为密码过期了,会卡住直到超时报错
    
    现在想在 s.connect 这个地方做个检测,如果碰到密码过期就返回错误,翻了参数也没发现有控制的地方,请教有什么好的处理方式么。
    
    2 条回复    2016-10-28 07:30:29 +08:00
    KIDJourney
        1
    KIDJourney  
       2016-10-26 10:26:14 +08:00
    exec_command 的时候把 stdin 和 stdout 拿出来,读一下 stdout ,密码过期的话用把新密码打到 stdin 里就行。

    参见[How to sudo in para]( http://stackoverflow.com/questions/6270677/how-to-run-sudo-with-paramiko-python)
    eyp82
        2
    eyp82  
       2016-10-28 07:30:29 +08:00
    我觉得配置 ssh 无密码登陆不是更好一些? 不需要你脚本里记录密码, 另外也不需要考虑密码过期的问题了.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5402 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:00 · PVG 14:00 · LAX 23:00 · JFK 02:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.