Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ssh package of golang

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use golang's ssh package". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use golang's ssh package.

In addition to finding a lot of problems with the gexpect package, here we try to use ssh user@127.0.0.1 's way of thinking for user switching. Here we record the specific usage, the solution to the ssh: must specify HostKeyCallback problem encountered and the problems in the final use process.

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.

2. The 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.1 golang.org/x/crypto/ssh 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: 0Med. TTYOSPEED: 14400, err = session.RequestPty ("linux", 32,160, modes) ce (err) Request pty) err = session.Shell () ce (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 to nil without verification. Click HostKeyCallback to see the source code of HostKeyCallback: func (hostname string, remote net.Addr, key ssh.PublicKey) error {return nil}. Thank you for reading. The above is the content of "how to use golang's ssh package". After the study of this article, I believe you have a deeper understanding of how to use golang's ssh package, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report