python批量修改ssh密码的实现

yipeiwu_com6年前Python基础

由于工作需要本文主结合了excel表格,对表格中的ssh密码进行批量修改

以下是详细代码(python3):

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/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设计】。

相关文章

Python hashlib加密模块常用方法解析

这篇文章主要介绍了Python hashlib加密模块常用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 主要用于对字符串的加...

Python列表推导式的使用方法

1.列表推导式书写形式:   [表达式 for 变量 in 列表]    或者  [表达式 for 变量 in 列表 if 条件] 2.举例说明:...

python实现flappy bird游戏

flappy bird最近火遍大江南北,教你用python写游戏的第一课就向它开刀了。 这个课程的基础是假定你有比较不错的编程功底,对python有一点点的基础。 一、准备工作 1、用p...

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

本文实例讲述了Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 滤镜特效,Marble Filt...

初探利用Python进行图文识别(OCR)

初探利用Python进行图文识别(OCR)

话说什么是OCR????? 简介 OCR技术是光学字符识别的缩写(Optical Character Recognition),是通过扫描等光学输入方式将各种票据、报刊、书籍、文稿及其...