日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

以某一用户名和密码 登录请求脚本_linux expect自动交互脚本

發布時間:2024/10/8 linux 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以某一用户名和密码 登录请求脚本_linux expect自动交互脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.expect參數

2.啟用選項

-c :執行腳本前先執行的命令,可多次使用。-d :debug模式,可以在運行時輸出一些診斷信息,與在腳本開始處使用 exp_internal 1 相似。-D :啟用交換調式器,可設一整數參數。-f :從文件讀取命令,僅用于使用#!時。如果文件名為"-",則從stdin讀取(使用"./-"從文件名為-的文件讀取)。-i :交互式輸入命令,使用"exit"或"EOF"退出輸入狀態。-- :表示選項結束(如果你需要傳遞與expect選項相似的參數給腳本時),可放到 #! 行: #!/usr/bin/expect -- 。-v :顯示expect版本信息。

# 命令行參數

# $argv,參數數組,使用[lindex $argv n]獲取,$argv 0為腳本名字# $argc,參數個數set username [lindex $argv 1] # 獲取第1個參數set passwd [lindex $argv 2] # 獲取第2個參數set timeout 30 # 設置超時# spawn是expect內部命令,開啟ssh連接spawn ssh -l username 192.168.1.1# 判斷上次輸出結果里是否包含“password:”的字符串,如果有則立即返回,否則就等待一段時間(timeout)后返回expect "password:"# 發送內容ispass(密碼、命令等)send "ispass"# 發送內容給用戶send_user "$argv0 [lrange $argv 0 2]"send_user "It's OK"# 執行完成后保持交互狀態,控制權交給控制臺(手工操作)。否則會完成后會退出。interact

4.命令介紹

close:關閉當前進程的連接。

debug:控制調試器。

disconnect:斷開進程連接(進程仍在后臺運行)。

定時讀取密碼、執行priv_prog

send_user "password? "expect_user -re "(.*)"for {} 1 {} { if {[fork]!=0} {sleep 3600;continue} disconnect spawn priv_prog expect Password: send "$expect_out(1,string)" . . . exit}exit:退出expect。exp_continue [-continue_timer]:繼續執行下面的匹配。xp_internal [-f file] value:

5. expect范例

自動telnet會話:

#!/usr/bin/expect -fset ip [lindex $argv 0 ] # 接收第1個參數,作為IPset userid [lindex $argv 1 ] # 接收第2個參數,作為useridset mypassword [lindex $argv 2 ] # 接收第3個參數,作為密碼set mycommand [lindex $argv 3 ] # 接收第4個參數,作為命令set timeout 10 # 設置超時時間# 向遠程服務器請求打開一個telnet會話,并等待服務器詢問用戶名spawn telnet $ip expect "username:" # 輸入用戶名,并等待服務器詢問密碼 send "$userid" expect "password:" # 輸入密碼,并等待鍵?需要運行的命令 send "$mypassword" expect "%" # 輸入預先定好的密碼,等待運行結果 send "$mycommand" expect "%" # 將運行結果存入到變量中,顯示出來或者寫到磁盤中 set results $expect_out(buffer) # 退出telnet會話,等待服務器的退出提示EOF send "exit" expect eof

自動建立FTP會話

#!/usr/bin/expect -fset ip [lindex $argv 0 ] # 接收第1個參數,作為IPset userid [lindex $argv 1 ] # 接收第2個參數,作為Useridset mypassword [lindex $argv 2 ] # 接收第3個參數,作為密碼set timeout 10 # 設置超時時間# 向遠程服務器請求打開一個FTP會話,并等待服務器詢問用戶名spawn ftp $ip expect "username:" # 輸入用戶名,并等待服務器詢問密碼 send "$userid" expect "password:" # 輸入密碼,并等待FTP提示符的出現 send "$mypassword" expect "ftp>" # 切換到二進制模式,并等待FTP提示符的出現 send "bin" expect "ftp>" # 關閉ftp的提示符 send "prompt" expect "ftp>" # 下載所有文件 send "mget *" expect "ftp>" # 退出此次ftp會話,并等待服務器的退出提示EOF send "bye" expect eof

自動登錄SSH

#!/usr/bin/expect -f set ip [lindex $argv 0 ] # 接收第1個參數,作為IPset username [lindex $argv 1 ] # 接收第2個參數,作為usernameset mypassword [lindex $argv 2 ] # 接收第3個參數,作為密碼set timeout 10 # 設置超時時間spawn ssh $username@$ip # 發送ssh請求expect { # 返回信息匹配 "*yes/no" { send "yes"; exp_continue} # 第一次ssh連接會提示yes/no,繼續 "*password:" { send "$mypassword" } # 出現密碼提示,發送密碼 } interact # 交互模式,?戶會停留在遠程服務器上?

5.4 批量登錄ssh服務器執行操作范例,設定增量的for循環

#!/usr/bin/expectfor {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" } sleep 1 expect "password*" send "hello" expect "*#" send "echo hello expect! > /tmp/expect.txt" expect "*#" send "echo"}exit

5.5 批量登錄ssh并執?命令,foreach語法

if {$argc!=2} { send_user "usage: ./expect ssh_user password" 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" } sleep 1 expect "Enter passphrase for key*" send "password" expect "*#" send "echo hello expect! > /tmp/expect.txt" expect "*#" send "echo"}exit

另一自動ssh范例

#!/usr/bin/expect# 使用方法: script_name ip1 ip2 ip3 ...set timeout 20if {$argc < 1} { puts "Usage: script IPs" exit 1}# 替換你自己的用戶名set user "username"#替換你自己的登錄密碼set password "yourpassword"foreach IP $argv {spawn ssh $user@$IPexpect "(yes/no)?" { send "yes" expect "password:?" { send "$password" } } "password:?" { send "$password"}expect "$?"# 替換你要執?的命令send "last"expect "$?"sleep 10send "exit"expect eof}

ssh實現自動登錄,并停留在登錄服務器上

#!/usr/bin/expect -f #接收第一個參數,并設置IPset ip [ lindex $argv 0 ]#接收第二個參數,并設置密碼set password [ lindex $argv 1 ]#設置超時時間set timeout 10#發送ssh請求spawn ssh -p2002 root@$ip expect { "*yes/no" { send "yes"; exp_continue} "*password:" { send "$password" } }# 交互模式,用戶會停留在遠程服務器上面.interact

5.8 批量拷貝公鑰至目標主機

[root@LOCALHOST sh]# cat ssh-copy-id.sh #!/bin/bash# 截取第2列以DH開頭的行,并輸出第二列hosts=`awk '$2~/^DH/{print $2}' /etc/hosts`for host in $hosts;do ./ssh-copy-id.exp $host # 調用expect腳本done[root@LOCALHOST sh]# cat ssh-copy-id.exp #!/usr/bin/expect -fset timeout 5set host [lindex $argv 0]spawn ssh-copy-id -p2002 root@$hostexpect { "*yes/no" { send "yes";exp_continue } "*password:" { send "etyfhzm" }}expect eof

總結

以上是生活随笔為你收集整理的以某一用户名和密码 登录请求脚本_linux expect自动交互脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。