python批量修改ssh密碼的實(shí)現(xiàn)
由于工作需要本文主結(jié)合了excel表格,對(duì)表格中的ssh密碼進(jìn)行批量修改
以下是詳細(xì)代碼(python3):
'''
遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:857662006
尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書!
'''
#!/usr/bin/env python
#-*-coding:utf-8-*-
import paramiko
import socket
import pandas as pd
def demo(Ip,user,old_password,new_password):
# 建立一個(gè)sshclient對(duì)象
ssh = paramiko.SSHClient()
# 允許將信任的主機(jī)自動(dòng)加入到host_allow 列表,此方法必須放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 調(diào)用connect方法連接服務(wù)器
#如果遠(yuǎn)程執(zhí)行命令錯(cuò)誤信息是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)
# # 關(guān)閉連接
# 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錯(cuò)誤:\033[0m' + str(err))
print(Ip + " 密碼修改失??!")
# 關(guān)閉連接
ssh.close()
except paramiko.ssh_exception.AuthenticationException as e:
print(Ip + ' ' + '\033[31m賬號(hào)密碼錯(cuò)誤!\033[0m')
with open('nossh.txt','a') as f:
f.write(Ip + '\n')
except socket.timeout as e:
print(Ip + ' ' + '\033[31m連接超時(shí)!\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 賬號(hào):%s 舊密碼:%s 是否后臺(tái):%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)
此代碼可以適當(dāng)修改,進(jìn)行單獨(dú)的ssh密碼修改。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算
按位取反是位運(yùn)算符,而位運(yùn)算符是應(yīng)用在兩個(gè)數(shù)的運(yùn)算上,會(huì)對(duì)數(shù)字的二進(jìn)制所有位數(shù)進(jìn)行從低到高的運(yùn)算,下面這篇文章主要給大家介紹了關(guān)于Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算的相關(guān)資料,需要的朋友可以參考下2023-02-02
Python實(shí)現(xiàn)PPT/PPTX批量轉(zhuǎn)換成PDF
這篇文章主要為大家詳細(xì)介紹了如何使用Python將PowerPoint演示文稿(PPT、PPTX等)轉(zhuǎn)換為PDF文件,使演示內(nèi)容能夠在更多的設(shè)備上展示,感興趣的小伙伴可以了解下2024-01-01
python opencv圖片編碼為h264文件的實(shí)例
今天小編就為大家分享一篇python opencv圖片編碼為h264文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Pycharm創(chuàng)建項(xiàng)目時(shí)如何自動(dòng)添加頭部信息
這篇文章主要介紹了Pycharm創(chuàng)建項(xiàng)目時(shí) 自動(dòng)添加頭部信息,需要的朋友可以參考下2019-11-11
Python 隱藏輸入密碼時(shí)屏幕回顯的實(shí)例
今天小編就為大家分享一篇Python 隱藏輸入密碼時(shí)屏幕回顯的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
django應(yīng)用JWT(JSON?Web?Token)實(shí)戰(zhàn)教程
在前后端分離的項(xiàng)目中,JWT(JSON?Web?Token)作為一種廣泛使用的身份驗(yàn)證和授權(quán)機(jī)制,提供了一種安全、高效的方式來保護(hù)RESTful?API,本文詳細(xì)介紹了JWT的概念、優(yōu)勢(shì)、在Django中的應(yīng)用步驟和使用方法,是構(gòu)建安全、高效Web應(yīng)用的有效指南2024-10-10
Python?BeautifulSoup4實(shí)現(xiàn)數(shù)據(jù)解析與提取
Beautiful?Soup是一個(gè)Python的庫,用于解析HTML和XML文檔,提供了方便的數(shù)據(jù)提取和操作功能,下面小編就來和大家詳細(xì)聊聊如何利用BeautifulSoup4實(shí)現(xiàn)數(shù)據(jù)解析與提取吧2023-10-10

