Golang's SSH Package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

git clone https://github.com/golang/crypto.git, copy to the golang.org/x/directory.

Often used to establish an SSH connection to send a command, but sometimes need to impersonate the SSH client function, that is, directly connected to the remote host has been operating: (http://www.zhihu.com/question/30368538/answer/47835043)

Package Mainimport ("Golang.org/x/crypto/ssh"    "Log"    "OS"Func Main () {CE:= Func (err error, MSGstring) {        ifErr! =Nil {log. Fatalf ("%s Error:%v", MSG, err)} } client, err:= ssh. Dial ("TCP","172.16.116.200:22", &ssh. clientconfig{User:"Root", Auth: []ssh. Authmethod{ssh. Password ("^two^ten=1024$")}, 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 modes:=ssh. terminalmodes{ssh. ECHO:0, SSH. Tty_op_ispeed:14400, SSH. Tty_op_ospeed:14400,} Err= Session. Requestpty ("xterm", -, the, modes) CE (err,"Request Pty") Err=session. Shell () CE (err,"Start Shell") Err=session. Wait () CE (err,"return")}

The actual use of defects, through the tab completion is not displayed correctly. or use the shell directly to write it, simple and practical:

#!/usr/bin/ssh root@122.116. 16.200  "password: " "123456\r"  Interact

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.