Skip to content
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
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:latest
LABEL maintainer="Fei ye"

# install gcc make git
RUN apt-get update \
&& apt-get install -y build-essential git xz-utils \
&& git clone git://github.com/kongjian/tsar.git \
&& cd tsar \
&& make \
&& make install

CMD [ "top" ]
9 changes: 9 additions & 0 deletions cobra-test.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
18 changes: 18 additions & 0 deletions exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package exec

import "fmt"

func Cpu() {

fmt.Print("cpu")
}

func mem() {

fmt.Print("mem")
}

func Disk() {

fmt.Print("mem")
}
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/tiny-x/go-study

go 1.14

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/chaosblade-io/chaosblade-exec-docker v0.6.1-0.20200805071438-6d16a1434e52
github.com/chaosblade-io/chaosblade-exec-os v0.7.0
github.com/chaosblade-io/chaosblade-spec-go v0.7.0
github.com/gorilla/mux v1.8.0 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
github.com/spf13/cobra v1.0.0
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975
gotest.tools v2.2.0+incompatible // indirect
)

replace github.com/chaosblade-io/chaosblade-exec-os v0.7.0 => /Users/Shared/ChaosBladeProjects/chaosblade-opensource/chaosblade-exec-os
219 changes: 219 additions & 0 deletions go.sum

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions test/LocalChannel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"time"
)

func main() {
//localChannel:= channel.NewLocalChannel()
//ctx := context.Background()
//// 931001Lqh_
//response := localChannel.Run(ctx, "", "ssh root@118.24.31.95 'ls'")
//fmt.Println(response)

session, err := connect("root", "931001Lqh_", "101.37.30.161", "", 22, []string{})
if err != nil {
fmt.Println(err)
return
}
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Run("ls")
err = session.Run("ls")
fmt.Println(stdoutBuf.String())
fmt.Println(err.Error())
}

func connect(user, password, host, key string, port int, cipherList []string) (*ssh.Session, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
client *ssh.Client
config ssh.Config
session *ssh.Session
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
if key == "" {
auth = append(auth, ssh.Password(password))
} else {
pemBytes, err := ioutil.ReadFile(key)
if err != nil {
return nil, err
}

var signer ssh.Signer
if password == "" {
signer, err = ssh.ParsePrivateKey(pemBytes)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(password))
}
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(signer))
}

if len(cipherList) == 0 {
config = ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
} else {
config = ssh.Config{
Ciphers: cipherList,
}
}

clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
Config: config,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}

// connet to ssh
addr = fmt.Sprintf("%s:%d", host, port)

if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}

// create session
if session, err = client.NewSession(); err != nil {
return nil, err
}

modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}

if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
return nil, err
}

return session, nil
}
21 changes: 21 additions & 0 deletions test/PidTest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/chaosblade-io/chaosblade-spec-go/util"
)

func main() {
name := "/Users"
exist := util.IsDir(name)
fmt.Println(exist)

}

func md5Hex(s string) string {
m := md5.New()
m.Write([]byte(s))
return hex.EncodeToString(m.Sum(nil))
}
35 changes: 35 additions & 0 deletions test/StringTest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"regexp"
"strings"
)

func main() {

//目标字符串
searchIn := `
./blade create file append --filepath ~/chaosblade/mock.log
--content "\@{date '+%Y-%m-%d %H:%M\:%S'\}} @{date '+%Y-%m-%d %H:%M:%S' @{\n} {@{x\\\x} @{date '+%Y-%m-%d %H:%M:%Sxx'}hell world"
--count 2 --interval=2 @[RANDOM * 10] \@JAVA_HOME`

//解释正则表达式
reg := regexp.MustCompile(`\\?@\{(?s:([^(@{})]*[^\\]))\}|\\?@\[((?s:[^(@{})]*[^\\]))\]|\\?@\w+`)

//提取关键信息
result := reg.FindAllStringSubmatch(searchIn, -1)

//过滤<></>
for _, text := range result {
fmt.Println("text[o] = ", text[0])
fmt.Println("text[1] = ", text[1])
if strings.HasPrefix(text[0], "\\@") {
searchIn = strings.Replace(searchIn, text[0], text[0][1 : len(text[0]) - 1], 1)
continue
}
}

fmt.Println(searchIn)

}
1 change: 1 addition & 0 deletions test/StringTest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
29 changes: 29 additions & 0 deletions test/Test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)

func main() {

ticker := time.NewTicker(time.Second * 5)
go func() {
fmt.Printf("ticked at %v", time.Now())
for _ = range ticker.C {
fmt.Printf("ticked at %v", time.Now())
}
}()
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL)
for s := range ch {
switch s {
case syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL, os.Interrupt:
fmt.Println("caught interrupt, exit")
return
}
}
}
22 changes: 22 additions & 0 deletions test/a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>

int main()
{
struct passwd *pw;

if( ( pw = getpwuid( getuid() ) ) == NULL ) {
fprintf( stderr,
"getpwuid: no password entry\n" );
exit( EXIT_FAILURE );
}
printf( "login name %s\n", pw->pw_name );
printf( "user ID %d\n", pw->pw_uid );
printf( "group ID %d\n", pw->pw_gid );
printf( "home dir %s\n", pw->pw_dir );
printf( "login shell %s\n", pw->pw_shell );
exit( EXIT_SUCCESS );
}
35 changes: 35 additions & 0 deletions test/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import "C"
import (
"fmt"
"github.com/howeyc/gopass"
"github.com/spf13/cobra"
"os"
"strings"
)

func main() {
var rootCmd = &cobra.Command{
Use: "hugo",
Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Password: ")
passwd, err := gopass.GetPasswd()
fmt.Print(err)
text := strings.Replace(string(passwd), "\n", "", -1)

fmt.Println(text)
return nil
},
}

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}

}
6 changes: 6 additions & 0 deletions test/exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {

panic("exception")
}
25 changes: 25 additions & 0 deletions test/exec-docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"
"github.com/chaosblade-io/chaosblade-exec-docker/exec"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

func main() {

executor := exec.NewNetWorkSidecarExecutor()

executor.Exec("abc", context.Background(), &spec.ExpModel{
Target: "network",
ActionName: "delay",
ActionFlags: map[string]string{
"time": "3000",
"interface": "eth0",
"local-port": "8080",
"container-id": "a500c599e284",
"debug": "true",
},
})

}
29 changes: 29 additions & 0 deletions test/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-exec-os/exec"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

func main() {
executor := exec.NewSSHExecutor()
response := executor.Exec(
"abcde",
context.Background(),
&spec.ExpModel{
Target: "cpu",
ActionName: "load",
ActionFlags: map[string]string{
"channel": "ssh",
"ssh-host": "101.37.30.161",
"ssh-user": "root",
"ssh-key": "/Users/yefei/.ssh/hg.pem",
"ssh-key-passphrase": "true",
"debug": "true",
},
},
)
fmt.Print(response.ToString())
}
Loading