shell腳本實(shí)現(xiàn)ssh自動(dòng)登錄功能分享
更新時(shí)間:2014年04月23日 10:55:14 作者:
mac下沒有找到好用的類似secureCRT,就自己寫了個(gè)自動(dòng)登錄的腳本,分享一下,如果是新浪的,就基本不用修改代碼就直接能用
文件名:ssh_auto_login
復(fù)制代碼 代碼如下:
#!/usr/bin/expect
##
# ssh模擬登陸器
#
# @author zhiyuan <hzyhouzhiyuan艾特gmail.com>
##
if {$argc<4} {
puts "Error params: $argv"
puts "Expect params :user passwd ip port [translate_id]"
exit 1
}
set default_passcode "這里填通道機(jī)的默認(rèn)密碼"
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
#如果最后的字符匹配則執(zhí)行命令\r結(jié)尾表示確定
expect {
"*yes/no" { send "yes\r";exp_continue}
"*password:" { send "$password\r" }
}
#這里是需要通過通道機(jī)登陸時(shí)的匹配流程,根據(jù)需要自行修改。
expect {
"*PASSCODE:" {
send_user "請輸入通道機(jī)動(dòng)態(tài)密碼:";
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的自動(dòng)登錄,可以配合下邊的shell使用,很方便。
文件名:xxx_launcher
復(fù)制代碼 代碼如下:
#!/bin/sh
##
# 服務(wù)器登陸器
#
# @author zhiyuan <hzyhouzhiyuan@gmail.com>
##
channel_user="user_namexxx"
channel_passwd="xxxx"
#內(nèi)網(wǎng)通道機(jī)
internal_ip1=xxx.xxx.xxx.xxx
#聯(lián)通
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 需要通道機(jī)跳轉(zhuǎn)的ipxxx.xxx.xx
*)
echo "幫助信息:"
echo "\tthere is not a server named [$1]"
echo "\t服務(wù)器149:\t149"
echo "\t服務(wù)器49:\t49"
esac
此時(shí)登陸某個(gè)服務(wù)器的時(shí)候就直接 用上述shell帶要登錄的服務(wù)器參數(shù)即可,如: ./xxx_launcher 49
相關(guān)文章
如何解決jenkins 執(zhí)行shell腳本 sh: ... not f
這篇文章主要介紹了如何解決jenkins 執(zhí)行shell腳本 sh: ... not found的問題,具有很好的參考價(jià)值,希望對大家有所幫助。2023-06-06shell參數(shù)換行與shell輸出換行的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于shell參數(shù)換行與shell輸出換行的方法實(shí)例,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02Shell腳本實(shí)現(xiàn)關(guān)閉多余的tty終端
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)關(guān)閉多余的tty終端,本文腳本適用centos6系統(tǒng),請根據(jù)自己的服務(wù)器系統(tǒng)情況選擇使用,需要的朋友可以參考下2014-12-12