Python implements an SSH logon script that dynamically updates the remote machine list

Source: Internet
Author: User
Tags readfile

In a company, you often have to run commands remotely to many machines. The list of machines will gradually increase, so it is clear that the IP or domain name of so many machines is not what the human brain excels at. Therefore, it is necessary to maintain a list of SSH machines from which to generate a script for SSH to a machine in the machine list, and execute the script to SSH to the specified machine.

Required documents: Sshlist.txt, ssh_tpl.sh, updatessh.py; Output file: ssh.sh

SSH Machine list: Sshlist.txt

; Local Testing ; Development Environment ; test Environment

SSH Script Template: ssh_tpl.sh

Need to replace ${chooselist} and ${sshlist} with content generated from machine list

#!/bin/SH while[1 ] DoEcho "Choose Host:"${chooselist}read Input_value Case "$INPUT _value" inch${sshlist}*)          Echo-E"\033[43;31m Invalid params-$INPUT _value \033[0m"; ;;Esac Done

Python program that generates the final SSH logon script: updatessh.py

#!/usr/bin/env python#-*-coding:utf-8-*-# -------------------------------------------------------------------------------#Name:updatessh.py#purpose:reads ssh list from Sshlist.txt, replaces contents of ssh_tpl.sh#and finally build a ssh.sh file to SSH specific machines#              ##Author:qin.shuq##Created:#Output:ssh.sh#-------------------------------------------------------------------------------Sshtplfilename="ssh_tpl.sh"Sshlistfilename="Sshlist.txt"Sshscriptfilename="ssh.sh"username="Qin.shuq"defreadsshlist (): F=Open (sshlistfilename) sshmachinelist=[] I= 1 forLineinchF:sship, Comment= Tuple (Line.split (';') ) Sshmachinelist.append ((I, Sship.strip (), Comment.strip () )) I+=1f.close ()returnsshmachinelistdefreadFile (filename): F=open (filename) Contents="'     forLineinchf:contents+=Line f.close ()returnContentsdefreadsshtpl ():returnreadFile (sshtplfilename)defMain (): Sshmachinelist=readsshlist () chooselistcontents="'sshlistcontents="'     for(I, sship, comment)inchsshmachinelist:chooselistcontents+="echo \ "%d.%s (%s) \" \ n"%(i, sship, comment) sshlistcontents+="\t%d) \n\t\tssh%[email protected]%s\n \t\t; \ n"%(i, username, sship) sshtplcontents=readsshtpl () sshtplcontents= Sshtplcontents.replace ("${chooselist}", chooselistcontents). Replace ("${sshlist}", sshlistcontents) sshscriptfile= Open (Sshscriptfilename,'W') Sshscriptfile.write (sshtplcontents) sshscriptfile.close ()if __name__=='__main__': Main ()

the resulting SSH logon script: ssh.sh

#!/bin/SH while[1 ] DoEcho "Choose Host:"Echo "1.127.0.0.0.1 (local test)" Echo "2.1.1.1.1 (development environment)" Echo "3.2.2.2.2 (test environment)"Read Input_value Case "$INPUT _value" inch    1)         SSH[Email protected]127.0.0.0.1         ;; 2)         SSH[Email protected]1.1.1.1         ;; 3)         SSH[Email protected]2.2.2.2         ;; *)          Echo-E"\033[43;31m Invalid params-$INPUT _value \033[0m"; ;;Esac Done

use:

When a new SSH machine needs to be added, add to sshlist.txt, separating IP addresses and annotations with semicolons. Then execute the python updatessh.py to generate the ssh.sh script that will eventually be used for the login.

In order to knock a few characters, you can make a soft connection: Ln-s SSH.SH/USR/BIN/S

Python implements an SSH logon script that dynamically updates the remote machine list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.