以某一用户名和密码 登录请求脚本_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
f85e97416066d32365bebd8975cd015e.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值