发布时间: 2023-4-2 文章作者: myluzh 分类名称: Python 朗读文章
import paramiko import logging import os class AD: def __init__(self): self.ad = "jsjd.local" def modifySamA(self, new_jsid, old_jsid, name): logger.info(f"ModifySamA,current:{new_jsid},{old_jsid},{name}") try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ad = "jsjd.local" client.connect('10.1.26.21', 22, username='administrator', password='Aa88776655', timeout=5) logger.info(f"ADserver {self.ad} 连接成功") except: logger.error(f"ADserver {self.ad} 连接失败") exit() c = f"powershell.exe \"Set-ADUser \'{old_jsid}\' -UserPrincipalName \'{new_jsid}@{self.ad}\' -SamAccountName \'{new_jsid}\'\" && echo success" stdin, stdout, stderr = client.exec_command(c) info = stdout.read().decode('gbk') + stderr.read().decode('gbk') info = info.replace("\n", "").replace("\r", "").replace(" ", "") if "success" in info: logger.info(f"成功:已修改{old_jsid}对象为{new_jsid}@{self.ad}") elif "无法" in info and "找到" in info: logger.warning(f"失败:无法找到{name},{old_jsid}对象。") else: logger.warning(f"失败:{info}") client.close() if __name__ == "__main__": # 格式化logging日志模块 logger = logging.getLogger('rplog') logger.setLevel(level=logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') file_handler = logging.FileHandler(os.path.basename(__file__).split(".")[0] + "_running.log") file_handler.setLevel(level=logging.INFO) file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.DEBUG) stream_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(stream_handler) #开始 logger.info(f"脚本开始") AD = AD() ad = open("人员.csv") # 新工号,旧工号,名字 # AD.modifySamA("10003105", "JS106931", "鲁籽杭") for a in ad.readlines(): a = a.replace("\n", "").replace("\r", "").replace(" ", "") a = a.split(',') AD.modifySamA(a[0], a[1], a[2]) logger.info("结束") exit()
发表评论