您的位置:首页 > 运维架构 > Linux

Linux远程主机自动登录

2014-03-21 11:25 295 查看
猿类的生活是激情的,码农的工作是苦逼的.
在工作中我们往往会有这种情景,在本地通过secureCRT(估计win下比较多人用它应比putty多)远程登录到linux,然又要通过linux远程登录到其它linux,即是跨主机或者跨网段.

本地系统----------(step1)----------->Linux1---------(step2)---------->Linux2------------(step3)----------->Linux3

1.step1:SecureCRT会帮我们保存密码.它的原理是在本地用户生成一个非对称密钥(一般的RSA算法),然后私钥保存在本地中,把公钥scp到远程的Linux服务器,每次请求的时候带上私钥,Linux服务器则需要用公钥解密,这样实现自动登录.

2.step2:远程Linux登录其它Linux,也可以设置对称密钥实现自动登录,否则我们要登录Linux2,则需在要Linux1的基础数上每次使用ssh命令,并按提示输入密码....

3.step3也是一样.

这里暂时不说公私密钥方式,如果有需要网上比较多教程,如需要大家可移步到此http://keepalived.iteye.com/blog/1181165

这里是使用比较特殊的方式,实际上是将人工输入ssh user@ip 并且将密码通过配置文件自动登录.

1.脚本remote-ssh.sh,自动输入ssh登录命令.

2.脚本remote.sh,自动帮用户输放ssh时提示的yes或登录情况.

3.指定脚本位置.ssh.sh

4.指定ip地址是登录密码,端口文件.

已将细节划分成4个独立的文件.

1.remote-ssh.sh:

#!/usr/bin/expect -f

#从尾随的参数组选取第一个参数作为ip
set ip [lindex $argv 0 ]

#第二个参数作为端口
set port [lindex $argv 1 ]

#第三个参数作为用户名
set user [lindex $argv 2 ]

#第四个参数作为密码
set password [lindex $argv 3 ]

#超时时间(是设置expect函数中的timeout)
set timeout 20

#ssh 登录命令
spawn ssh $user@$ip -p$port

#读取ssh命令后交互的语句,根据语句发送具体的命令
expect {

#超时处理,退出
timeout { puts "Connect timeout, stopping connecting!"; exit 0}
"*yes/no" {
#第一次会遇到yes/no,exp_continue是继续向下处理下一个case
send "yes\r"; exp_continue
}
"*password:" {
send "$password\r"
}
}

#expect eof
interact


2.remote.sh

#!/bin/bash
echo $(pwd)
cd /d "%~dp0"
echo ${pwd}

#循环ip文件函数
function for_in_file(){

#ip-passwd文件
file=$1

#ip-user-passwd分割符号
seperator=$2

#echo $file
#echo $seperator

#判断是否输入ip-passwd文件名
if [ $file ]
then
#echo "Ip-Passwd file is: $file"
#判断ip文件是否存在
if [ ! -f "$file" ]
then
echo "Can't find the file named $file, please check again!"
exit
fi
else
echo "Argument `$file`(file) can't be null"
exit
fi

#判断是否输入文件分隔符
if [ ! $seperator ]
then
echo "Have not been sepecified the file line seperator!"
exit
fi

#定义ip数组
declare -a ips
index=0
#读取ip配置文件
for line in  `cat $file`
do
#echo $line
eval $(
echo $line | awk -F $seperator 'BEGIN{OFS=","}{print "ips[$[index]]="$1}'
)

#echo "index:$index"
#echo "ips:${ips[@]}"

index=$((${index}+1))
done

#插入显示的分割符与ip数组的结果,以供调用返回结果能够分割真实有用的ip数组
echo "$3${ips[@]}"
}

#选择ip函数
function select_ip(){

#remote-ssh.sh
remotessh=$4
#ip数组
declare -a selectMenu
temp=$(echo `for_in_file "$1" "$2" "$3"` | awk -F $3 'BEGIN{OFS=""}{print $2}')
selectMenu=($temp)
#echo "selectMenu:${selectMenu[*]}"

#以下是组装select方法.
scmd='select ip in ${selectMenu[@]};'
scmd=$scmd$'\n do'
scmd=$scmd$'\n case $ip in'

for line in `cat $1`
do
scmd=$scmd'\n'$(echo $line | awk -F $2 'BEGIN{OFS=""}{print $1") echo You choose ip:" $1 ";$remotessh " $1 " "  $2 " "  $3 " '\''" $4 "'\'';;"}')
done

scmd=$scmd'\n*) echo "Error input, choose again with number!";continue;;'
scmd=$scmd$'\n esac;'
scmd=$scmd$'\n break;'
scmd=$scmd$'\n done'
echo -e $scmd
iambeautifulseperatorline="\n\n\n------------------------------I am beautiful seperated line ---------------------------------\n"
while echo -e $iambeautifulseperatorline"Use serial number to choose the ip to ssh connect, eg. 1 or 2 or 3 and so on!"
do
eval $(echo -e $scmd)
done
}

select_ip $1 $2 $3 $4


3.ssh.sh

#!/bin/bash

/opt/${PWD##*/}/remote.sh /opt/${PWD##*}/ip.txt :=: JASIC /opt/${PWD##*/}/remote-ssh.sh


4.ip.txt

192.168.37.161:=:22:=:sxt:=:sxt1234$#@!


其实时间比较紧,写得不好,有点凌乱,如果对大家有用,希望在其上面改得更好用一些.更方便.顺便更熟悉下shell...

代码也以文件形式上传.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: