python 跳板机登录脚本


这里有个功能没能实现,就是让人员自己输入密码,交互式的。传入密码进来,而不是在这里定义好。
还望指点。想在python这里实现交互让用户输入MySQL的密码
 
 
 
passwd = '123.com'
logou_flag = False
for i in range(3):
    user_input = raw_input("Please input your passwd:").strip()
    if len(user_input) ==0:continue
    if user_input == passwd:
        while True:
            print "welcome login!"
            user_choice = raw_input("""
            1.hadoop-2
            2.hadoop-3
            3.exit
            """).strip()
            user_choice = int(user_choice)
            if user_choice == 1:
                print "ssh user@hadoop-2"
            if user_choice == 2:
                print "ssh user@hadoop-3"
            if user_choice == 3:
                logou_flag = True
                break
    if logou_flag:
        print "going to logout"
        break
    print "-----going to do something else....."

 

 
 
用shell方式实现:
 
[root@hz-bf-02 ~]# cat /mnt/shell/denglv.sh 
#! /bin/bash
#hexudong  345078833
function trapper(){
        trap '' INT QUIT TSTP TERM HUP  
}
function menu(){
        cat <<-EOF
==============Host List================
        1)hz-mgdb-01
        2)hz-mgdb-02
        3)hz-rds-01
        4)hz-rds-02
        5)hz-web-01--die
        6)hz-web-02--die
        7)mysql
        9)hz-web-03
       10)hz-web-04
        8)exit
=======================================
        EOF
}
function host(){
        case "$1" in
          1)
            ssh $USER@hz-mgdb-01
            ;;
          2)
            ssh $USER@hz-mgdb-02
            ;;
          3)
            ssh $USER@hz-rds-01
            ;;
          4)
            ssh $USER@hz-rds-02
            ;;
          5)
            ssh $USER@hz-web-01
            ;;
          6)
            ssh $USER@hz-web-02
            ;;
          7)
            mysql -h 账户 -u 用户名-p$2
            ;;
          9)
            ssh $USER@hz-web-03
            ;;
          10)
            ssh $USER@hz-web-04
            ;;
          8|*)
            exit
        esac
}
function main(){
        while true
          do
            trapper
            clear
            menu
            read -p "please select:" num passwd
            host $num $passwd
        done
}
main

 

 
现在使用的python,还有待完善,欢迎帮我完善
 
[root@hz-bf-02 ~]# cat /mnt/shell/denglv.py 
#!/usr/bin/evn python
import os
passwd = '123.com'
logou_flag = False
for i in range(3):
    user_input = raw_input("""Please input your passwd:
No password,Please quit!!!
Or Looking for xudong   :""").strip()
    if len(user_input) ==0:continue
    if user_input == passwd:
        while True:
            print "welcome login!"
            user_choice = raw_input("""
                1.hz-mgdb-01
                2.hz-mgdb-02
                3.hz-rds-01
                4.hz-rds-02
                5.hz-web-03
                6.hz-web-04
                7.mysql
                8.exit
            """).strip()
            user_choice = int(user_choice)
            if user_choice == 1:
                os.system('ssh root@hz-mgdb-01')
            if user_choice == 2:
                os.system('ssh root@hz-mgdb-02')
            if user_choice == 3:
                os.system('ssh root@hz-rds-01')
            if user_choice == 4:
                os.system('ssh root@hz-rds-02')
            if user_choice == 5:
                os.system('ssh root@hz-web-03')
            if user_choice == 6:
                os.system('ssh root@hz-web-04')
            if user_choice == 7:
                os.system('mysql -h"保密" -u"保密" -p -A')
            if user_choice == 8:
                logou_flag = True
                break
    if logou_flag:
        print "going to logout"
        break
    print "-----going to do something else....."

 


评论关闭