====== Программирование диалогов expect ======
===== Установка =====
server# apt install expect
===== Cisco CLI =====
==== Вариант использования с элементами конфигурации в коде скрипта ====
!!! Не обрабатывается ситуация первого подключения (подтверждения ключа)
server# ssh switchN
server# cat cisco_change_conf.exp
#!/usr/bin/expect
set sw [lindex $argv 0];
spawn ssh $sw
send "\n"
expect "sword:" { send "cisco\n" }
expect "#" { send "conf t\n" }
expect "#" { send "ip scp server enable\n" }
expect "#" { send "end\n" }
#expect "#" { send "vlan database\n" }
#expect "#" { send "vtp transparent\n" }
#expect "#" { send "vlan 2 name LAN2\n" }
#expect "#" { send "exit\n" }
expect "#" { send "write\n" }
expect "#" { send "exit\n" }
send_user "\n"
exit 0
server# chmod +x cisco_change_conf.exp
server# ./cisco_change_conf.exp switchN
==== Вариант использования с элементами конфигурации в файле на tftp сервере ====
server# cat /srv/tftp/cisco_change_conf.txt
ip scp server enable
end
server# cat cisco_change_conf.exp
...
expect "#" { send "copy tftp://server/cisco_change_conf.txt runn\n" }
send "\n"
send "\n"
...
===== Communigate CLI =====
mail# cat cgp_cli.exp
#!/usr/bin/expect
set cmd [lindex $argv 0];
spawn telnet localhost 106
expect "200" {
send "USER postmaster\n"
}
expect "300" {
send "PASS Pa\$\$w0rd\n"
}
expect "200" {
send "$cmd\n\n"
}
expect "200" {}
sleep 1
send "QUIT\n"
send_user "\n"
exit 0
mail# chmod +x cgp_cli.exp
mail# ./cgp_cli.exp 'CREATEACCOUNT user2 {Password = "password2";RealName = "Петр Петрович Петров";}'
mail# ./cgp_cli.exp 'GETACCOUNTSETTINGS user2'
===== Asterisk AMI =====
* [[http://the-asterisk-book.com/1.6/asterisk-manager-api.html#manager-interface-beispiel-expect|Example: Getting the number of voicemail messages with expect]]
# cat /usr/share/originate_ami.exp
#!/usr/bin/expect
set username "admin"
set secret "admin"
set host "127.0.0.1"
set port "5038"
set channel [lindex $argv 0];
set num [lindex $argv 1];
set name [lindex $argv 2];
send_user "Args: $channel $num $name\n"
spawn telnet $host $port
expect "Manager" {
send "Action: Login\n"
send "Username: $username\n"
send "Secret: $secret\n\n"
}
expect "Response: Success" {
send "Action: Originate\n"
send "Channel: SIP/$channel\n"
send "Context: default\n"
send "Exten: $num\n"
send "Callerid: $name<$num>\n"
send "Priority: 1\n\n"
}
sleep 1
send "Action: Logoff\n\n"
exit 0
# /usr/share/originate_ami.exp 403 301 "Hello World"
===== Docker ======
webinar BBB# cat /root/scripts/ch_web_pass.exp
#!/usr/bin/expect
set email [lindex $argv 0];
set password [lindex $argv 1];
spawn docker exec -it greenlight-v2 bash
send "\n"
expect "bash*#" {send "bundle exec rails c\n"}
expect "irb*>" { send "User.find_by(email: \"$email\").update_attribute(:password,\"$password\")\n" }
expect "irb*>" { send "exit\n" }
expect "bash*#" { send "exit\n" }
send_user "\n"
exit 0