python批量修改ssh密码
由于工作需要本文主结合了excel表格,对表格中的ssh密码进行批量修改
以下是详细代码(python3):
#!/usr/bin/env python
#-*-coding:utf-8-*- import paramiko
import socket
import pandas as pd def demo(Ip,user,old_password,new_password):
# 建立一个sshclient对象
ssh = paramiko.SSHClient()
# 允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 调用connect方法连接服务器
#如果远程执行命令错误信息是b'the input device is not a TTY\n' 去掉docker exec -it 中的t就好了
try:
ssh.connect(hostname=Ip, port=22, username=user, password=old_password,timeout=5)
#ubuntu修改密码两种方法
#方法一
# command1 = "echo '%s:%s' | chpasswd"%(user,new_password)
# stdin, stdout, stderr = ssh.exec_command(command1)
# out, err = stdout.read(), stderr.read()
# if err != '':
# print(err)
#
# else:
# print(out)
# # 关闭连接
# ssh.close()
#方法二
command = "passwd %s" %(user)
stdin, stdout, stderr = ssh.exec_command(command)
#\n模拟回车 输两次密码
stdin.write(new_password + '\n' + new_password + '\n')
out, err = stdout.read(), stderr.read()
successful = 'password updated successfully'
#print(out,err)
if successful in str(err):
print(Ip + " 密码修改成功!")
else:
print('\033[31m错误:\033[0m' + str(err))
print(Ip + " 密码修改失败!")
# 关闭连接
ssh.close()
except paramiko.ssh_exception.AuthenticationException as e:
print(Ip + ' ' + '\033[31m账号密码错误!\033[0m')
with open('nossh.txt','a') as f:
f.write(Ip + '\n')
except socket.timeout as e:
print(Ip + ' ' + '\033[31m连接超时!\033[0m')
with open('timeoutssh','a') as f:
f.write(Ip + '\n')
def Red_Excel(IP):
import sys
import time
file = r'E:\xxx.xlsx'
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', 1000)
n = pd.read_excel(file,sheet_name='xxx') #表格中的sheet名
#print(n.values)
#显示含某字段的特定行
n1 = (n.loc[n['IP']==IP])
if not n1.empty:
n2 = n1.values
ip = n2[0][1]
user = n2[0][4]
password_old = n2[0][5]
password_new = n2[0][22]
houtai = n2[0][16]
print('IP:%s 账号:%s 旧密码:%s 是否后台:%s 新密码:%s' % (ip, user, password_old, houtai,password_new))
demo(ip,user,password_old,password_new)
else:
print('记录表无此IP!')
if __name__ == "__main__":
with open('ip.txt') as f:
for i in f:
ip = i.split('\n')[0]
Red_Excel(ip) 此代码可以适当修改,进行单独的ssh密码修改。
python批量修改ssh密码的更多相关文章
- saltstack+python批量修改服务器密码
saltstack安装:略过 python脚本修改密码: # -*- coding utf-8 -*- import socket import re import os import sys imp ...
- Python 批量修改root密码
#_*_coding:utf8_*_ from multiprocessing import Process, Pool import paramiko import sys,os host_list ...
- python批量修改linux主机密码
+++++++++++++++++++++++++++++++++++++++++++标题:python批量修改Linux服务器密码时间:2019年2月24日内容:基于python实现批量修改linu ...
- Python脚本批量修改服务器密码
搭建环境 centos 7.4 使用脚本 python 批量修改connect用户的密码 生成密码为随机密码 保存为xls文档 passwd_chang #!/usr/bin/env python ...
- 批量修改Linux密码脚本(Python)
搭建环境 centos 7.4 使用脚本 python 批量修改connect用户的密码 生成密码为随机密码 保存为xls文档 #!/usr/bin/env python # -*- coding: ...
- ansible非root用户批量修改root密码
前言: 由于线上服务器密码长久没有更新,现领导要求批量更换密码.线上的之前部署过salt,但由于各种因素没有正常使用. 使用自动化工具批量修改的计划搁浅了,后来领导给了个python多线程修改密码脚本 ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- Python批量修改图片格式和尺寸
Python批量修改图片格式和尺寸 备注: 1.导入了PIL库,是处理图片用的,很强大; 2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除. 3.导入 ...
- (转)linux passwd批量修改用户密码
linux passwd批量修改用户密码 原文:http://blog.csdn.net/xuwuhao/article/details/46618913 对系统定期修改密码是一个很重要的安全常识, ...
随机推荐
- vue视频: 自定义指令 && 拖拽 && 自定义键盘信息
v-textv-forv-html 指令: 扩展html语法 自定义指令:1. 自定义属性指令: Vue.directive(指令名称,function(参数){ this.el -> 原生DO ...
- [NGINX] - 配置文件优化 - NGINX.CONF
Nginx 本文主要针对公司的Nginx负载均衡配置进行解释,配置文件在最下方.因为公司没有使用PHP,所以NGINX里面并没有太多facgi模块相关优化 NGINX.CONF user 语 ...
- 徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】
After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a ...
- zabbix debug and vulnerability https://www.zabbix.com/documentation/3.0/manual/concepts/sender
https://www.zabbix.com/documentation/3.0/manual/concepts/sender zabbix--- zabbix_sender -vv -z 172.2 ...
- HTTPS握手
作用 内容加密 建立一个信息安全通道,来保证数据传输的安全: 身份认证 确认网站的真实性 数据完整性 防止内容被第三方冒充或者篡改 https的采用了对称加密和非对称加密.握手过程中采用非对称加密,得 ...
- python中读取配置文件的方式
方式1:argparse argparse,是Python标准库中推荐使用的编写命令行程序的工具.也可以用于读取配置文件. 字典样式的配置文件*.conf 配置文件test1.conf { " ...
- MySQL 的mysqldump备份
MySQL 的mysqldump备份 来自<mysql技术内幕 innodb存储引擎> --single-transaction:只对innodb表有效 --lock-tables:对My ...
- ReactNative 环境配置
一直是从事iOS的开发,现在研究下mac环境下reatNative的环境配置: 1. 安装HomeBlew(OS系统上的一个安装包管理器,安装后可以方便后续安装包的安装.) 终端命令: ruby -e ...
- android ReactNative之Cannot find entry file index.android.js in any of the roots
android ReactNative之Cannot find entry file index.android.js in any of the roots 2018年04月02日 14:53:12 ...
- 通过IP地址和子网掩码与运算计算相关地址
通过IP地址和子网掩码与运算计算相关地址 知道IP地址和子网掩码后可以算出 网络地址 广播地址 地址范围 本网有几台主机 例一:下面例子IP地址为192.168.100.5 子网掩码是255.255. ...