关闭

(go) 上传文件ta3 专用(无多级目录)

742人阅读 评论(0) 收藏 举报
分类:
package main
import (
    "bufio"
    "flag"
    "fmt"
    "io"
    "log"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
    "time"
)
const (
    filename = "c:\\myetc\\remote.etc"
)
func main() {
    var servicefilepath string
    var serviceIP string
    var passwd string
    var serviceusername string
    var port string
    //参数判断
    if len(os.Args) == 1 {
        fmt.Printf("usage: %s filename1 filename2 ...\n", filepath.Base(os.Args[0]))
        return
    }
    //打开之前应该判断文件是否存在
    _, err := os.Stat(filename)
    if err != nil {
        log.Println("文件不存在!")
        log.Println("设置的样例:c:\\myetc\\remote.etc"
        log.Println("m_ta/m_ta@192.168.224.128:22:/home/m_ta/src")
        return
    }
    f, err := os.Open(filename) //打开文件
    //打开文件出错处理
    defer func() {
        f.Close()
    }()
    if nil == err {
        buff := bufio.NewReader(f) //读入缓存
        for {
            line, err := buff.ReadString('\n') //以'\n'为结束符读入一行
            if err != nil || io.EOF == err {
                break
            }
            //如果是#开头的则认为是注释符
            strings.Trim(line, " ")
            if ok := strings.Contains(line, "#"); ok {
                continue
            }
            //去回车换行符
            //样例 m_ta/m_ta@192.168.224.128:22:/home/m_ta/src
            splitstrings := strings.FieldsFunc(strings.Replace(line, "\r\n", "", -1), func(char rune) bool {
                switch char {
                case ':', '/', '@':
                    return true
                }
                return false
            })
            passwd, serviceusername, serviceIP, port = splitstrings[0], splitstrings[1], splitstrings[2], splitstrings[3]
            servicefilepath = "/" + strings.Join(splitstrings[4:], "/")
        }
        parameter := serviceusername + "@" + serviceIP + ":" + servicefilepath
        flag.Parse() // Scans the arg list and sets up flags
        for i := 0; i < flag.NArg(); i++ {
            cmd := exec.Command("pscp.exe", "-P", port, "-pw", passwd, flag.Arg(i), parameter)
            err := cmd.Run()
            if err != nil {
                log.Print(err)
            } else {
                log.Print("OK!")
            }
        }
        time.Sleep(1 * time.Second)
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:450952次
    • 积分:4048
    • 等级:
    • 排名:第8432名
    • 原创:95篇
    • 转载:17篇
    • 译文:9篇
    • 评论:40条
    最新评论