码迷,mamicode.com
首页 > 编程语言 > 详细

python实现动态更新远程机器列表的SSH登录脚本

时间:2014-11-19 14:05:18      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   os   使用   sp   for   文件   

 

      在公司里, 常常要远程到很多机器上执行命令。机器列表会逐渐增多, 记忆这么多机器的IP或域名显然不是人脑所擅长的。因此, 需要保持一份SSH机器列表,从这些机器列表生成一个用于SSH到机器列表中机器的脚本, 执行该脚本就可以SSH到指定机器上。

      必需文件:  sshlist.txt,  ssh_tpl.sh , updatessh.py  ;  输出文件:  ssh.sh

      SSH 机器列表: sshlist.txt

127.0.0.0.1 ; 本地测试
1.1.1.1 ; 开发环境
2.2.2.2 ; 测试环境

     SSH 脚本模板: ssh_tpl.sh  

     需要以机器列表生成的内容分别替换  ${ChooseList} 和 ${SSHList}

#!/bin/sh
while [ 1 ]
do
echo "Choose host: "
${ChooseList}
read INPUT_VALUE
case "$INPUT_VALUE" in
${SSHList}
    *)  
        echo -e "\033[43;31m invalid params-$INPUT_VALUE \033[0m";
        ;;
esac
done

     生成最终SSH登录脚本的 python 程序: 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"

def readSSHList():
    f = open(sshlistFilename)
    sshMachineList = []
    i = 1
    for line in f:
        sshIp, comment = tuple(line.split(;))
        sshMachineList.append((i, sshIp.strip(), comment.strip()))
        i+=1
    f.close()
    return sshMachineList

def readFile(filename):
    f = open(filename)
    contents = ‘‘
    for line in f:
        contents += line
    f.close()
    return contents

def readSSHTpl():
    return readFile(sshTplFilename)

def main():
    sshMachineList = readSSHList()
    chooseListContents = ‘‘
    sshListContents = ‘‘
    for (i, sshIp, comment) in sshMachineList:
        chooseListContents += "echo \" %d. %s ( %s ) \" \n" % (i, sshIp, comment)
        sshListContents += "\t%d) \n\t\tssh %s@%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()

 

     最终生成的SSH登录脚本: ssh.sh

 

#!/bin/sh
while [ 1 ]
do
echo "Choose host: "
echo " 1. 127.0.0.0.1 ( 本地测试 ) " 
echo " 2. 1.1.1.1 ( 开发环境 ) " 
echo " 3. 2.2.2.2 ( 测试环境 ) " 

read INPUT_VALUE
case "$INPUT_VALUE" in
    1) 
        ssh qin.shuq@127.0.0.0.1
         ;; 
    2) 
        ssh qin.shuq@1.1.1.1
         ;; 
    3) 
        ssh qin.shuq@2.2.2.2
         ;; 

    *)  
        echo -e "\033[43;31m invalid params-$INPUT_VALUE \033[0m";
        ;;
esac
done

 

 

     使用:

      当需要添加新的SSH机器时, 加入到 sshlist.txt , 以分号隔开 ip 地址 和 注释。 然后执行 python updatessh.py 即可生成最终用于登录的 ssh.sh 脚本。

      为了少敲几个字符, 可以做个软连接:  ln -s  ssh.sh  /usr/bin/s 

 

python实现动态更新远程机器列表的SSH登录脚本

标签:style   blog   ar   color   os   使用   sp   for   文件   

原文地址:http://www.cnblogs.com/lovesqcc/p/4108012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!