except自動(dòng)登錄的幾段代碼分享
#!/usr/bin/expect -f
set timeout 30
set host "192.168.1.198"
spawn ssh $host
expect_before "no)?" {
send "yes\r" }
sleep 1
expect "password:"
send "123456\r"
expect "*#"
send "echo my name is fivetrees > /root/fivetrees.txt\r"
interact
##----------------------------
[root@fivetrees ~]# cat expect
#!/usr/bin/expect
for {set i 10} {$i <= 12} {incr i} {
set timeout 30
set ssh_user [lindex $argv 0]
spawn ssh -i .ssh/$ssh_user abc$i.com
expect_before "no)?" {
send "yes\r" }
sleep 1
expect "password*"
send "hello\r"
expect "*#"
send "echo hello expect! > /tmp/expect.txt\r"
expect "*#"
send "echo\r"
}
exit
##-------------------------
#!/usr/bin/expect
if {$argc!=2} {
send_user "usage: ./expect ssh_user password\n"
exit
}
foreach i {11 12} {
set timeout 30
set ssh_user [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh -i .ssh/$ssh_user root@xxx.yy.com
expect_before "no)?" {
send "yes\r" }
sleep 1
expect "Enter passphrase for key*"
send "password\r"
expect "*#"
send "echo hello expect! > /tmp/expect.txt\r"
expect "*#"
send "echo\r"
}
exit
##---------------------------
#!/usr/bin/expect
set timeout 20
if {$argc < 1} {
puts "Usage: script IP"
exit 1
}
# 替換你自己的用戶名
set user "username"
#替換你自己的登錄密碼
set password "yourpassword"
foreach IP $argv {
spawn ssh $user@$IP
expect \
"(yes/no)?" {
send "yes\r"
expect "password:?" {
send "$password\r"
}
} "password:?" {
send "$password\r"
}
expect "\$?"
# 替換你要執(zhí)行的命令
send "last\r"
expect "\$?"
sleep 10
send "exit\r"
expect eof
}
使用方法
script_name ip1 ip2 ip3 ...
##---------------------
#!/bin/sh
# -*- tcl -*- \
exec tclsh $0 "$@"
package require Expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set argv [lrange $argv 2 end]
set prompt "(%|#|\\$) $"
foreach ip $argv {
spawn ssh -t $username@$ip sh
lappend ids $spawn_id
}
expect_before -i ids eof {
set index [lsearch $ids $expect_out(spawn_id)]
set ids [lreplace $ids $index $index]
if [llength $ids] exp_continue
}
expect -i ids "(yes/no)\\?" {
send -i $expect_out(spawn_id) yes\r
exp_continue
} -i ids "Enter passphrase for key" {
send -i $expect_out(spawn_id) \r
exp_continue
} -i ids "assword:" {
send -i $expect_out(spawn_id) $password\r
exp_continue
} -i ids -re $prompt {
set spawn_id $expect_out(spawn_id)
send "echo hello; exit\r"
exp_continue
} timeout {
exit 1
}
相關(guān)文章
shell腳本中main函數(shù)中$#獲取不到腳本傳入?yún)?shù)個(gè)數(shù)淺析
Linux的shell腳本,有時(shí)候我們?cè)谶\(yùn)行shell腳本時(shí)會(huì)給腳本傳入?yún)?shù),出于邏輯上的嚴(yán)謹(jǐn),在腳本中可能會(huì)做一些邏輯判斷或處理,例如判斷腳本傳入?yún)?shù)的個(gè)數(shù),這篇文章主要介紹了shell腳本中main函數(shù)中$#獲取不到腳本傳入?yún)?shù)個(gè)數(shù)淺析,需要的朋友可以參考下2024-03-03
shell編程時(shí)出現(xiàn):未預(yù)期的符號(hào)?`then'?附近有語(yǔ)法錯(cuò)誤?或者?:?行?:?`then&
這篇文章主要介紹了shell編程時(shí)出現(xiàn):未預(yù)期的符號(hào)?`then'?附近有語(yǔ)法錯(cuò)誤?或者?:?行?:?`then'問(wèn)題,本文給大家介紹的非常詳細(xì),需要的朋友參考下2022-12-12
shell腳本編寫(xiě)ping包及arp的監(jiān)控并發(fā)送短信功能
這篇文章主要介紹了shell腳本編寫(xiě)ping包及arp的監(jiān)控并發(fā)送短信功能,需要的朋友可以參考下2017-03-03
使用Shell 腳本實(shí)現(xiàn)每隔100行插入一條記錄且記錄第一列包含行號(hào)其他列不變
本文通過(guò)實(shí)例代碼給大家講解了使用Shell 腳本實(shí)現(xiàn)每隔100行插入一條記錄且記錄第一列包含行號(hào)其他列不變的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
Linux Shell 常見(jiàn)的命令行格式簡(jiǎn)明總結(jié)
這篇文章主要介紹了Linux Shell 常見(jiàn)的命令行格式簡(jiǎn)明總結(jié),非常實(shí)用,需要的朋友可以參考下2014-04-04
Shell中if的基本語(yǔ)法和常見(jiàn)判斷用法
這篇文章主要介紹了Shell中if的基本語(yǔ)法和常見(jiàn)判斷用法,本文講解了if的基本語(yǔ)法、對(duì)字符串的判斷、對(duì)數(shù)字的判斷、對(duì)文件屬性的判斷、邏輯判斷等內(nèi)容,需要的朋友可以參考下2015-06-06
touch shell腳本并修改為777權(quán)限的方法
這篇文章主要介紹了touch shell腳本并修改為777權(quán)限的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

