企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 语法 ### Func ``` func DiscardRequests(in <-chan *Request) func FingerprintLegacyMD5(pubKey PublicKey) string func FingerprintSHA256(pubKey PublicKey) string func Marshal(msg interface{}) []byte func MarshalAuthorizedKey(key PublicKey) []byte func ParseDSAPrivateKey(der []byte) (*dsa.PrivateKey, error) func ParseRawPrivateKey(pemBytes []byte) (interface{}, error) func ParseRawPrivateKeyWithPassphrase(pemBytes, passphrase []byte) (interface{}, error) func Unmarshal(data []byte, out interface{}) error ```` ### Type ``` type AuthMethod func GSSAPIWithMICAuthMethod(gssAPIClient GSSAPIClient, target string) AuthMethod func KeyboardInteractive(challenge KeyboardInteractiveChallenge) AuthMethod func Password(secret string) AuthMethod func PasswordCallback(prompt func() (secret string, err error)) AuthMethod func PublicKeys(signers ...Signer) AuthMethod func PublicKeysCallback(getSigners func() (signers []Signer, err error)) AuthMethod func RetryableAuthMethod(auth AuthMethod, maxTries int) AuthMethod type Client func Dial(network, addr string, config *ClientConfig) (*Client, error) func NewClient(c Conn, chans <-chan NewChannel, reqs <-chan *Request) *Client func (c *Client) Dial(n, addr string) (net.Conn, error) func (c *Client) DialTCP(n string, laddr, raddr *net.TCPAddr) (net.Conn, error) func (c *Client) HandleChannelOpen(channelType string) <-chan NewChannel func (c *Client) Listen(n, addr string) (net.Listener, error) func (c *Client) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) func (c *Client) ListenUnix(socketPath string) (net.Listener, error) func (c *Client) NewSession() (*Session, error) type Conn func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan NewChannel, <-chan *Request, error) type ServerConn func NewServerConn(c net.Conn, config *ServerConfig) (*ServerConn, <-chan NewChannel, <-chan *Request, error) type HostKeyCallback func FixedHostKey(key PublicKey) HostKeyCallback func InsecureIgnoreHostKey() HostKeyCallback type PublicKey func NewPublicKey(key interface{}) (PublicKey, error) func ParseAuthorizedKey(in []byte) (out PublicKey, comment string, options []string, rest []byte, err error) func ParseKnownHosts(in []byte) (marker string, hosts []string, pubKey PublicKey, comment string, rest []byte, ...) func ParsePublicKey(in []byte) (out PublicKey, err error) type Session func (s *Session) Close() error func (s *Session) CombinedOutput(cmd string) ([]byte, error) func (s *Session) Output(cmd string) ([]byte, error) func (s *Session) RequestPty(term string, h, w int, termmodes TerminalModes) error func (s *Session) RequestSubsystem(subsystem string) error func (s *Session) Run(cmd string) error func (s *Session) SendRequest(name string, wantReply bool, payload []byte) (bool, error) func (s *Session) Setenv(name, value string) error func (s *Session) Shell() error func (s *Session) Signal(sig Signal) error func (s *Session) Start(cmd string) error func (s *Session) StderrPipe() (io.Reader, error) func (s *Session) StdinPipe() (io.WriteCloser, error) func (s *Session) StdoutPipe() (io.Reader, error) func (s *Session) Wait() error func (s *Session) WindowChange(h, w int) error ``` ## 实例 ### 远程登录 / 执行 cmd ``` ce := func(err error, msg string) { if err != nil { log.Fatalf("%s error: %v", msg, err) } } client, err := ssh.Dial("tcp", "192.168.0.151:22", &ssh.ClientConfig{ User: "root", Auth: []ssh.AuthMethod{ssh.Password("bigant.cn")}, HostKeyCallback: ssh.InsecureIgnoreHostKey(), }) ce(err, "dial") session, err := client.NewSession() ce(err, "new session") defer session.Close() session.Stdout = os.Stdout session.Stderr = os.Stderr session.Stdin = os.Stdin // 方式一: ssh 直接执行 //session.Run("ls -l") // 方式二:在控制台控制 modes := ssh.TerminalModes{ ssh.ECHO: 0, ssh.TTY_OP_ISPEED: 14400, ssh.TTY_OP_OSPEED: 14400, } err = session.RequestPty(`[linux](https://www.linuxprobe.com/ "linux")`, 32, 160, modes) ce(err, "request pty") err = session.Shell() ce(err, "start shell") err = session.Wait() ce(err, "return") ```