You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
5 years ago
|
package tools
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"b612.me/sshd"
|
||
|
|
||
|
"github.com/spf13/cobra"
|
||
|
)
|
||
|
|
||
|
var sftpcmd = &cobra.Command{
|
||
|
Use: "sftp",
|
||
|
Short: "sftp上传下载",
|
||
|
Long: "sftp上传下载",
|
||
|
Run: func(this *cobra.Command, args []string) {
|
||
|
d, _ := this.Flags().GetBool("download")
|
||
|
i, _ := this.Flags().GetString("identify")
|
||
|
k, _ := this.Flags().GetString("password")
|
||
|
p, _ := this.Flags().GetInt("port")
|
||
|
b, _ := this.Flags().GetInt("buffer")
|
||
|
var user, host string
|
||
|
var err error
|
||
|
if len(args) != 3 {
|
||
|
fmt.Println("sftp <[user@]Host> <Local> <Remote>")
|
||
|
this.Help()
|
||
|
return
|
||
|
}
|
||
|
hosts := strings.Split(args[0], "@")
|
||
|
if len(hosts) == 1 {
|
||
|
host = hosts[0]
|
||
|
user = "root"
|
||
|
} else {
|
||
|
user = hosts[0]
|
||
|
host = hosts[1]
|
||
|
}
|
||
|
fmt.Println("进行SSH连接……")
|
||
|
client, err := sshd.Connect(user, k, host, i, p, []string{})
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return
|
||
|
}
|
||
|
defer client.Close()
|
||
|
fmt.Println("已连接上……")
|
||
|
sftp, err := sshd.CreateSftp(client)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return
|
||
|
}
|
||
|
defer sftp.Close()
|
||
|
fmt.Println("已建立SFTP……")
|
||
|
shell := func(pect float64) {
|
||
|
if pect != 100.0 {
|
||
|
fmt.Printf("传输已完成:%f%%\r", pect)
|
||
|
} else {
|
||
|
fmt.Printf("传输已完成:%f%%\n", pect)
|
||
|
}
|
||
|
}
|
||
|
if !d {
|
||
|
err = sshd.FtpTransferOutFunc(args[1], args[2], b, shell, sftp)
|
||
|
} else {
|
||
|
err = sshd.FtpTransferInFunc(args[1], args[2], b, shell, sftp)
|
||
|
}
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
Maincmd.AddCommand(sftpcmd)
|
||
|
sftpcmd.Flags().BoolP("download", "d", false, "进行下载")
|
||
|
sftpcmd.Flags().StringP("identify", "i", "", "RSA登录密钥")
|
||
|
sftpcmd.Flags().StringP("password", "k", "", "登录密码")
|
||
|
sftpcmd.Flags().IntP("port", "p", 22, "登录端口")
|
||
|
sftpcmd.Flags().IntP("buffer", "b", 10240, "buffer大小")
|
||
|
}
|