PuTTY Go - Portable Usage & Go Language Integration
Discover how to use PuTTY on the go with portable installations and integrate SSH functionality into your Go (Golang) applications for powerful automation.
Overview of PuTTY-Go Usage
"PuTTY-Go" typically refers to two distinct use cases:
- Portable PuTTY: Running PuTTY without installation on any Windows system
- PuTTY with Go: Integrating SSH connections in Go applications using PuTTY tools or native Go libraries
Portable PuTTY: Running Without Installation
Portable PuTTY is ideal for system administrators, developers, and users who need SSH access on multiple machines without administrative privileges.
Setting Up Portable PuTTY
Step-by-Step Guide:
- Download PuTTY executables: Get putty.exe, plink.exe, and puttygen.exe from the official website
- Create a portable folder: Place all executables in a USB drive or cloud-synced folder (e.g., Dropbox, OneDrive)
- Store sessions: Export your saved sessions using the Registry export method or use configuration files
- Run anywhere: Simply double-click putty.exe on any Windows machine
Benefits of Portable PuTTY
- No installation required - works on locked-down corporate systems
- Consistent configuration across multiple machines
- Easy to backup and share settings
- Perfect for troubleshooting on different systems
PuTTY with Go (Golang)
Integrating SSH functionality into Go applications allows for powerful automation, remote management tools, and custom deployment scripts.
Using Plink with Go
You can call PuTTY's command-line tool (plink.exe) from Go using theos/exec
package:
package main
import (
"fmt"
"os/exec"
)
func main() {
// Execute SSH command using plink
cmd := exec.Command("plink.exe",
"-ssh",
"user@hostname",
"-pw", "password",
"ls -la")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Printf("Output:\n%s", output)
}
Using Native Go SSH Library (golang.org/x/crypto/ssh)
For better integration, use Go's native SSH library instead of external tools:
package main
import (
"fmt"
"log"
"golang.org/x/crypto/ssh"
)
func main() {
// SSH client configuration
config := &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{
ssh.Password("your-password"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Connect to SSH server
client, err := ssh.Dial("tcp", "hostname:22", config)
if err != nil {
log.Fatal("Failed to dial: ", err)
}
defer client.Close()
// Create session
session, err := client.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
}
defer session.Close()
// Run command
output, err := session.CombinedOutput("ls -la")
if err != nil {
log.Fatal("Failed to run command: ", err)
}
fmt.Printf("Output:\n%s", output)
}
Using SSH Keys with Go
For better security, use SSH key authentication instead of passwords:
package main
import (
"io/ioutil"
"log"
"golang.org/x/crypto/ssh"
)
func main() {
// Read private key file
key, err := ioutil.ReadFile("/path/to/private/key")
if err != nil {
log.Fatal("Unable to read private key: ", err)
}
// Parse private key
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatal("Unable to parse private key: ", err)
}
// SSH client configuration with key
config := &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Rest of connection code...
}
Security Tips for PuTTY-Go
Best Practices:
- Use SSH keys: Always prefer key-based authentication over passwords
- Secure key storage: Encrypt your private keys and store them securely
- Use Pageant: For portable setups, carry pageant.exe to manage keys in memory
- Implement HostKeyCallback: In Go, use proper host key verification instead of
InsecureIgnoreHostKey()
- Environment variables: Store sensitive credentials in environment variables, not in code
- Connection timeouts: Always implement timeout mechanisms to prevent hanging connections
Automation Tips
- Batch operations: Use Go to automate SSH connections to multiple servers
- Logging: Implement comprehensive logging for all SSH operations
- Error handling: Handle connection failures gracefully with retry logic
- Configuration files: Store server details in JSON/YAML configuration files
- Concurrent connections: Use Go goroutines for parallel SSH operations
Summary: When to Use PuTTY-Go
Use Portable PuTTY When:
- You need SSH access on multiple Windows machines
- Installation privileges are restricted
- You want consistent session configurations everywhere
- Working in environments where software installation is audited
Use Go with SSH When:
- Building automation tools or deployment scripts
- Creating custom remote management applications
- Implementing SSH functionality in microservices
- Need cross-platform SSH solutions (Windows, Linux, macOS)
- Requiring programmatic control over SSH sessions
Pro Tip: For the best of both worlds, create a Go application that uses the native SSH library and compile it as a portable executable. This gives you custom functionality with zero installation requirements!