{"id":282,"date":"2022-03-03T13:41:50","date_gmt":"2022-03-03T05:41:50","guid":{"rendered":"https:\/\/www.db2go.net\/?p=282"},"modified":"2022-03-03T13:41:50","modified_gmt":"2022-03-03T05:41:50","slug":"%e4%bd%bf%e7%94%a8ssh%e5%8c%85%e5%ae%9e%e7%8e%b0%e4%ba%a4%e4%ba%92%e5%bc%8f%e6%89%a7%e8%a1%8c%e8%bf%9c%e7%a8%8b%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/www.db2go.net\/?p=282","title":{"rendered":"\u4f7f\u7528ssh\u5305\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4"},"content":{"rendered":"<p>\u521b\u5efaconnect<\/p>\n<pre><code class=\"language-go line-numbers\">func connect(user, password, host string, port int) (*ssh.Session, error) {\n    var (\n        auth  []ssh.AuthMethod\n        addr  string\n        clientConfig *ssh.ClientConfig\n        client *ssh.Client\n        session *ssh.Session\n        err  error\n    )\n    \/\/ get auth method\n    auth = make([]ssh.AuthMethod, 0)\n    auth = append(auth, ssh.Password(password))\n\n    clientConfig = &amp;ssh.ClientConfig{\n        User: user,\n        Auth: auth,\n        Timeout: 30 * time.Second,\n        HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {\n            return nil\n        },\/\/ \u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u8fd9\u4e2a\uff0c\u4f1a\u62a5\u9519\uff1assh: must specify HostKeyCallback\n    }\n\n    \/\/ connet to ssh\n    addr = fmt.Sprintf(\"%s:%d\", host, port)\n\n    if client, err = ssh.Dial(\"tcp\", addr, clientConfig); err != nil {\n        return nil, err\n    }\n\n    \/\/ create session\n    if session, err = client.NewSession(); err != nil {\n        return nil, err\n    }\n\n    return session, nil\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">func main() {\n    session, err := connect(\"\u7528\u6237\u540d\", \"\u5bc6\u7801\", \"IP\u5730\u5740\", \u8bbf\u95ee\u7aef\u53e3)\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer session.Close()\n\n    fd := int(os.Stdin.Fd())\n    oldState, err := terminal.MakeRaw(fd)\n    if err != nil {\n        panic(err)\n    }\n    defer terminal.Restore(fd, oldState)\n\n    \/\/ excute command\n    session.Stdout = os.Stdout\n    session.Stderr = os.Stderr\n    session.Stdin = os.Stdin\n\n    termWidth, termHeight, err := terminal.GetSize(fd)\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ Set up terminal modes\n    modes := ssh.TerminalModes{\n        ssh.ECHO:  1, \/\/ enable echoing\n        ssh.TTY_OP_ISPEED: 14400, \/\/ input speed = 14.4kbaud\n        ssh.TTY_OP_OSPEED: 14400, \/\/ output speed = 14.4kbaud\n    }\n\n    \/\/ Request pseudo terminal\n    if err := session.RequestPty(\"xterm-256color\", termHeight, termWidth, modes); err != nil {\n        log.Fatal(err)\n    }\n\n    session.Run(\"top\")\n}\n<\/code><\/pre>\n<p>\u6548\u679c\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.db2go.net\/wp-content\/uploads\/2022\/03\/wp_editor_md_85df01b92a2ae8aedb298556e1e8a633.jpg\"><img decoding=\"async\" src=\"https:\/\/www.db2go.net\/wp-content\/uploads\/2022\/03\/wp_editor_md_85df01b92a2ae8aedb298556e1e8a633.jpg\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u5efaconnect func connect(user, password, host string, por&hellip;<\/p>\n <a href=\"https:\/\/www.db2go.net\/?p=282\" title=\"\u4f7f\u7528ssh\u5305\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">\u4f7f\u7528ssh\u5305\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-suredandan","post-282","post","type-post","status-publish","format-standard","category-golang"],"views":1351,"_links":{"self":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=282"}],"version-history":[{"count":1,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":283,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/282\/revisions\/283"}],"wp:attachment":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}