python中可以paramiko實現在遠端主機上執行命令,上傳和下載檔案,用go也可以封裝一個,在go中用ssh就sftp包可以實現,實現了下面的功能
- 在遠端主機執行命令返回結果、返回值
- 上傳和下載檔案遠端主機上,以及傳輸了多少個位元組
認證方式
- 如果指定了密碼,那麼採用使用者+密碼的方式認證,否則採用使用者+祕鑰的方式
- 如果沒有指定使用者,則預設使用當前的使用者
- 如果沒有指定密碼,將採用使用者+祕鑰方式,預設取~/.ssh/id_rsa檔案私鑰檔案獲取祕鑰
和paramik類似
直接上程式碼
package main
import (
"errors"
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"log"
"os"
"os/user"
"time"
)
var (
DefaultSShTcpTimeout = 15 * time.Second // 與ssh建立連線的預設時間,自己設定一個就行
)
// 錯誤定義
var (
InvalidHostName = errors.New("invalid parameters: hostname is empty")
InvalidPort = errors.New("invalid parameters: port must be range 0 ~ 65535")
)
// 返回當前使用者名稱
func getCurrentUser() string {
user, _ := user.Current()
return user.Username
}
// 存放上傳或下載的資訊
type TransferInfo struct {
Kind string // upload或download
Local string // 本地路徑
Dst string // 目標路徑
TransferByte int64 // 傳輸的位元組數(byte)
}
func (t *TransferInfo) String() string {
return fmt.Sprintf(`TransforInfo(Kind:"%s", Local: "%s", Dst: "%s", TransferByte: %d)`,
t.Kind, t.Local, t.Dst, t.TransferByte)
}
// 存放執行結果的結構體資訊
type ExecInfo struct {
Cmd string
Output []byte
ExitCode int
}
func (e *ExecInfo) OutputString() string {
return string(e.Output)
}
func (e *ExecInfo) String() string {
return fmt.Sprintf(`ExecInfo(cmd: "%s", exitcode: %d)`,
e.Cmd, e.ExitCode)
}
type AuthConfig struct {
*ssh.ClientConfig
User string
Password string
KeyFile string
Timeout time.Duration
}
func (a *AuthConfig) setDefault() {
if a.User == "" {
a.User = getCurrentUser()
}
if a.KeyFile == "" {
userHome, _ := os.UserHomeDir()
a.KeyFile = fmt.Sprintf("%s/.ssh/id_rsa", userHome)
}
if a.Timeout == 0 {
a.Timeout = DefaultSShTcpTimeout
}
}
func (a *AuthConfig) SetAuthMethod() (ssh.AuthMethod, error) {
a.setDefault()
if a.Password != "" {
return ssh.Password(a.Password), nil
}
data, err := ioutil.ReadFile(a.KeyFile)
if err != nil {
return nil, err
}
singer, err := ssh.ParsePrivateKey(data)
if err != nil {
return nil, err
}
return ssh.PublicKeys(singer), nil
}
func (a *AuthConfig) ApplyConfig() error {
authMethod, err := a.SetAuthMethod()
if err != nil {
return err
}
a.ClientConfig = &ssh.ClientConfig{
User: a.User,
Auth: []ssh.AuthMethod{authMethod},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: a.Timeout,
}
return nil
}
// 存放連線的結構體
type conn struct {
client *ssh.Client
sftpClient *sftp.Client
}
func (c *conn) Close() {
if c.sftpClient != nil {
c.sftpClient.Close()
c.sftpClient = nil
}
if c.client != nil {
c.client.Close()
c.client = nil
}
}
// SSHClient結構體
type SSHClient struct {
conn
HostName string
Port int
AuthConfig AuthConfig
}
// 設定預設埠資訊
func (s *SSHClient) setDefaultValue() {
if s.Port == 0 {
s.Port = 22
}
}
// 與遠端主機連線
func (s *SSHClient) Connect() error {
if s.client != nil {
log.Println("Already Login")
return nil
}
if err := s.AuthConfig.ApplyConfig(); err != nil {
return err
}
s.setDefaultValue()
addr := fmt.Sprintf("%s:%d", s.HostName, s.Port)
var err error
s.client, err = ssh.Dial("tcp", addr, s.AuthConfig.ClientConfig)
if err != nil {
return err
}
return nil
}
// 一個session只能執行一次命令,也就是說不能在同一個session執行多次s.session.CombinedOutput
// 如果想執行多次,需要每條為每個命令建立一個session(這裡是這樣做)
func (s *SSHClient) Exec(cmd string) (*ExecInfo, error) {
session, err := s.client.NewSession()
if err != nil {
return nil, err
}
defer session.Close()
output, err := session.CombinedOutput(cmd)
var exitcode int
if err != nil {
// 斷言轉成具體實現型別,獲取返回值
exitcode = err.(*ssh.ExitError).ExitStatus()
}
return &ExecInfo{
Cmd: cmd,
Output: output,
ExitCode: exitcode,
}, nil
}
// 將本地檔案上傳到遠端主機上
func (s *SSHClient) Upload(localPath string, dstPath string) (*TransferInfo, error) {
transferInfo := &TransferInfo{Kind: "upload", Local: localPath, Dst: dstPath, TransferByte: 0}
var err error
// 如果sftp客戶端沒有開啟,就開啟,為了複用
if s.sftpClient == nil {
if s.sftpClient, err = sftp.NewClient(s.client); err != nil {
return transferInfo, err
}
}
localFileObj, err := os.Open(localPath)
if err != nil {
return transferInfo, err
}
defer localFileObj.Close()
dstFileObj, err := s.sftpClient.Create(dstPath)
if err != nil {
return transferInfo, err
}
defer dstFileObj.Close()
written, err := io.Copy(dstFileObj, localFileObj)
if err != nil {
return transferInfo, err
}
transferInfo.TransferByte = written
return transferInfo, nil
}
// 從遠端主機上下載檔案到本地
func (s *SSHClient) Download(dstPath string, localPath string) (*TransferInfo, error) {
transferInfo := &TransferInfo{Kind: "download", Local: localPath, Dst: dstPath, TransferByte: 0}
var err error
if s.sftpClient == nil {
if s.sftpClient, err = sftp.NewClient(s.client); err != nil {
return transferInfo, err
}
}
//defer s.sftpClient.Close()
localFileObj, err := os.Create(localPath)
if err != nil {
return transferInfo, err
}
defer localFileObj.Close()
dstFileObj, err := s.sftpClient.Open(dstPath)
if err != nil {
return transferInfo, err
}
defer dstFileObj.Close()
written, err := io.Copy(localFileObj, dstFileObj)
if err != nil {
return transferInfo, err
}
transferInfo.TransferByte = written
return transferInfo, nil
}
// SSHclient的構造方法
func NewSSHClient(hostname string, port int, authConfig AuthConfig) (*SSHClient, error) {
switch {
case hostname == "":
return nil, InvalidHostName
case port > 65535 || port < 0:
return nil, InvalidPort
}
sshClient := &SSHClient{HostName: hostname, Port: port, AuthConfig: authConfig}
err := sshClient.Connect()
if err != nil {
return nil, err
}
return sshClient, nil
}
func main() {
// 測試
sshClient, err := NewSSHClient("172.16.0.178", 22, AuthConfig{User: "root"})
if err != nil {
fmt.Println(err)
return
}
defer sshClient.Close()
//第一次 執行命令
execinfo, err := sshClient.Exec("ls -l")
fmt.Println(execinfo.OutputString(), err)
//第二次執行命令
out1, exitcode2 := sshClient.Exec("ifconfig -a")
fmt.Println(string(out1), exitcode2)
// 上傳檔案
transInfoUpload, err := sshClient.Upload("/tmp/passwd", "/tmp/password_upload")
fmt.Println(transInfoUpload, err)
// 下載檔案
transInfoDownload, err := sshClient.Download("/etc/passwd", "/tmp/passwd_download")
fmt.Println(transInfoDownload, err)
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結