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/[email protected]: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/[email protected]: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)
}
}