(go) Upload file ta3 dedicated (no multilevel directory)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. 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
Parameter determination
If Len (OS. Args) = = 1 {
Fmt. Printf ("Usage:%s filename1 filename2 ... \ n", filepath. Base (OS. Args[0]))
Return
}
You should determine if the file exists before opening it.
_, Err: = OS. Stat (filename)
If err! = Nil {
Log. PRINTLN ("file does not exist!")
Log. PRINTLN ("Setup Sample: 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)//opening file
Open File Error handling
Defer func () {
F.close ()
}()
If nil = = Err {
Buff: = Bufio. Newreader (f)//read-in cache
for {
Line, err: = Buff. ReadString (' \ n ')//read a line with ' \ n ' for Terminator
If err! = Nil | | Io. EOF = = Err {
Break
}
If it starts with a #, it's considered an annotation.
Strings. Trim (line, "")
If OK: = Strings. Contains (Line, "#"); OK {
Continue
}
Go to the carriage return line break
Sample 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)
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.