微信招聘动态模板免费:expect例子——?自动ftp下载,?自动实现telnet

来源:百度文库 编辑:中财网 时间:2024/04/28 02:28:41

expect例子—— 自动ftp下载, 自动实现telnet

(2009-08-11 15:16:13) 转载标签:

自动ftp

自动telnet

杂谈

分类: perl,cgi,shell脚本编程 自动ftp下载
[macg@localhost tiptest]$ vi expecttest
#!/usr/bin/expect
set timeout 3

spawn ftp 192.168.1.11
expect "*Name*"
exec sleep 1
send "macg\n"
expect "*Password:*"
exec sleep 1
send "008421\n"
send "\n"
expect "ftp>"
send "bin\n"
exec sleep 1
send "get asian_0264_4.mpg\n"
exec sleep 4
expect "ftp>"
send "quit\n"
expect "*$*"
send_user "success\n"
[macg@localhost tiptest]$ ls
exp1 expecttest testm.c tt.c ttt.sh

[macg@localhost tiptest]$ ./expecttest
spawn ftp 192.168.1.11
Connected to 192.168.1.11.
221 Goodbye!
500 'AUTH': command not understood.
KERBEROS_V4 rejected as an authentication type
Name (192.168.1.11:macg): macg
331 Password required for macg.
Password:
230 User macg logged in.
Using binary mode to transfer files.
ftp> ftp> bin
200 Type set to I.
ftp> get asian_0264_4.mpg
local: asian_0264_4.mpg remote: asian_0264_4.mpg
227 Entering Passive Mode (192,168,1,11,4,12).
150 Opening data connection for asian_0264_4.mpg.
226 Transfer complete.
1269764 bytes received in 1.4 seconds (8.9e+02 Kbytes/s)
ftp> quit
221 Goodbye!
success

$ ls
asian_0264_4.mpg exp1 expecttest testm.c tt.c ttt.sh


一个自动实现telnet到cisco router 并自动tftp running-config 到host的shell script程序
$ ls
autocisco autodownload autologin user.cfg

$ cat user.cfg
[host] user macg
[host] passwd 008421
[host] filename 2509.cfg
[host] ip 192.168.1.12
[cisco] ip 192.168.1.150
[cisco] passwd cisco
[cisco] enable cisco
配置文件
$ cat autodownload
#!/bin/bash
user=`cat $1 | grep host | gawk '/user/{print $3}'`
passwd=`cat $1 | grep host | gawk '/passwd/{print $3}'`
cfgfile=`cat $1 | grep host | gawk '/filename/{print $3}'`
hostipadd=`cat $1 | grep host | gawk '/ip/{print $3}'`
./autologin $user $passwd $hostipadd $cfgfile

ipadd=`cat $1 | grep cisco | gawk '/ip/{print $3}'`
ciscopass=`cat $1 | grep cisco | gawk '/passwd/{print $3}'`
enablepass=`cat $1 | grep cisco | gawk '/enable/{print $3}'`
./autocisco $ciscopass $enablepass $ipadd $cfgfile 运行主文件

$ cat autologin
#!/usr/bin/expect

proc do_console_login {login pass} {

set timeout 5
set done 1
set timeout_case 0

while ($done) {
exec sleep 1
expect {
"*login:" { send "$login\n" }
"*Password:" { send "$pass\n" }
"*$*" {
set done 0
send_user "\n\nLogin Successfully...\n\n"
send_user "done is $done\n"
return
}
timeout {
switch -- $timeout_case {
0 { send "\n" }
1 {
send_user "Send a return...\n"
send "\n"
}
2 {
puts stderr "Login time out...\n"
exit 1
}
}
incr timeout_case
}
}
}

}

proc do_exec_cmd {cfgfile} {

set timeout 5
send "\n"
expect "*$*"
send "touch /home/macg/tftpboot/$cfgfile\n"
因为unix tftp前,必须先touch文件且改文件权限
expect "*$*"
send "chmod 666 /home/macg/tftpboot/$cfgfile\n"
send "ls -l /home/macg/tftpboot/$cfgfile\n"
expect {
"*$*"
{
send "exit\n"
send_user "\n\nFinished...\n\n"
}
}
}


