linux在sh脚本ssh,shell脚本实现ssh自动登录功能分享

文件名:ssh_auto_login

#!/usr/bin/expect

##

# ssh模拟登陆器

#

# @author zhiyuan

##

if {$argc<4} {

puts "Error params: $argv"

puts "Expect params :user passwd ip port [translate_id]"

exit 1

}

set default_passcode "这里填通道机的默认密码"

set user [lindex $argv 0]

set password [lindex $argv 1]

set ip [lindex $argv 2]

set port [lindex $argv 3]

set timeout 10

while 1 {

spawn ssh -p $port $user@$ip

#如果最后的字符匹配则执行命令\r结尾表示确定

expect {

"*yes/no" { send "yes\r";exp_continue}

"*password:" { send "$password\r" }

}

#这里是需要通过通道机登陆时的匹配流程,根据需要自行修改。

expect {

"*PASSCODE:" {

send_user "请输入通道机动态密码:";

expect_user -re "(.*)\n"

set random_passcode $expect_out(1,string)

send "$default_passcode$random_passcode\r"

expect {

"Access Denied" { continue }

"Enter:" { send "1\r" }

}

set translate_ip [lindex $argv 4]

if { $translate_ip != "" } {

expect "*):" { send "$translate_ip\r" }

}

}

#"Last login:*" { }

}

break

}

#无法匹配$,还不知道怎么解决

#expect -re "*\$" { puts "test123"; send "source /etc/profile\r" }

#expect "*\$" { send "cd ~\r" }

send_user "login success!"

interact

上边是ssh的自动登录,可以配合下边的shell使用,很方便。

文件名:xxx_launcher

#!/bin/sh

##

# 服务器登陆器

#

# @author zhiyuan

##

channel_user="user_namexxx"

channel_passwd="xxxx"

#内网通道机

internal_ip1=xxx.xxx.xxx.xxx

#联通

unicom_ip1=xxx.xxx.xxx.xxx

#电信

telecom_ip1=xxx.xxx.xxx.xxx

case "$1" in

ci)

expect ssh_auto_login $channel_user $channel_passwd $internal_ip3 22

cl)

expect ssh_auto_login $channel_user $channel_passwd $unicom_ip1 22

cd)

expect ssh_auto_login $channel_user $channel_passwd $telecom_ip1 22

149)

expect ssh_auto_login channel_user channel_passwd xxx.xx.xxx.xxx 22

49)

expect ssh_auto_login $channel_user $channel_passwd $unicom_ip1 22 需要通道机跳转的ipxxx.xxx.xx

*)

echo "帮助信息:"

echo "\tthere is not a server named [$1]"

echo "\t服务器149:\t149"

echo "\t服务器49:\t49"

esac

此时登陆某个服务器的时候就直接 用上述shell带要登录的服务器参数即可,如: ./xxx_launcher 49

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值