使用Golang执行ssh命令
- v1.0.3 2022/06/26 升级:日志组件升级
- v1.0.4 2022/06/26 新增:文件上传和下载
- v1.0.5 2022/06/26 优化:移除日志
package main
import (
"fmt"
"github.com/zhangdapeng520/zdpgo_ssh"
)
func main() {
// 创建对象
s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
Host: "192.168.33.10",
Port: 22,
Username: "zhangdapeng",
Password: "zhangdapeng",
})
// 进行连接
output, err := s.Run("free -h")
// 查看命令结果
fmt.Printf("%v\n%v", output, err)
// 查看健康状态
fmt.Println(s.Status())
}
package main
import (
"fmt"
"github.com/zhangdapeng520/zdpgo_ssh"
)
func main() {
s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
Host: "192.168.33.10",
Port: 22,
Username: "zhangdapeng",
Password: "zhangdapeng",
})
output, err := s.Sudo("ls -lah")
fmt.Printf("%v\n%v", output, err)
}
package main
import (
"fmt"
"github.com/zhangdapeng520/zdpgo_ssh"
)
func main() {
s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
Host: "192.168.33.10",
Port: 22,
Username: "zhangdapeng",
Password: "zhangdapeng",
})
output, err := s.Sudo("ls -lah")
fmt.Printf("%v\n%v", output, err)
// 上传文件
s.UploadFile("README.md", "README111.md")
output, err = s.Sudo("ls -lah")
fmt.Printf("%v\n%v", output, err)
}
package main
import (
"fmt"
"github.com/zhangdapeng520/zdpgo_ssh"
)
func main() {
s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
Host: "192.168.33.10",
Port: 22,
Username: "zhangdapeng",
Password: "zhangdapeng",
})
output, err := s.Sudo("ls -lah")
fmt.Printf("%v\n%v", output, err)
// 下载文件
s.DownloadFile("README111.md", "README111.md")
output, err = s.Sudo("ls -lah")
fmt.Printf("%v\n%v", output, err)
}