set LOGIN [lindex $argv 0]
set PASS [lindex $argv 1]
set IPADD [lindex $argv 2]
set CFGFILE [lindex $argv 3]

spawn telnet $IPADD

do_console_login $LOGIN $PASS
do_exec_cmd $CFGFILE$ cat autocisco
#!/usr/bin/expect

proc do_console_login {pass enablepass} {

set timeout 5
set done 1
set timeout_case 0

expect "Password:"
send "$pass\n"
exec sleep 1
expect "*>*"
send "enable\n"
expect "Password:"
send "$enablepass\n"
exec sleep 1
expect "*#"
send_user "\n\nLogin Successfully...\n\n"
return
}

proc do_exec_cmd {cfgfile} {

set timeout 5
send "\n"
expect "*#"
send "copy start tftp\n"
expect "Address or name of remote host*"
send "192.168.1.12\n"
expect "Destination filename*"
send "$cfgfile\n"
expect {
"*#"
{
send "exit\n"
send_user "\n\nFinished...\n\n"
}
}

}

set PASS [lindex $argv 0]
set ENABLEPASS [lindex $argv 1]
set IPADD [lindex $argv 2]
set CFGFILE [lindex $argv 3]

spawn telnet $IPADD

do_console_login $PASS $ENABLEPASS
do_exec_cmd $CFGFILE$ ./autodownload user.cfg
spawn telnet 192.168.1.12
Trying 192.168.1.12...
Connected to www.macg.com.1.168.192.in-addr.arpa (192.168.1.12).
Escape character is '^]'.
Fedora Core release 4 (Stentz)
Kernel 2.6.11-1.1369_FC4 on an i686
login: macg
Password:
Last login: Fri Feb 16 21:03:48 from www.macg.com.1.168.192.in-addr.arpa
macg
[macg@localhost ~]$ macg
-bash: macg: command not found
[macg@localhost ~]$

Login Successfully...

done is 0

[macg@localhost ~]$ touch /home/macg/tftpboot/2509.cfg
[macg@localhost ~]$ chmod 666 /home/macg/tftpboot/2509.cfg
[macg@localhost ~]$ ls -l /home/macg/tftpboot/2509.cfg
-rw-rw-rw- 1 macg macg 0 Feb 16 21:04 /home/macg/tftpboot/2509.cfg
[macg@localhost ~]$

Finished...

spawn telnet 192.168.1.150
Trying 192.168.1.150...
Connected to 192.168.1.150 (192.168.1.150).
Escape character is '^]'.


User Access Verification

Password:
ciscor>enable
Password:
ciscor#

Login Successfully...


ciscor#copy start tftp
Address or name of remote host []? 192.168.1.12
Destination filename [ciscor-confg]? 2600.cfg
!!
4178 bytes copied in 0.308 secs (13565 bytes/sec)
ciscor#

Finished...$ ls ../tftpboot/
2600.cfg cisco.cfg gogo.cfg testcfg.cfg

$ cat ../tftpboot/2509.cfg
!
version 12.2
service timestamps debug uptime
service timestamps log uptime
no service password-encryption
!
hostname ciscor
!
enable secret 5 $1$dwwL$Z/S2FbQXuSG7f9OVPMkcs0
enable password cisco
!
ip subnet-zero
ip cef
no ip domain-lookup
ip host r6 2006 192.168.1.150
ip host r2 2002 192.168.1.150
ip host r5 2005 192.168.1.150
ip host r1 2001 192.168.1.150
ip host sw1 2008 192.168.1.150
ip host r4 2004 192.168.1.150
ip host r3 2003 192.168.1.150
!
!
!
interface Loopback0
ip address 7.7.2.2 255.255.255.255
!
interface Loopback1
ip address 7.7.22.22 255.255.255.0
!