使用python批量更新xshell登錄腳本


xshell 的優點

  1. 比CRT更簡潔好用
  2. 容易獲得免費的個人版,功能完全夠用
  3. 啟動速度也更快
  4. 更新配置的時候可以直接用python讀取xsh文件進行批量修改

 

現在就寫一下相關腳本和注意事項

 

使用順序

  1. 要確保xsh文件是utf-16的,因為這就是默認的編碼,如果不一致,那么在更新password的時候,會無法使用

  2. 創建python程序的文件夾目錄是

    -- session

    -- result

    -- core.py

  3. 先用一個xsh登錄后,輸入密碼,更新密碼,再提取更新后的密碼,作為updat_pwd

  4. 適用於所有的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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM