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
  • 相关阅读:
    【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
    【Azure 服务总线】详解Azure Service Bus SDK中接收消息时设置的maxConcurrentCalls,prefetchCount参数
    【Azure 应用服务】App Service站点Header头中的中文信息显示乱码?当下载文件时,文件名也是乱码?
    【Azure 应用服务】Azure App Service 自带 FTP服务
    【Azure 微服务】PowerShell中,用Connect-ServiceFabricCluster命令无法连接到sf-test.chinaeast2.cloudapp.chinacloudapi.cn:19000 问题分析
    【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID
    【Azure Developer】Visual Studio 2019中如何修改.Net Core应用通过IIS Express Host的应用端口(SSL/非SSL)
    【Azure 应用服务】Azure SignalR 是否可以同时支持近十万人在线互动
    Markedown实现
    vue-router的使用之如何让导航栏实现一种动态数据 与 如何实现导航栏刷新页面保持状态不变
  • 原文地址:https://www.cnblogs.com/wangshide/p/2881693.html
Copyright © 2011-2022 走看看