使用python批量更新xshell登录脚本
xshell 的优点
- 比CRT更简洁好用
- 容易获得免费的个人版,功能完全够用
- 启动速度也更快
- 更新配置的时候可以直接用python读取xsh文件进行批量修改
现在就写一下相关脚本和注意事项
使用顺序
-
要确保xsh文件是utf-16的,因为这就是默认的编码,如果不一致,那么在更新password的时候,会无法使用
-
创建python程序的文件夹目录是
-- session
-- result
-- core.py
-
先用一个xsh登录后,输入密码,更新密码,再提取更新后的密码,作为updat_pwd
-
适用于所有的ssh登陆,password更新的时候记得加入换行符
示例代码如下
# -*- coding: utf-8 -*- #__author__: wellmaster #__date__: 2018/8/28 import os import shutil update_pwd = "Password=DZmB5密码obHAfTPq0/sLqJcE/gq+ljsYZNr+P1bCjNYb7nhFiiWfvqJ\n" judge1 = "Password=" for root, dirs, files in os.walk('Sessions'): os.makedirs('result\%s'%root) for f in files: srcfile = '%s/%s' % (root, f) dstfile = 'result\%s\%s' % (root, f) dsttemp = 'result\%s\%s.temp' % (root, f) if '.xsh' in f: shutil.copyfile(srcfile, dsttemp) print(dsttemp) f1 = open(dsttemp,'r',encoding='utf-16') f2 = open(dstfile,'w',encoding='utf-16') for j in f1: if judge1 in j : j = update_pwd f2.write(j) f1.close() f2.close() os.remove(dsttemp) else: shutil.copyfile(srcfile, dstfile)
如果手贱选错了保存编码,可以用下面的代码进行转换,先转成utf-16的再
"""创建两个文件夹 一个是u8 一个是u16 分别放置不同编码的文件 """ import os for root, dirs, files in os.walk('u8'): os.makedirs('u16\%s' % root) for f in files: if '.xsh' in f: with open('%s\%s'%(root,f),'r',encoding='utf-8') as f1: with open('u16\%s\%s'%(root,f), 'w',encoding='utf-16') as f2: for i in f1: f2.write(i)
更新过编码后,再使用主程序进行更新password