http://localhost:7999/ftpopen?path/ にアクセスすると
WinSCP.exe が ftp://user4:pass@192.168.100.70/path/ を開くサンプル。
ftpopen.go
package main
import (
"fmt"
"net/http"
"runtime"
"os/exec"
"time"
)
func ftpopen(w http.ResponseWriter, r *http.Request) {
var path = r.FormValue("path")
fmt.Println(time.Now().Format("15:04:05") +" "+ path)
if runtime.GOOS == "windows" {
exec.Command("winscp.exe", "ftp://user4:pass@192.168.100.70/"+path).Run()
} else {
exec.Command("pcmanfm", "ftp://192.168.100.70/"+path).Run()
}
}
func main() {
http.HandleFunc("/ftpopen", ftpopen)
fmt.Println("Listening port 7999")
http.ListenAndServe("127.0.0.1:7999", nil)
}
コンパイル
GOOS=windows GOARCH=386 go build ftpopen.go
ユーザ名をaccount.txtに記述する場合
account.txt
user4
fileread/fileread.go
package fileread
import (
"bufio"
"fmt"
"os"
)
func Get_user() string {
var fp *os.File
fp, _ = os.Open("account.txt")
defer fp.Close()
scanner := bufio.NewScanner(fp)
var username string
for scanner.Scan() {
username = scanner.Text()
fmt.Print(username)
}
return username
}
ftpopen.go
package main
import (
"./fileread"
"fmt"
"net/http"
"os/exec"
"runtime"
"time"
)
func ftpopen(w http.ResponseWriter, r *http.Request) {
var path = r.FormValue("path")
var user = fileread.Get_user()
fmt.Println(" "+time.Now().Format("15:04:05") + " " + path)
if runtime.GOOS == "windows" {
exec.Command("winscp.exe", "ftp://"+user+":pass@192.168.100.70/"+path).Run()
} else {
exec.Command("pcmanfm", "ftp://192.168.100.70/"+path).Run()
}
}
func main() {
http.HandleFunc("/ftpopen", ftpopen)
fmt.Println("Listening port 7999")
http.ListenAndServe("127.0.0.1:7999", nil)
}
explorerを開く場合
package main
import (
"fmt"
"net/http"
"runtime"
"os/exec"
"time"
)
func explorer(w http.ResponseWriter, r *http.Request) {
var path = r.FormValue("path")
fmt.Println(time.Now().Format("15:04:05") +" "+ path)
if runtime.GOOS == "windows" {
// explorerを複数開かないようにする
exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", path).Run()
} else {
exec.Command("pcmanfm", "smb://192.168.100.70/"+path).Run()
}
}
func main() {
http.HandleFunc("/open", explorer)
fmt.Println("Listening port 7999")
// chrome起動 (並列に実行)
// go exec.Command("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "http://google.co.jp").Run()
http.ListenAndServe("127.0.0.1:7999", nil)
}