Linux Shell ssh登录脚本,linuxshellssh登录


  Linux 登陆服务器敲命令太多,某时候确实不便,所以就用shell写了一个

 我的blog地址: http://www.cnblogs.com/caoguo

 

一.说明

  • 支持秘密和密钥两种格式
  • 用户名和密码都是写文件的,明文保存

二.配置

  • 密码文件配置:

序号:IP:端口:用户:密码:说明 1:192.168.88.128:22:root:toor:虚拟机web服务器

  • 密钥文件放在keys文件夹下,密码位置写成密钥文件名,文件名必须以.pem结尾

github地址:https://github.com/ca0gu0/so

脚本下载:git clone git@github.com:ca0gu0/so.git

 

补充:

 

发现没有代码还是不好,把代码还是贴上

so.sh文件的内容

#!/bin/bash


direc=`dirname $0`
function color(){
    blue="\033[0;36m"
    red="\033[0;31m"
    green="\033[0;32m"
    close="\033[m"
    case $1 in
        blue)
            echo -e "$blue $2 $close"
        ;;
        red)
            echo -e "$red $2 $close"
        ;;
        green)
            echo -e "$green $2 $close"
        ;;
        *)
            echo "Input color error!!"
        ;;
    esac
}

function copyright(){
    echo "#####################"
    color blue "   SSH Login Platform   "
    echo "#####################"
    echo
}

function underline(){
    echo "-----------------------------------------"
}

function main(){

while [ True ];do


    echo "序号 |       主机      | 说明"
    underline
    awk 'BEGIN {FS=":"} {printf("\033[0;31m% 3s \033[m | %15s | %s\n",$1,$2,$6)}' $direc/password.lst
    underline
    read -p '[*] 选择主机: ' number
    pw="$direc/password.lst"
    ipaddr=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $2}}' $pw)
    port=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $3}}' $pw)
    username=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $4}}' $pw)
    passwd=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $5}}' $pw)

    case $number in
        [0-9]|[0-9][0-9]|[0-9][0-9][0-9])
            echo $passwd | grep -q ".pem$"
            RETURN=$?
            if [[ $RETURN == 0 ]];then
                ssh -i $direc/keys/$passwd $username@$ipaddr -p $port
                echo "ssh -i $direc/$passwd $username@$ipaddr -p $port"
            else
                expect -f $direc/ssh_login.exp $ipaddr $username $passwd $port
            fi
        ;;
        "q"|"quit")
            exit
        ;;

        *)
            echo "Input error!!"
        ;;
    esac
done
}

copyright
main

 

ssh_login.exp 这个一个expect脚本,用户写交互的工具

#!/usr/bin/expect -f
set TARGET [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWD [lindex $argv 2]
set PORT [lindex $argv 3]
set timeout 10

spawn ssh $USER@$TARGET -p $PORT
expect {
    "*yes/no" {send "yes\r"; exp_continue}
    "*password:" {send "$PASSWD\r"}
}
interact

 

password.lst密码文件格式

1:192.168.88.128:22:root:toor:虚拟机web服务器
2:192.168.88.130:22:ca0gu0:toor:虚拟机mysql数据库服务器
103:192.168.88.4:22:root:sellercube:本地开发服务器

 

 

相关内容

    暂无相关文章