zoukankan      html  css  js  c++  java
  • autoftp 多个网络间传送文件

    Table of Contents

    1 MOTIVATION

      Now I am a new leaner in a company for about one year, and usually I have to use the telnet/ftp to get a file through many LANs. I found it wastes a lot of time. So I want to develop a tool to do this repeated procedure.

      But I don't know how to do. I have known about python, but I found it is also difficult to develop a auto ftp tool with it.

      Accidentally, I found a instrument: tcl/expect, and it is designed to do the auto interaction.

      Then, I use a few days to learn the tcl/expect language syntax. And I use a weekend to develop this tool: autoftp.exp. Then it appears in fount of us.

      Enjoy it…

    2 SOFTWARE DESCRIPTION

    under GNU/GPL release

    3 PREINSTALL

    • need tcl
    • need expect

    4 HOW TO

    • config IP/Username/Password in ./autoftp.exp UPTABLE or DOWNTABLE example:
       
      IPusernamepasswordpathsaveflag
      192.168.1.1 user01 password01 /home/user01 on
      192.168.1.2 user02 password02 /home/user02 off
      192.168.1.3 user03 password03 /home/user03 on
    • download

      file03 at the 192.168.1.3:/home/user03/

      expample:

      ./autoftp.exp up file03

      then the file03 will be transfered to 192.168.1.1:/home/user01/

    • upload

      file01 at the 192.168.1.1:/home/user01/

      expample:

      ./autoftp.exp down file01

      then the file01 will be transfered to 192.168.1.3:/home/user03/

    5 LIMITS

    • When downloading file, ftp/telnet usually can't be back reversely, only using ftp> put command. Then I have changed the ftp> put –> ftp> get.
    • If the ftp> cd path; error, this is usually limited by the admin user. If this situation occurs, you will have to do some translate, like copy file to the path that can be changed by ftp tool.

    6 ACKNOWLEDGE

    • lihan
    • hecj

    7 SOURCE

    #!/usr/bin/expect --
    
    #--------------------------------------------------------------#
    #                                                              #
    # a multilayer LANs ftp tool                                   #
    #                                                              #
    # usage: ./autoftp.exp [up|down] filename                      #
    #                                                              #
    # history:                                                     #
    # 2013.1.26     1.0version    author:wangsd     basic version  # 
    #                                                              #
    #--------------------------------------------------------------#
    
    #-----------------------------------------#
    #                                         #
    # a table storing the ip and account info #
    #                                         #
    #-----------------------------------------#
    #  ip addr             username     password         path             saveflag 
    set UPTABLE {
    {   192.168.1.1        user01       password01       /home/user01/    on  }
    {   192.168.1.2        user02       password02       /home/user02/    off }
    {   192.168.1.3        user03       password03       /home/user03/    on  }
    }
    
    set DOWNTABLE {
    {   192.168.1.1        user01       password01       /home/user01/    on  }
    {   192.168.1.2        user02       password02       /home/user02/    off }
    {   192.168.1.3        user03       password03       /home/user03/    on  }
    }
    
    #-----------------------------------------#
    #                                         #
    #     initialize parameters               #
    #                                         #
    #-----------------------------------------#
    set OK 0 ; # right return value
    set NG 1 ; # error return value
    set RET $NG ; # function's return value
    set FTP_RET $NG ; # do ftp function's return value
    set TEL_RET $NG ; # do telnet function's return value
    
    if { $argc != 2 } {
        puts "usage ./autoftp.exp [up|down] filename"
        exit 0
    }
    
    set ACTION  [lindex $argv 0] 
    set FILE    [lindex $argv 1]
    
    if { $ACTION == "up" } {
        set MAX [llength $UPTABLE]
    } else {
        set MAX [llength $DOWNTABLE]
    }
    
    
    #-----------------------------------------#
    #                                         #
    #     turn off the standard output        #
    #     set time out value                  #
    #                                         #
    #-----------------------------------------#
    log_user 0
    set timeout -1
    
    #-----------------------------------------#
    #                                         #
    #    function: put a file to a server     #
    #                                         #
    #-----------------------------------------#
    proc do_ftp_put { hostname username password path file} {
        global OK
        global NG
        send "ftp $hostname\r"
        expect { 
            "Name*:"     { send   "$username\r" }
            timeout      { return $NG           } }
        expect { 
            "Password:"  { send   "$password\r" }
            timeout      { return $NG           } } 
        expect { 
            "ftp>"       { send   "cd $path\r"  } 
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send   "bin\r"       }
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send "put $file\r"   }
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send   "by\r"        }
            timeout      { return $NG           } }
        expect { 
            -re "(#|%|\\\$) ?$" { return $OK   } 
            timeout     { return $NG           } }
    } 
    
    #-----------------------------------------#
    #                                         #
    #    function: get a file from a server   #
    #                                         #
    #-----------------------------------------#
    proc do_ftp_get { hostname username password path file} {
        global OK
        global NG
        send "ftp $hostname\r"
        expect { 
            "Name*:"     { send   "$username\r" }
            timeout      { return $NG           } }
        expect { 
            "Password:"  { send   "$password\r" }
            timeout      { return $NG           } } 
        expect { 
            "ftp>"       { send   "cd $path\r"  } 
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send   "bin\r"       }
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send "get $file\r"   }
            timeout      { return $NG           } }
        expect { 
            "ftp>"       { send   "by\r"        }
            timeout      { return $NG           } }
        expect { 
            -re "(#|%|\\\$) ?$" { return $OK   } 
            timeout     { return $NG           } }
    }
    
    #-----------------------------------------#
    #                                         #
    #    function:  telnet a remote server    #
    #                                         #
    #-----------------------------------------#
    proc do_telnet { hostname username password path } {
        global OK
        global NG
        send "telnet $hostname\r"
        expect { 
            -timeout    20
            "login:"    { send "$username\r" }
            timeout     { return $NG         } }
        expect { 
            "Password:" { send "$password\r" }
             timeout     { return $OK         } }
        expect { 
            -re "(#|%|\\\$) ?$" { send "cd $path\r" } 
            timeout     { return $NF         } } 
        expect { 
            -re "(#|%|\\\$) ?$" { return $OK } 
            timeout     { return $NG         } }
    }
    
    #-----------------------------------------#
    #                                         #
    #     upload action's Main part           #
    #                                         #
    #-----------------------------------------#
    if { $ACTION == "up" } {
    
        set hostname [lindex $UPTABLE 0 0]
        set username [lindex $UPTABLE 0 1]
        set password [lindex $UPTABLE 0 2]
        set path     [lindex $UPTABLE 0 3]
    
        #------------------#
        # expect spawn     #
        #------------------#
        spawn telnet $hostname
        expect "login:"    { send "$username\r" }
        expect "Password:" { send "$password\r" }
        expect -re "(#|%|\\\$) ?$" { send "cd $path\r" }
        expect { 
            -re "(#|%|\\\$) ?$" { } 
            timeout { puts "telnet to $hostname ERROR"; exit 1 }
        }
    
        set index 1
        while {$index < $MAX} {
            set hostname  [lindex $UPTABLE $index 0]
            set username  [lindex $UPTABLE $index 1]
            set password  [lindex $UPTABLE $index 2]
            set path      [lindex $UPTABLE $index 3]
            set saveflag  [lindex $UPTABLE [expr $index-1] 4]
    
            #------------#
            # ftp once   #
            #------------#
            puts -nonewline "put to $hostname ."
            set FTP_RET [do_ftp_put    $hostname $username $password $path $FILE]
    
            #-------------#
            # delete file #
            #-------------#
            if { $saveflag == "off" } {
                send "rm -f $FILE\r"
            }
            puts -nonewline "."
    
            #-------------#
            # telnet once #
            #-------------#
            set TEL_RET [do_telnet $hostname $username $password $path]
    
            puts -nonewline "."
    
            if { $FTP_RET == $OK && $TEL_RET == $OK } {
               puts "OK"
            } else {
               puts "ERROR" 
               exit 1
            }
    
            incr index +1
        }
    
        puts "Upload complete."
    }
    
    #-----------------------------------------#
    #                                         #
    #   download action's Main part           #
    #                                         #
    #-----------------------------------------#
    if { $ACTION == "down" } {
        set hostname [lindex $DOWNTABLE 0 0]
        set username [lindex $DOWNTABLE 0 1]
        set password [lindex $DOWNTABLE 0 2]
        set path     [lindex $DOWNTABLE 0 3]
    
        #---------------------------#
        # telnet to the last server #
        #---------------------------#
        spawn telnet $hostname
        expect "login:"    { send "$username\r" }
        expect "Password:" { send "$password\r" }
        expect -re "(#|%|\\\$) ?$" { send "cd $path\r" }
        expect { 
            -re "(#|%|\\\$) ?$" { } 
            timeout { puts "telnet to $hostname ERROR"; exit 1 }
        }
    
        set index 1
        while { $index < [expr $MAX-1] } {
            set hostname  [lindex $DOWNTABLE $index 0]
            set username  [lindex $DOWNTABLE $index 1]
            set password  [lindex $DOWNTABLE $index 2]
            set path      [lindex $DOWNTABLE $index 3]
    
            set TEL_RET [do_telnet $hostname $username $password $path]
            puts "telnet to $hostname OK"
            incr index +1 
        }
    
        #----------------------------------#
        # from last server to first server #
        #----------------------------------#
        set index [expr $MAX-1]
        while { $index > 0 } {
            set hostname  [lindex $DOWNTABLE $index 0]
            set username  [lindex $DOWNTABLE $index 1]
            set password  [lindex $DOWNTABLE $index 2]
            set path      [lindex $DOWNTABLE $index 3]
            set saveflag  [lindex $DOWNTABLE $index 4]
    
            #------------#
            # ftp once   #
            #------------#
            puts -nonewline "get from $hostname ."
            set FTP_RET [do_ftp_get $hostname $username $password $path $FILE]
    
            #-------------#
            # delete file #
            #-------------#
            set TEL_RET [do_telnet $hostname $username $password $path] 
            if { $saveflag == "off" } {
                send "rm -f $FILE\r"
            }
            if { $TEL_RET == $OK } {
                send "exit\r"
            } else {
                puts "ERROR"
                exit 1
            } 
            expect -re "(#|%|\\\$) ?$"
            puts -nonewline "."
    
            #-------------#
            # telnet once #
            #-------------#
            if { $FTP_RET == $OK } {
                send "exit\r"
            } else {
                puts "ERROR"
                exit 1
            } 
    
            expect -re "(#|%|\\\$) ?$"
    
            set TEL_RET $OK
            #set TEL_RET [do_telnet $hostname $username $password $path]
    
            puts -nonewline "."
    
            if { $FTP_RET == $OK && $TEL_RET == $OK } {
               puts "OK"
            } else {
               puts "ERROR" 
               exit 1
            }
    
            incr index -1
        }
    
        puts "Download complete."
    }
    
    #--------------------- end of file -------------------------------#
    

    Date: 2013-01-29 16:42:23

    Author:wangsd

    Org version 7.8.11 with Emacs version 24

    Validate XHTML 1.0
  • 相关阅读:
    sort和uniq去重操作【转】
    MySQL中interactive_timeout和wait_timeout的区别【转】
    Keepalived详解(五):Keepalived集群中MASTER和BACKUP角色选举策略【转】
    Keepalived详解(四):通过vrrp_script实现对集群资源的监控【转】
    Keepalived详解(三):Keepalived基础功能应用实例【转】
    Spring详解(六)------AspectJ 实现AOP
    深入理解计算机系统(1.3)------操作系统的抽象概念
    深入理解计算机系统(1.2)------存储设备
    深入理解计算机系统(1.1)------Hello World 是如何运行的
    深入理解计算机系统(序章)------谈程序员为什么要懂底层计算机结构
  • 原文地址:https://www.cnblogs.com/wangshide/p/2881693.html
Copyright © 2011-2022 走看看