In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use the ssh package in golang. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Installation of ssh package crypto
The package used by ssh is "golang.org/x/crypto/ssh". Because golang.org is walled, the mirrored version on github is used here, so you can't use go get for installation here (don't use go get, don't use go get, don't use go get). After using git clone https://github.com/golang/crypto.git, put it under the $GOPATH/src/golang.org/x/ directory.
II. Ssh login code
Package mainimport ("golang.org/x/crypto/ssh"log"os") func main () {ce: = func (err error, msg string) {if err! = nil {log.Fatalf ("% s error:% v", msg, err)}} client, err: = ssh.Dial ("tcp", "127.0.0.1 log 22", & ssh.ClientConfig {User: "zabbix", Auth: [] ssh.AuthMethod {ssh.Password ("123456")}, HostKeyCallback: ssh.InsecureIgnoreHostKey () }) ce (err, "dial") session, err: = client.NewSession () ce (err, "new session") defer session.Close () session.Stdout = os.Stdoutsession.Stderr = os.Stderrsession.Stdin = os.Stdinmodes: = ssh.TerminalModes {ssh.ECHO: 0mlsh.TTYOSPEED: 14400, err = session.RequestPty ("linux", 32,160, modes) ce (err, "request pty") err = session.Shell (err) "start shell") err = session.Wait () ce (err, "return")}
After the code execution, it is found that the corresponding user can be switched normally, and the problem of repeated output of the command is also solved, but tab can not complete the command, and the problem that ps auxf view cannot be full-screen still exists.
3. Ssh: must specify HostKeyCallback error resolution
You may encounter a ssh: must specify HostKeyCallback error when executing the above code, which occurs because the following line is not added:
HostKeyCallback: ssh.InsecureIgnoreHostKey ()
In addition to adding the above code to solve the problem, you can also add the following code:
/ / you need to verify the server. You can return nil without verification. Click HostKeyCallback to see the source code of HostKeyCallback: func (hostname string, remote net.Addr, key ssh.PublicKey) error {return nil}. The above is how to use the ssh package in golang. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.