helloyi / go-sshclient Goto Github PK
View Code? Open in Web Editor NEWsimple sshclient with go
License: MIT License
simple sshclient with go
License: MIT License
When using go get -u github.com/helloyi/go-sshclient
, it always return v1.1.1 version, According to semantic version spec if major version greater than v1, it should append to go module name,So module github.com/helloyi/go-sshclient should change to module github.com/helloyi/go-sshclient/v2 in v2.x.
Lines 29 to 31 in 5bb6d0b
How I get the *ssh.Client? Thanks very much~
Hi
Can you add license file to this one (example MIT) so I can use this code as part of my other projects?
Thanks :)
I'm trying to connect to an ssh server with an insecure connection, I can even configure some things but it always gives me an error [no password].
No SSH Config
Error:
2024/01/03 08:54:21 ssh: handshake failed: ssh: no common algorithm for key exch
ange; client offered: [curve25519-sha256 [email protected] ecdh-sha2-
nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 diffie-hellman-group14-sha256 dif
fie-hellman-group14-sha1 ext-info-c [email protected]], server offere
d: [diffie-hellman-group1-sha1]
With SSH Config
Error:
2024/01/03 08:52:58 ssh: handshake failed: ssh: unable to authenticate, attempte
d methods [none password], no supported methods remain
Awesome & Useful library
Please add a installation guide! :)
It would be nice if this project supported proxy servers.
For example, if you're connecting through a bastion or a jump box, to have that as an option.
Something like,
sshclient.DialWithProxy("host:port", "username", "pkey", "proxyhost:port", "proxy-username", "proxy-pkey")
or something like that.
I was new and didn't know how to submit my code to the repository and create a Pull request.
I have tried to submit, but the error told me that I did not have permission.
The code I want to change is as follows:
// The timeout period parameter is added
func DialWithPasswd(addr, user, passwd string, timeout time.Duration) (*Client, error) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(passwd),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
Timeout: timeout, // The timeout period parameter is added
}
return Dial("tcp", addr, config)
}
// DialWithKey starts a client connection to the given SSH server with key authmethod.
// The timeout period parameter is added
func DialWithKey(addr, user, keyfile string, timeout time.Duration) (*Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
Timeout: timeout, // The timeout period parameter is added
}
return Dial("tcp", addr, config)
}
The other two functions I'd like to add are Runshell(when executing a Runshell function, the command is executed directly after cmd is passed in, which I prefer to do) and CheckConnection(assuming I have a list of ip addresses). I want to check that the ssh connection can be established before executing a series of commands on these IP addresses. If not, skip this ip address.)
code:
type ClinetInfo struct {
Ip string
Port string
Username string
PrikeyFile string
}
func Runshell(cli ClinetInfo, cmd string) (stdout, stderr bytes.Buffer) {
addr := fmt.Sprintf("%v:%v", cli.Ip, cli.Port)
client, err := DialWithKey(addr, cli.Username, cli.PrikeyFile, time.Second*3)
defer client.Close()
utils.CheckErr(err)
client.Cmd(cmd).SetStdio(&stdout, &stderr).Run()
return stdout, stderr
}
func CheckConnection(cli ClinetInfo) (ok bool, err error) {
ok = true
addr := fmt.Sprintf("%v:%v", cli.Ip, cli.Port)
client, err := DialWithKey(addr, cli.Username, cli.PrikeyFile, time.Second*3)
if err != nil {
ok = false
} else {
client.Close()
}
return
}
There's a typo in the following code, it should be "Height" instead of "Hight"
type TerminalConfig struct {
Term string
Hight int
Weight int
Modes ssh.TerminalModes
}
How do you upload files through a connection?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.