利用python批量创建.xsh文件实现Xshell批量登录服务器

 

  1. 实现环境:win10+Xshell6+python3  这些都必须提前装好
  2. 文件路径如下:

     

  3. 首先新建一个会话,并且输入用户名和密码,然后连接会话,登录成功后就可以退出,在Session这个目录中就会出现一个.xsh文件,用记事本打开这个文件,里面password字段对应的就是加密后的密码.

  4. 在Session目录中创建一个IP.txt文件,这个文件用来保存需要批量登录的服务器的地址,一个ip地址一行.
  5. 在任意目录下创建python脚本文件,随意命名,脚本内容如下:
    #! conding: utf8
    
    import os
    
    config_file = '''
    [CONNECTION:PROXY]
    Proxy=
    StartUp=0
    [CONNECTION:SERIAL]
    BaudRate=6
    StopBits=0
    FlowCtrl=0
    Parity=0
    DataBits=3
    ComPort=0
    [SessionInfo]
    Version=6.0
    Description=Xshell session file
    [TRACE]
    SockConn=1
    SshLogin=0
    SshTunneling=0
    SshPacket=0
    TelnetOptNego=0
    [CONNECTION:SSH]
    KeyExchange=
    SSHCiphers=chacha20-poly1305@openssh.com:1,aes128-ctr:1,aes192-ctr:1,aes256-ctr:1,aes128-gcm@openssh.com:1,aes256-gcm@openssh.com:1,aes128-cbc:1,aes192-cbc:1,aes256-cbc:1,3des-cbc:1,blowfish-cbc:1,cast128-cbc:1,arcfour:1,rijndael128-cbc:1,rijndael192-cbc:1,rijndael256-cbc:1,rijndael-cbc@lysator.liu.se:1,arcfour128:1,arcfour256:1
    AgentForwarding=0
    ForwardToXmanager=1
    Compression=0
    NoTerminal=0
    UseAuthAgent=0
    MAC=
    SSHMACs=hmac-sha2-256-etm@openssh.com:1,hmac-sha2-512-etm@openssh.com:1,hmac-sha1-etm@openssh.com:1,hmac-sha2-256:1,hmac-sha2-512:1,hmac-sha1:1,hmac-sha1-96:1,hmac-md5:1,hmac-md5-96:1,hmac-ripemd160:1,hmac-ripemd160@openssh.com:1,umac-64@openssh.com:1,umac-128@openssh.com:1,hmac-sha1-96-etm@openssh.com:1,hmac-md5-etm@openssh.com:1,hmac-md5-96-etm@openssh.com:1,umac-64-etm@openssh.com:1,umac-128-etm@openssh.com:1
    InitRemoteDirectory=
    ForwardX11=1
    VexMode=0
    Cipher=
    Display=localhost:0.0
    FwdReqCount=0
    InitLocalDirectory=
    SSHKeyExchanges=curve25519-sha256@libssh.org:1,ecdh-sha2-nistp256:1,ecdh-sha2-nistp384:1,ecdh-sha2-nistp521:1,diffie-hellman-group-exchange-sha256:1,diffie-hellman-group-exchange-sha1:1,diffie-hellman-group18-sha512:1,diffie-hellman-group16-sha512:1,diffie-hellman-group14-sha256:1,diffie-hellman-group14-sha1:1,diffie-hellman-group1-sha1:1
    RemoteCommand=
    SaveHostKey=0
    [BELL]
    FilePath=
    RepeatTime=3
    FlashWindow=0
    BellMode=1
    IgnoreTime=3
    [USERINTERFACE]
    NoQuickButton=0
    QuickCommand=
    [CONNECTION:FTP]
    Passive=1
    InitRemoteDirectory=
    InitLocalDirectory=
    [TRANSFER]
    FolderMethod=0
    DropXferHandler=2
    XmodemUploadCmd=rx
    ZmodemUploadCmd=rz -E
    FolderPath=
    YmodemUploadCmd=rb -E
    AutoZmodem=1
    SendFolderPath=
    DuplMethod=0
    XYMODEM_1K=0
    [CONNECTION]
    Port=22
    Host={host_ip}
    Protocol=SSH
    AutoReconnect=0
    AutoReconnectLimit=0
    Description=
    AutoReconnectInterval=30
    FtpPort=21
    UseNaglesAlgorithm=0
    IPV=0
    [TERMINAL]
    Rows=24
    CtrlAltIsAltGr=1
    InitOriginMode=0
    InitReverseMode=0
    DisableBlinkingText=0
    CodePage=65001
    InitAutoWrapMode=1
    Cols=80
    InitEchoMode=0
    Type=xterm
    DisableAlternateScreen=0
    CJKAmbiAsWide=0
    ScrollBottomOnKeyPress=0
    DisableTitleChange=0
    ForceEraseOnDEL=0
    InitInsertMode=0
    ShiftForcesLocalUseOfMouse=1
    FontLineCharacter=1
    ScrollbackSize=1024
    InitCursorMode=0
    FixedCols=0
    BackspaceSends=2
    UseInitSize=0
    UseLAltAsMeta=0
    UseRAltAsMeta=0
    AltKeyMapPath=
    DeleteSends=0
    DisableTermPrinting=0
    IgnoreResizeRequest=1
    ScrollBottomOnTermOutput=1
    FontPowerLine=1
    ScrollErasedText=1
    KeyMap=0
    RecvLLAsCRLF=0
    EraseWithBackgroundColor=1
    InitNewlineMode=0
    InitKeypadMode=0
    TerminalNameForEcho=Xshell
    [TERMINAL:WINDOW]
    ColorScheme=XTerm
    FontQuality=0
    LineSpace=0
    CursorColor=65280
    CursorBlinkInterval=600
    TabColorType=0
    CursorAppearance=0
    TabColorOther=0
    FontSize=14
    AsianFontSize=14
    CursorBlink=0
    BGImageFile=
    BoldMethod=2
    CursorTextColor=0
    BGImagePos=0
    AsianFont=DejaVu Sans Mono
    FontFace=DejaVu Sans Mono
    CharSpace=0
    MarginBottom=5
    MarginLeft=5
    MarginTop=5
    MarginRight=5
    [CONNECTION:TELNET]
    XdispLoc=1
    NegoMode=0
    Display=$PCADDR:0.0
    [HIGHLIGHT]
    HighlightSet=None
    [CONNECTION:AUTHENTICATION]
    Pkcs11Pin=
    Library=0
    Passphrase=
    Pkcs11Middleware=
    Delegation=0
    UseInitScript=0
    TelnetLoginPrompt=ogin:
    Password={pwd}
    RloginPasswordPrompt=assword:
    UseExpectSend=0
    TelnetPasswordPrompt=assword:
    ExpectSend_Count=0
    Method=0
    ScriptPath=
    UserKey=
    UserName={username}
    [LOGGING]
    FilePath=%n_%Y-%m-%d_%t.log
    Overwrite=1
    WriteFileTimestamp=0
    Encoding=2
    TimestampFormat=[%a] 
    TermCode=0
    AutoStart=0
    Prompt=0
    WriteTermTimestamp=0
    [ADVANCED]
    WaitPrompt=
    PromptMax=0
    SendLineDelayType=0
    SendLineDelayInterval=0
    SendCharDelayInterval=0
    [CONNECTION:RLOGIN]
    TermSpeed=38400
    [CONNECTION:KEEPALIVE]
    SendKeepAliveInterval=60
    KeepAliveInterval=60
    TCPKeepAlive=0
    KeepAliveString=
    SendKeepAlive=0
    KeepAlive=1
    '''
    
    
    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 = 'E:\\Xshell\\Xshell\\Sessions'  ###本地的路径
        ip_file_path = 'E:\\Xshell\\Xshell\\Sessions\\IP.txt'  ###本地的IP地址的文件
        username = 'root'  ###用户名
        pwd = 'TN+3k9UAvLFfghR5tNV3K9DKhmwArV+SbjWAcgZZzIDTnJ0JgCo='  ###pwd 加密后的密码.加密后的密码可以自己先ssh登录一台服务器
        # ,然后保存连接,在生成的.xsh文件里复制出来
        main(xshell_session_path, ip_file_path, pwd, username)

    Session目录的路径根据你实际路径,执行这个python脚本,就会批量生成.xsh文件,这样就能批量登录服务器了.

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值