起因

线上环境是在内网,登陆线上环境需要使用 VPN + 堡垒机 登陆,但是我日常登陆线上环境都是 VPN + 堡垒机 + Socks5常驻代理,在shell端只需要保存会话,会话使用socks5代理即可无缝ssh到线上服务器。

今天来发现无法登陆服务器,但是ssh *** -f -N ****@**** 建立后台socks的方式并不会把密码过期的问题暴露出来,经过多次尝试,手动登陆服务器才发现用于登陆服务器的密码已过期。

手动更新密码后问题恢复。

解决问题

由于这一批服务器都存在密码过期的问题,所以打算尝试使用 Ansible 来批量登陆服务器更新密码。

https://github.com/ansible/ansible/issues/1619 受到国外大神的解答,尝试进行处理。

使用 Ansible expect 模块来进行处理。

python 需要安装 pexpect 包。

pip install pexpect

正常手动操作的console返回信息如下:

You are required to change your password immediately (password aged)
Last login: Thu Aug 13 11:23:46 2020 from 1*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

playbook task 内容:

- name: Set password if expired
delegate_to: 127.0.0.1
become: no
expect:
command: ssh {{ ansible_ssh_common_args }} {{ ansible_user }}@{{ inventory_hostname }}
timeout: 10
responses:
"password:":
# we couldn't keep the same old password
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass }}"
- "{{ ansible_ssh_pass }}"
# if succesfully login then quit
"\\~\\]\\$": exit
register: status
changed_when: "'authentication tokens updated successfully' in status.stdout"

按照大神的解答,进行尝试过后,发现行不通,提示如下:

You are required to change your password immediately (password aged)
Last login: Thu Aug 13 11:23:46 2020 from 1*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
passwd: Authentication token manipulation error

认真理解了一番,发现,reponses里面少了一次键入密码的机会,我的环境是通过密码认证登录的 ,如果是使用秘钥登录,即ansible_ssh_common_args 应配置为秘钥方式,更正后如下:

- name: Set password if expired
delegate_to: 127.0.0.1
become: no
expect:
command: ssh {{ ansible_ssh_common_args }} {{ ansible_user }}@{{ inventory_hostname }}
timeout: 10
responses:
"(?i)password:":
# we couldn't keep the same old password
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass }}"
- "{{ ansible_ssh_pass }}"
# if succesfully login then quit
"\\~\\]\\$": exit
register: status
changed_when: "'authentication tokens updated successfully' in status.stdout"

解释:

第一次键入 ansible_ssh_pass_old 为 command ssh 的登录密码。

第二次键入 ansible_ssh_pass_old 为 提示过期输入当前密码。

第三次键入 ansible_ssh_pass 为 新密码。

第四次键入 ansible_ssh_pass 为 确认新密码。

结果测试

ansible-playbook 功能测试结果:

STDOUT:

Warning: Permanently added 'm******o' (ECDSA) to the list of known hosts.
symin@m******o's password:
You are required to change your password immediately (password aged)
Last login: Fri Jul 31 10:46:38 2020 from 1*.*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Connection to m*****o closed.

大功告成。

SSH 提示密码过期,如何通过 ansible 批量更新线上服务器密码的更多相关文章

  1. Ansible 批量管理Windows Server服务器

    Ansible批量管理Windows Server         Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppe ...

  2. ansible批量管理服务 上

    1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...

  3. ansible 批量在远程主机上执行命令

    ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...

  4. Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)

    按照集团运维信息安全制度, 需要每个一段时间对线上服务器密码进行一次变更,通过shell脚本部署比较繁琐,所以决定采用ansible脚本对远程主机root密码进行批量重置,该脚本已经在稳定运行在正式环 ...

  5. mac下连接本地安装的mysql报错提示密码过期

    前提: mac中之前安装了mysql,一段时间没使用,今天使用mysql客户端去连接,报错提示密码过期,原因是mysql5.7之后版本有密码过期这个功能. error: Your password h ...

  6. 如何修改oracle数据库用户密码过期策略

    转至:https://www.cnblogs.com/zhangshuaihui/p/11451590.html 1.   查询数据库用户何时过期 登陆数据库PL/SQL工具,输入以下sql语句: s ...

  7. Python自动批量修改服务器密码

    工作中,我们经常会定期更换服务器密码,如果手动去修改,不仅费时,而且容易出错.下面提供了一种思路,可以实现批量.自动修改服务器密码. 大致思路:首先,为每一台服务器设定一个唯一标识:其次,将每台服务器 ...

  8. Ansible 批量修改密码

    客户要求每3个月修改一次主机密码.密码规则为客服提供的一串字符 xxxx + 主机后3位. 将需要登录主机添加到 Ansible. 将需要登录主机的公钥添加到 known_hosts ssh-keys ...

  9. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

随机推荐

  1. jmeter 录制排除模式

    jmeter录制时,静态的资源不需要,可以在录制的时候直接排除. .*\.(bmp|css|js|gif|icov|jpeg|png|swf|woff|woff2|htm|html).* .*\.(j ...

  2. bzoj4025-二分图【线段树分治,并查集】

    正题 题目链接:https://darkbzoj.tk/problem/4025 题目大意 \(n\)个点\(m\)条边,每条边会在一个\(T\)以内的时间段内出现,对于任意一个\(T\)以内的时刻求 ...

  3. MySQL的本地事务、全局事务、分布式事务

    本地事务 事务特性:ACID,其中C一致性是目的,AID是手段. 实现隔离性 写锁:数据加了写锁,其他事务不能写也不能读. 读锁:数据加了读锁,其他事务不能加写锁可以加读锁,可以允许自己升级为写锁. ...

  4. stm32-HAL使用usart发送中断判断发送标志库问题

    前言: stm32是嵌入式MCU开发中最多应用的芯片,很早之前我们开发ST芯一般都是标准库开发,标准库简洁好读,现在要配合CubeMX生成代码,所以官方主推HAL库和LL库,但是HAL代码冗杂很绕,因 ...

  5. 7.JVM调优-方法区,堆,栈调优详解

    通常我们都知道在堆空间新生代Eden区满了,会触发minor GC, 在老年代满了会触发full GC, 触发full GC会导致Stop The World, 那你们知道还有一个区域满了一会触发Fu ...

  6. Elasticsearch 存储成本省 60%,稿定科技干货分享

    背景 稿定科技旗下稿定设计产品是一个聚焦商业设计的多场景在线设计平台,打破了软硬件间的技术限制,汇集创意内容与设计工具于一体,为不同场景下的设计需求提供优质的解决方案,满足图片.视频等全类型媒介的设计 ...

  7. NOIP模拟77

    前言 感觉最近太飘了,这次考试是挺好的一次打击(好像也不算是). 犯了一个智障错误(双向边一倍数组 100pts->30pts)别的就.. T1 最大或 解题思路 一开始我以为是一个找规律,然而 ...

  8. PAT (Basic Level) Practice (中文)1014 福尔摩斯的约会 (20分)

    1014 福尔摩斯的约会 (20分) 带侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hys ...

  9. Java(44)JDK新特性之函数式接口

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201667.html 博客主页:https://www.cnblogs.com/testero ...

  10. scala基础篇---- Try finally不加catch的使用情形

    普通的try-catch-finally Try{ } catch{//不加catch向上抛出异常 case  _=> } finally{//一般是资源关闭 } 普通的try-finally ...