Documentation ¶
Index ¶
- Constants
- Variables
- func AuthWithAgent() (ssh.AuthMethod, error)
- func AuthWithKeyboardPassword(password string) (ssh.AuthMethod, error)
- func AuthWithPassword(password string) (ssh.AuthMethod, error)
- func AuthWithPrivateKey(keyfile string, passphrase string) (ssh.AuthMethod, error)
- func AuthWithPrivateKeyString(key string, password string) (ssh.AuthMethod, error)
- func AuthWithPrivateKeyTerminal(keyfile string) (ssh.AuthMethod, error)
- func AuthWithPrivateKeys(keyFiles []string, passphrase string) (ssh.AuthMethod, error)
- func FileExist(file string) bool
- func HasAgent() bool
- func KeyFile() string
- func Md5File(filename string) (string, error)
- func MkdirAll(path string) error
- type Client
- func Connect(cnf *Config) (client *Client, err error)
- func New(cnf *Config) (client *Client, err error)
- func NewClient(host, port, user, password string) (client *Client, err error)
- func NewDSN() (client *Client)
- func NewWithAgent(Host, Port, User string) (client *Client, err error)
- func NewWithPrivateKey(Host, Port, User, Passphrase string) (client *Client, err error)
- func (c *Client) Close()
- func (c *Client) Download(remote string, local string) (err error)
- func (c *Client) Exec(cmd string) error
- func (c *Client) Exec1(cmd string) error
- func (c *Client) ExecSu(cmd, passwd string) ([]byte, error)
- func (c *Client) FileExist(filepath string) (bool, error)
- func (c *Client) IsDir(path string) bool
- func (c *Client) IsExist(path string) bool
- func (c *Client) IsFile(path string) bool
- func (c *Client) IsNotExist(path string) bool
- func (c *Client) Md5File(path string) string
- func (c *Client) Mkdir(path string, fi os.FileInfo) error
- func (c *Client) MkdirAll(dirpath string) error
- func (c *Client) Output(cmd string) ([]byte, error)
- func (c *Client) ReadAll(filepath string) ([]byte, error)
- func (c *Client) Remove(path string) error
- func (c *Client) RemoveAll(remoteDir string) error
- func (c *Client) RemoveDir(remoteDir string) error
- func (c *Client) RemoveDirectory(path string) error
- func (c *Client) RemoveFile(remoteFile string) error
- func (c *Client) Run(cmd string)
- func (c *Client) RunScript(scriptPath string) ([]byte, error)
- func (c *Client) Size(path string) int64
- func (c *Client) Upload(local string, remote string) (err error)
- func (c *Client) UploadDir(localDir string, remoteDir string) (err error)
- func (c *Client) UploadFile(localFile, remote string) error
- type Config
- func (cnf *Config) Connect() (client *Client, err error)
- func (c *Config) SetKeys(keyfiles []string) *Config
- func (c *Config) WithHost(host string) *Config
- func (c *Config) WithKey(keyfile, passphrase string) *Config
- func (c *Config) WithPassword(password string) *Config
- func (c *Config) WithUser(user string) *Config
Constants ¶
const DefaultTimeout = 30 * time.Second
Variables ¶
var Default = DefaultConfig
var DefaultConfig = &Config{
Host: "localhost",
Port: 22,
User: "root",
}
Functions ¶
func AuthWithAgent ¶
func AuthWithAgent() (ssh.AuthMethod, error)
AuthWithAgent use already authed user
func AuthWithKeyboardPassword ¶
func AuthWithKeyboardPassword(password string) (ssh.AuthMethod, error)
AuthWithKeyboardPassword Generate a password-auth'd ssh ClientConfig
func AuthWithPassword ¶
func AuthWithPassword(password string) (ssh.AuthMethod, error)
AuthWithPassword Generate a password-auth'd ssh ClientConfig
func AuthWithPrivateKey ¶
func AuthWithPrivateKey(keyfile string, passphrase string) (ssh.AuthMethod, error)
AuthWithPrivateKey 自动监测是否带有密码
func AuthWithPrivateKeyString ¶
func AuthWithPrivateKeyString(key string, password string) (ssh.AuthMethod, error)
AuthWithPrivateKeyString 直接通过字符串
func AuthWithPrivateKeyTerminal ¶
func AuthWithPrivateKeyTerminal(keyfile string) (ssh.AuthMethod, error)
AuthWithPrivateKeyTerminal 通过终端读取带密码的 PublicKey
func AuthWithPrivateKeys ¶
func AuthWithPrivateKeys(keyFiles []string, passphrase string) (ssh.AuthMethod, error)
AuthWithPrivateKeys 设置多个 ~/.ssh/id_rsa ,如果加密用passphrase尝试
Types ¶
type Client ¶
type Client struct { *Config SSHClient *ssh.Client SSHSession *ssh.Session SFTPClient *sftp.Client }
func NewWithAgent ¶
func NewWithPrivateKey ¶
func (*Client) ExecSu ¶
ExecSu Execute cmd via sudo. Do not include the sudo command in the cmd string. For example: Client.ExecSudo("uptime", "password"). If you are using passwordless sudo you can use the regular Exec() function.
func (*Client) RemoveDirectory ¶
RemoveDirectory Remove a directory from the remote server
func (*Client) RemoveFile ¶
func (*Client) RunScript ¶
RunScript Executes a shell script file on the remote machine. It is copied in the tmp folder and ran in a single session. chmod +x is applied before running. Returns an SshResponse and an error if any has occured
func (*Client) Upload ¶
Upload 上传本地文件 local 到sftp远程目录 remote like rsync rsync -av src/ dst ./src/* --> /root/dst/* rsync -av src/ dst/ ./src/* --> /root/dst/* rsync -av src dst ./src/* --> /root/dst/src/* rsync -av src dst/ ./src/* --> /root/dst/src/*
func (*Client) UploadFile ¶
UploadFile 上传本地文件 localFile 到sftp远程目录 remote
type Config ¶
type Config struct { User string Host string Port int Password string KeyFiles []string Passphrase string StickySession bool // DisableAgentForwarding, if true, will not forward the SSH agent. DisableAgentForwarding bool // HandshakeTimeout limits the amount of time we'll wait to handshake before // saying the connection failed. HandshakeTimeout time.Duration // KeepAliveInterval sets how often we send a channel request to the // server. A value < 0 disables. KeepAliveInterval time.Duration // Timeout is how long to wait for a read or write to succeed. Timeout time.Duration }