Knowledge point 031 - batch installation of. xsh files by using WIN version of python

Keywords: ssh SHA1 Python Windows

1. Baidu goes to python's official website, downloads the software selected in the figure above, creates a new folder xshell in the local disk D of the computer, enters the xshell folder, creates a new IP.txt file, and inputs 6 servers you want to log in to

10.0.84.151
10.0.3.127
10.0.3.128
10.0.3.129
10.0.3.130
10.0.3.131

2. Open xshell, click New in file, enter IP address in host option, right-click 10.0.3.127 in the figure, select the lowest properties, and select logging to log in. You can record the history of logged in operation commands

3. click the red box in the figure to display the page and copy the address on the top right

4. Edit script and. PY script in windows

#! 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:\\Your computer path\\%n_%d%t.log   ####Flexible to change
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('Establish{} fail!!!!!! '.format(host_ip),e)
        else:
            print('Establish{} Success!'.format(host_ip))
				
                
if __name__ == '__main__':
    xshell_session_path = 'D:\\xshell\\Experimental machine'   ###Local path
    ip_file_path = 'D:\\xshell\\IP.txt'  ###File for local IP address
    username = 's-linuxad'    ###User name
    pwd = '/aHCCaX/zEY='     ###pwd encrypted password 
    main(xshell_session_path, ip_file_path, pwd, username)

5. Press and hold shift + right button in the blank space of the folder to find the powershell window to open here. Enter the file name at the end of. py newly created in D disk in the page, and the operation will be successful

6. Copy the local. xsh of the computer to the bastion machine, and you can open the machine you want to log in with a keyboard in the bastion machine

Posted by rhosk on Thu, 02 Apr 2020 02:04:28 -0700