init
This commit is contained in:
commit
6e51a7cc40
10
go.mod
Normal file
10
go.mod
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module sshfs_connect
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
|
|
||||||
|
require github.com/kevinburke/ssh_config v1.2.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2 // indirect
|
||||||
|
golang.org/x/sys v0.1.0 // indirect
|
||||||
|
)
|
||||||
6
go.sum
Normal file
6
go.sum
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
|
||||||
|
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
|
||||||
|
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
|
||||||
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
85
main.go
Normal file
85
main.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"github.com/moby/sys/mountinfo"
|
||||||
|
"github.com/kevinburke/ssh_config"
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
var eFlag = flag.Bool("e", false, "open mountpoint in your editor")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
args := flag.Args()
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Println("No hostname specified.")
|
||||||
|
os.Exit(80)
|
||||||
|
} else {
|
||||||
|
hostname := ssh_config.Get(args[0], "HostName")
|
||||||
|
user := ssh_config.Get(args[0], "User")
|
||||||
|
port := ssh_config.Get(args[0], "Port")
|
||||||
|
ifile := ssh_config.Get(args[0], "IdentityFile")
|
||||||
|
|
||||||
|
if len(hostname) == 0 || len(user) == 0 || len(ifile) == 0 {
|
||||||
|
fmt.Println("Hostname not found in ~/.ssh_config")
|
||||||
|
os.Exit(3)
|
||||||
|
} else {
|
||||||
|
mount := verify_mount_dir(hostname)
|
||||||
|
|
||||||
|
fmt.Println("Hostname: ",hostname)
|
||||||
|
fmt.Println("User: ", user)
|
||||||
|
fmt.Println("Port: ", port)
|
||||||
|
fmt.Println("Ifile: ", ifile)
|
||||||
|
fmt.Println("Mount: ", mount)
|
||||||
|
fmt.Println("---")
|
||||||
|
|
||||||
|
chkmount, chkmount_err := mountinfo.Mounted(mount)
|
||||||
|
if chkmount_err != nil {
|
||||||
|
fmt.Println("mountinfo.Mounted() failed with %s\n", chkmount_err)
|
||||||
|
}
|
||||||
|
if chkmount == false {
|
||||||
|
mount_sshfs(hostname, user, ifile, port, mount)
|
||||||
|
} else {
|
||||||
|
fmt.Println("!!! Already mounted")
|
||||||
|
}
|
||||||
|
run_editor(mount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run_editor(mount string) {
|
||||||
|
if(*eFlag == true) {
|
||||||
|
cmd := exec.Command("subl", mount)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("run_editor() failed with\n",err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func verify_mount_dir(hostname string)(mount string) {
|
||||||
|
homedir, homedirerr := os.UserHomeDir()
|
||||||
|
if homedirerr != nil {
|
||||||
|
fmt.Println( homedirerr )
|
||||||
|
}
|
||||||
|
mount = homedir+"/Servers/"+hostname
|
||||||
|
os.MkdirAll(mount, os.ModePerm)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func mount_sshfs(hostname string, user string, ifile string, port string, mount string) {
|
||||||
|
cmd := exec.Command("sshfs", "-p", port, "-o", "IdentityFile="+ifile+",idmap=user,dir_cache=no", user+"@"+hostname+":/", mount)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("mount_sshfs() failed with\n",err)
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
sshfs_connect
Executable file
BIN
sshfs_connect
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user