• shell脚本编程-例子_使用expect下载ftp文件


    expecthi一种能够按照脚本内容设定的方式和交互程序进行“对话”的程序

    由于linux中的一些命令不太适合脚本化的自动运行,比如fdisk、telnet、ftp连接下载等,所以就必须使用expect来解决这些场景下的自动化运行问题。

    系统默认没有装ftp、expect命令,需要自己先进行yum安装(也可手工安装)

    脚本事例(expect.exp)如下:

    #!/usr/bin/expect    
    set ip [ lindex $argv 0 ]  #脚本的第一个参数,远程主机的IP地址
    set file [ lindex $argv 1 ]  #脚本的第二个参数,指定下载的文件名
    set timeout 10 #设置超时时间
    spawn ftp $ip #执行ftp $ip命令
    expect "Name*" #如果出现Name字符
    send "anonymous
    "  #则输入anoymous并回车
    expect "Password*" #如果出现Password字符
    send "
    " #则仅输入回车
    expect "fpt>*" #如果出现ftp字符
    send "get $file
    " #则发送get $file命令
    expect {
    #如果返回的字符串含有received则说明下载成功
    "*received*" { send_user "Download successful!";send "quit
    " }
    #如果返回的字符串中含有Faild则说明下载失败.
    "*Failed*" { send_user "Download failed!";send "quit
    "}
    }
    expect eof

    赋予执行权限:chmod 774 ./expect.exp

    执行本脚本是可以使用如下语句:

    ./expect.exp 195.92.253.2 robots.txt

    如果你执行成功应该出现如下语句:

    [cos@localhost shell]$ ./expect.exp  195.92.253.2 robots.txt
    spawn ftp 195.92.253.2
    Connected to 195.92.253.2 (195.92.253.2).
    220 (vsFTPd 2.3.4)
    Name (195.92.253.2:cos): anonymous
    331 Please specify the password.
    Password:
    230-Welcome to ZenIV
    230-
    230-The software on this site is made available for free without warranty or
    230-other right of recourse implied or otherwise. No statement save one in
    230-writing by the owner of the system changes this usage agreement. This 
    230-software is provided in the United Kingdom for United Kingdom users,
    230-any export download is at your own risk and liability.
    230-
    230-Many parts of this archive are mirrors of other sites. While we try not
    230-to mirror any inappropriate material we do not have editorial control over
    230-such mirrors and cannot make such a guarantee.
    230-
    230-There is no other user agreement, should your local law make such an
    230-agreement invalid you are prohibited from using this site, and may be
    230-committing an offence under the computer misuse act by continuing.
    230-
    230-By downloading any file from this site you agree to these terms and
    230-conditions, disconnect now if you do not.
    230-
    230-* * * * * * * * * * * * * Transfer Problems ? * * * * * * * * * * * * *
    230-*                                                                     *
    230-*  If you are having problems accessing this site, then please use    *
    230-*  "passive" transfer mode rather than "port" transfer mode.  Thanks. *
    230-*                                                                     *
    230-*  Note: do **NOT** try using download accelerators like aria2c which *
    230-*        make multiple connections, this will likely trigger an       *
    230-*        autonatic ban respons which you will have to request to be   *
    230-*        removed                                                      *
    230-*                                                                     *
    230-* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> get robots.txt
    local: robots.txt remote: robots.txt
    227 Entering Passive Mode (195,92,253,2,70,227).
    150 Opening BINARY mode data connection for robots.txt (103 bytes).
    226 Transfer complete.
    103 bytes received in 0.000627 secs (164.27 Kbytes/sec)
    ftp> Download successful!quit
    221 Goodbye.

    祝你好运。

    2015年10月16日00:33:59

  • 相关阅读:
    Cocos2d-x 3.2编译生成Android程序出错Error running command, return code: 2的解决方法
    利用Theme自定义Activity进入退出动画
    Activity的四种launchMode
    android中设置控件获得焦点
    android 反编译 for mac
    android中libs目录下armeabi和armeabi-v7a的区别
    解决Sublime Text 3中文显示乱码(tab中文方块)问题,sublime tab乱码
    mysql教程
    Failed to load c++ bson extension, using pure JS version
    mongodb导出数据
  • 原文地址:https://www.cnblogs.com/xccnblogs/p/4884071.html
Copyright © 2020-2023  润新知