知識點031-批量利用python的WIN版本建立.xsh文件批量登陸

1.百度去python的官網, 下載上圖選中軟件,而後在計算機本地磁盤D中新建文件夾 xshell ,進入xshell文件夾新建IP.txt文件,輸入6臺你想登錄的服務器python

10.0.84.151
10.0.3.127
10.0.3.128
10.0.3.129
10.0.3.130
10.0.3.131linux

2.打開xshell, 點擊file 中的新建,而後在host選項中輸入IP地址,而後選中圖中的10.0.3.127 右鍵,選擇最下面的properties ,選擇logging,進行登錄的操做,能夠記錄登錄的操做命令歷史記錄shell

3.點擊圖中的紅框框,出現頁面,把右上方的地址複製好        windows

4.在windows 中編輯腳本,編輯.PY 腳本服務器

#! conding: utf8

import os

config_file='''
[CONNECTION:SERIAL]
Parity=0
StopBits=0
ComPort=0
BaudRate=6
FlowCtrl=0
DataBits=3
[CONNECTION]
Port=22
Protocol=SSH
AutoReconnect=0
TCPKeepAlive=0
KeepAliveInterval=60
AutoReconnectInterval=30
Host={host_ip}
KeepAlive=1
AutoReconnectLimit=0
[Information]
Description=Xshell Session Profile
MinorVersion=0
MajorVersion=3
[CONNECTION:AUTHENTICATION]
TelnetLoginPrompt=ogin:
TelnetPasswordPrompt=assword:
ScriptPath=
UseExpectSend=0
UserName={username}
UserKey=
ExpectSend_Count=0
Password={pwd}
Passphrase=
UseInitScript=0
Method=0
RloginPasswordPrompt=assword:
[CONNECTION:TELNET]
NegoMode=0
Display=$PCADDR:0.0
XdispLoc=1
[USERINTERFACE]
NoQuickButton=0
ShowOnLinkBar=0
QuickCommand=
[CONNECTION:SSH]
ForwardX11=1
VexMode=0
LaunchAuthAgent=1
InitLocalDirectory=
MAC=
UseAuthAgent=0
Compression=0
Cipher=
Display=localhost:0.0
InitRemoteDirectory=
ForwardToXmanager=1
FwdReqCount=0
NoTerminal=0
CipherList=aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael128-cbc,rijndael192-cbc,rijndael256-cbc,aes256-ctr,aes192-ctr,aes128-ctr,rijndael-cbc@lysator.liu.se
MACList=hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-ripemd160,hmac-ripemd160@openssh.com
[TERMINAL]
ScrollErasedText=1
DisableTitleChange=0
IgnoreResizeRequest=0
UseInitSize=0
DisableBlinkingText=0
ShiftForcesLocalUseOfMouse=1
ForceEraseOnDEL=0
KeyMap=0
InitReverseMode=0
DeleteSends=0
BackspaceSends=2
UseAltAsMeta=0
InitKeypadMode=0
InitCursorMode=0
AltKeyMapPath=
CtrlAltIsAltGr=1
DisableTermPrinting=0
ScrollBottomOnKeyPress=0
InitInsertMode=0
Type=xterm
Rows=24
CodePage=65001
ScrollbackSize=1024
InitNewlineMode=0
InitEchoMode=0
CJKAmbiAsWide=1
InitOriginMode=0
DisableAlternateScreen=0
ScrollBottomOnTermOutput=1
InitAutoWrapMode=1
RecvLLAsCRLF=0
Cols=80
EraseWithBackgroundColor=1
[LOGGING]
AutoStart=1
Type=0
FilePath=C:\\你的計算機路徑\\%n_%d%t.log   ####能夠靈活改變
Overwrite=1
FileMethod=1
[CONNECTION:FTP]
Passive=1
InitLocalDirectory=
InitRemoteDirectory=
[CONNECTION:PROXY]
StartUp=0
Proxy=
[TERMINAL:WINDOW]
FontSize=12
CharSpace=0
CursorBlink=0
LineSpace=0
FontFace=Courier New
BoldMethod=2
ColorScheme=White on Black
CursorAppearance=0
MarginLeft=2
CursorTextColor=0
MarginBottom=2
MarginTop=2
MarginRight=2
CursorColor=65280
CursorBlinkInterval=600
[TRANSFER]
FolderMethod=0
SendFolderPath=
FolderPath=
DuplMethod=0
AutoZmodem=1
[CONNECTION:RLOGIN]
TermSpeed=38400
[TRACE]
SshTunneling=0
SshLogin=0
SockConn=1
TelnetOptNego=0
'''

def getIP(ip_file_path):
	with open(ip_file_path, 'r') as f:
		host_ip = f.readlines()
		host_ip = list(map(lambda x: x.strip(), host_ip))
	return host_ip
				

def main(xshell_session_path, ip_file_path, pwd, username):
    ip_list = getIP(ip_file_path)
    
    for host_ip in ip_list:
        try:
            xsh_file = os.path.join(xshell_session_path, host_ip) + '.xsh'
            with open(xsh_file, 'w') as f:
                f.write(config_file.format(host_ip=host_ip, pwd=pwd, username=username))
        except Exception as e:
            print('建立{} 失敗!!!!!! '.format(host_ip),e)
        else:
            print('建立{} 成功!'.format(host_ip))
				
                
if __name__ == '__main__':
    xshell_session_path = 'D:\\xshell\\實驗機'   ###本地的路徑
    ip_file_path = 'D:\\xshell\\IP.txt'  ###本地的IP地址的文件
    username = 's-linuxad'    ###用戶名
    pwd = '/aHCCaX/zEY='     ###pwd 加密後的密碼 
    main(xshell_session_path, ip_file_path, pwd, username)

5.在文件夾的空白處按住shift + 右鍵 找到在此處打開powershell 窗口 ,在頁面中輸入D盤中新建的.py結尾的文件名, 就能夠成功操做 session

6.將計算機本地的.xsh 複製到堡壘機中, 就能夠在堡壘機中一鍵盤打開想要登錄的機器 ssh

相關文章
相關標籤/搜索