Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

sshkit/rake in Golang

NotificationsYou must be signed in to change notification settings

zhiminwen/magetool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Download and Import

go get github.com/zhiminwen/magetool
import ("github.com/zhiminwen/magetool/shellkit""github.com/zhiminwen/magetool/sshkit")

Synopsis

Mage

UseMage to organize the tasks together

// +build magepackage mainimport ("os""github.com/zhiminwen/magetool/shellkit")funcinit() {os.Setenv("MAGEFILE_VERBOSE","true")}//Run CmdfuncRunCmd() {shellkit.Execute("cmd","/c","type test.log & sleep 10")}//dirfuncDir() {shellkit.Execute("cmd","/c","time /t 1>&2 & exit 1")}

Shellkit

  • Use Shellkit.Execute() to run local command.
  • Use Shellkit.Capture to capture command output.

SSHkit

Init the client

//Use passwordclient1:=sshkit.NewSSHClient("192.168.5.10","22","ubuntu","password","")//Use private keys2:=sshkit.NewSSHClient("192.168.5.10","22","ubuntu","","mykeyfile"// Use a slice of the clientvarservers []*sshkit.SSHClientservers=append(servers,sshkit.NewSSHClient("192.168.5.10","22","ubuntu","password",""))servers=append(servers,sshkit.NewSSHClient("192.168.5.10","22","ubuntu2","password",""))

Execute a command

// Run against a single clientclient1.Execute("id; hostname")//Run against slice of serverssshkit.Execute(servers,"id; hostname")

Capture a result

result,err:=client.Capture("hostname")
sshkit.ExecuteFunc(servers,func(t*sshkit.SSHClient) {id,err:=t.Capture("id -u")iferr!=nil {log.Printf("Failed to get id:%v",err)return  }ifid=="1000" {return  }cmd:=fmt.Sprintf("echo %s; hostname",id)t.Execute(cmd) })

Upload and Download

client1.Upload("example.txt","/tmp/example.txt")client1.Download("/etc/hosts","hosts.txt")
client1.Put(`This is a test content`, "mytest.txt")list, err := servers[0].Get("/etc/passwd")

Interact with command

client1.Execute(`rm -rf .ssh/known_hosts`)client1.ExecuteInteractively("scp ubuntu@ubuntu:/tmp/test.txt /tmp/test.txt.dupped",map[string]string{`\(yes/no\)`:"yes","password":"password", })

Full example to build a docker image and push to dockerhub

Source Code

About

sshkit/rake in Golang

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp