Skip to content

CreateAuthMethodAgent and an examples of its use #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
# vscode
.vscode

# Binaries for programs and plugins
.vscode/
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

*.DS_Store
*.sublime-*

_TEST
_test
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
go-sshlib
====

[![GoDoc](https://godoc.org/github.com/blacknon/go-sshlib?status.svg)](https://godoc.org/github.com/blacknon/go-sshlib)
[![GoDoc](https://godoc.org/github.com/abakum/go-sshlib?status.svg)](https://godoc.org/github.com/abakum/go-sshlib)

## About

Expand All @@ -14,11 +14,11 @@ If use **pkcs11** authentication, cgo must be enabled.

## Usage

[See GoDoc reference.](https://godoc.org/github.com/blacknon/go-sshlib)
[See GoDoc reference.](https://godoc.org/github.com/abakum/go-sshlib)

## Download

GO111MODULE=on go get github.com/blacknon/go-sshlib
GO111MODULE=on go get github.com/abakum/go-sshlib

## Example

Expand All @@ -37,7 +37,7 @@ If use **pkcs11** authentication, cgo must be enabled.
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
sshlib "github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand All @@ -64,7 +64,7 @@ If use **pkcs11** authentication, cgo must be enabled.
// Create ssh.AuthMethod
authMethod := sshlib.CreateAuthMethodPassword(password)

// If you use ssh-agent forwarding, uncomment it.
// If you use ssh-agent, uncomment it.
// con.ConnectSshAgent()

// Connect ssh server
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.12
62 changes: 62 additions & 0 deletions _example/example_cmdshell_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2020 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

// Shell connection Example file.
// Change the value of the variable and compile to make sure that you can actually connect.
//

package main

import (
"fmt"
"os"

"github.com/abakum/go-sshlib"
)

var (
// dropbear on linux
// host = "10.161.115.160"
// port = "22"
// user = "root"
// command = "ssh user_@10.161.115.189"

// sshd of OpenSSH on Windows
// host = "10.161.115.189"
// port = "22"
// user = "user_"
// command = "ssh root@10.161.115.160"

// sshd of gliderlabs on Windows
host = "10.161.115.189"
port = "2222"
user = "user_"
command = "ssh root@10.161.115.160"
)

func main() {
// Create sshlib.Connect
con := &sshlib.Connect{
// If you use x11 forwarding, please uncomment next line.
// ForwardX11: true,

// If you use ssh-agent forwarding, please set to true.
// And after, run `con.ConnectSshAgent()`.
ForwardAgent: true,
}

// setup con.Agent for use ssh-agent
con.ConnectSshAgent()

// Connect ssh server
// set authMethods to nil for use ssh-agent
err := con.CreateClient(host, port, user, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Start ssh shell with command
con.CmdShell(nil, command)
}
2 changes: 1 addition & 1 deletion _example/example_dynamic_forward_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
6 changes: 3 additions & 3 deletions _example/example_multiproxy_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -59,8 +59,8 @@ func main() {

socks5Proxy := &sshlib.Proxy{
Type: "socks5",
Addr: httpProxyHost,
Port: httpProxyPort,
Addr: socks5ProxyHost,
Port: socks5ProxyPort,
Forwarder: httpProxyDialer,
}
socks5ProxyDialer, err := httpProxy.CreateProxyDialer()
Expand Down
2 changes: 1 addition & 1 deletion _example/example_portforward_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
2 changes: 1 addition & 1 deletion _example/example_proxycmd_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
2 changes: 1 addition & 1 deletion _example/example_reverse_dynamic_forward_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
2 changes: 1 addition & 1 deletion _example/example_reverse_portforward_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
sshlib "github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
27 changes: 12 additions & 15 deletions _example/example_shell_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

var (
host = "target.com"
port = "22"
user = "user"
// host = "10.161.115.160"
// port = "22"
// user = "root"
host = "10.161.115.189"
port = "2222"
user = "user_"
password = "password"

termlog = "./test_termlog"
Expand All @@ -40,8 +43,8 @@ func main() {
// Create ssh.AuthMethod
authMethod := sshlib.CreateAuthMethodPassword(password)

// If you use ssh-agent forwarding, uncomment it.
// con.ConnectSshAgent()
// If you use ssh-agent, uncomment it.
con.ConnectSshAgent()

// Connect ssh server
err := con.CreateClient(host, port, user, []ssh.AuthMethod{authMethod})
Expand All @@ -51,15 +54,9 @@ func main() {
}

// Set terminal log
con.SetLog(termlog, false)

// Create Session
session, err := con.CreateSession()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// con.SetLog(termlog, true)
con.SetLogWithRemoveAnsiCode(termlog, false)

// Start ssh shell
con.Shell(session)
con.Shell(nil)
}
10 changes: 5 additions & 5 deletions _example/example_shell_pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
)

var (
Expand All @@ -34,17 +34,17 @@ func main() {
}

// Create ssh.AuthMethod
authMethod, err := sshlib.CreateAuthMethodPKCS11("/usr/local/opt/opensc/lib/opensc-pkcs11.so", "")
authMethods, err := sshlib.CreateAuthMethodPKCS11("/usr/local/opt/opensc/lib/opensc-pkcs11.so", "")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// If you use ssh-agent forwarding, uncomment it.
// con.ConnectSshAgent()
// If you use ssh-agent, uncomment it.
con.ConnectSshAgent()

// Connect ssh server
err = con.CreateClient(host, port, user, authMethod)
err = con.CreateClient(host, port, user, authMethods)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions _example/example_shell_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func main() {
os.Exit(1)
}

// If you use ssh-agent forwarding, uncomment it.
// If you use ssh-agent, uncomment it.
// con.ConnectSshAgent()

// Connect ssh server
Expand Down
86 changes: 86 additions & 0 deletions _example/example_sshproxy_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2020 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

// Shell connection Example file.
// Change the value of the variable and compile to make sure that you can actually connect.
//
// This file has a simple ssh proxy connection.
// Also, the authentication method is agent authentication.
// Please replace as appropriate.

package main

import (
"fmt"
"os"

"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

var (
// ssh -J user@proxy.com user@target.com
// Proxy ssh server
// host1 = "proxy.com"
// port1 = "22"
// user1 = "user"

// sshd of OpenSSH on Windows
host1 = "10.161.115.189"
port1 = "2222"
user1 = "user_"

// Target ssh server
// host2 = "target.com"
// port2 = "22"
// user2 = "user"

// dropbear on linux
host2 = "10.161.115.160"
port2 = "22"
user2 = "root"
)

func main() {
// ==========
// proxy connect
// ==========

// Create proxy sshlib.Connect
proxyCon := &sshlib.Connect{}

// // Connect to ssh-agent
// proxyCon.ConnectSshAgent()

// Create ssh.AuthMethod from ssh-agent for target
AuthMethod := proxyCon.CreateAuthMethodAgent()

// Connect proxy server
err := proxyCon.CreateClient(host1, port1, user1, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// ==========
// target connect
// ==========

// Create target sshlib.Connect
targetCon := &sshlib.Connect{
ProxyDialer: proxyCon.Client,
}

// Connect target server
err = targetCon.CreateClient(host2, port2, user2, []ssh.AuthMethod{AuthMethod})
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Set terminal log
// targetCon.SetLog(termlog, false)

targetCon.Shell(nil)
}
2 changes: 1 addition & 1 deletion _example/example_sshproxy_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"fmt"
"os"

sshlib "github.com/blacknon/go-sshlib"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)

Expand Down
Loading