How to use ssh method of isolated Package

Best Syzkaller code snippet using isolated.ssh

isolated.go

Source:isolated.go Github

copy

Full Screen

...30 cfg *Config31 target string32 closed chan bool33 debug bool34 sshkey string35 port int36}37func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {38 cfg := &Config{}39 if err := config.LoadData(env.Config, cfg); err != nil {40 return nil, err41 }42 if len(cfg.Targets) == 0 {43 return nil, fmt.Errorf("config param targets is empty")44 }45 if cfg.Target_Dir == "" {46 return nil, fmt.Errorf("config param target_dir is empty")47 }48 // sshkey is optional49 if env.SshKey != "" && !osutil.IsExist(env.SshKey) {50 return nil, fmt.Errorf("ssh key '%v' does not exist", env.SshKey)51 }52 if env.Debug {53 cfg.Targets = cfg.Targets[:1]54 }55 pool := &Pool{56 cfg: cfg,57 env: env,58 }59 return pool, nil60}61func (pool *Pool) Count() int {62 return len(pool.cfg.Targets)63}64func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {65 inst := &instance{66 cfg: pool.cfg,67 target: pool.env.SshUser + "@" + pool.cfg.Targets[index],68 closed: make(chan bool),69 debug: pool.env.Debug,70 sshkey: pool.env.SshKey,71 }72 closeInst := inst73 defer func() {74 if closeInst != nil {75 closeInst.Close()76 }77 }()78 if err := inst.repair(); err != nil {79 return nil, err80 }81 // Create working dir if doesn't exist.82 inst.ssh("mkdir -p '" + inst.cfg.Target_Dir + "'")83 // Remove temp files from previous runs.84 inst.ssh("rm -rf '" + filepath.Join(inst.cfg.Target_Dir, "*") + "'")85 closeInst = nil86 return inst, nil87}88func (inst *instance) Forward(port int) (string, error) {89 if inst.port != 0 {90 return "", fmt.Errorf("isolated: Forward port already set")91 }92 if port == 0 {93 return "", fmt.Errorf("isolated: Forward port is zero")94 }95 inst.port = port96 return fmt.Sprintf("127.0.0.1:%v", port), nil97}98func (inst *instance) ssh(command string) ([]byte, error) {99 if inst.debug {100 Logf(0, "executing ssh %+v", command)101 }102 rpipe, wpipe, err := osutil.LongPipe()103 if err != nil {104 return nil, err105 }106 args := append(inst.sshArgs("-p"), inst.target, command)107 if inst.debug {108 Logf(0, "running command: ssh %#v", args)109 }110 cmd := exec.Command("ssh", args...)111 cmd.Stdout = wpipe112 cmd.Stderr = wpipe113 if err := cmd.Start(); err != nil {114 wpipe.Close()115 return nil, err116 }117 wpipe.Close()118 done := make(chan bool)119 go func() {120 select {121 case <-time.After(time.Second * 30):122 if inst.debug {123 Logf(0, "ssh hanged")124 }125 cmd.Process.Kill()126 case <-done:127 }128 }()129 if err := cmd.Wait(); err != nil {130 close(done)131 out, _ := ioutil.ReadAll(rpipe)132 if inst.debug {133 Logf(0, "ssh failed: %v\n%s", err, out)134 }135 return nil, fmt.Errorf("ssh %+v failed: %v\n%s", args, err, out)136 }137 close(done)138 if inst.debug {139 Logf(0, "ssh returned")140 }141 out, _ := ioutil.ReadAll(rpipe)142 return out, nil143}144func (inst *instance) repair() error {145 Logf(2, "isolated: trying to ssh")146 if err := inst.waitForSsh(30 * 60); err == nil {147 if inst.cfg.Target_Reboot == true {148 Logf(2, "isolated: trying to reboot")149 inst.ssh("reboot") // reboot will return an error, ignore it150 if err := inst.waitForReboot(5 * 60); err != nil {151 Logf(2, "isolated: machine did not reboot")152 return err153 }154 Logf(2, "isolated: rebooted wait for comeback")155 if err := inst.waitForSsh(30 * 60); err != nil {156 Logf(2, "isolated: machine did not comeback")157 return err158 }159 Logf(2, "isolated: reboot succeeded")160 } else {161 Logf(2, "isolated: ssh succeeded")162 }163 } else {164 Logf(2, "isolated: ssh failed")165 return fmt.Errorf("SSH failed")166 }167 return nil168}169func (inst *instance) waitForSsh(timeout int) error {170 var err error171 start := time.Now()172 for {173 if !vmimpl.SleepInterruptible(time.Second) {174 return fmt.Errorf("shutdown in progress")175 }176 if _, err = inst.ssh("pwd"); err == nil {177 return nil178 }179 if time.Since(start).Seconds() > float64(timeout) {180 break181 }182 }183 return fmt.Errorf("isolated: instance is dead and unrepairable: %v", err)184}185func (inst *instance) waitForReboot(timeout int) error {186 var err error187 start := time.Now()188 for {189 if !vmimpl.SleepInterruptible(time.Second) {190 return fmt.Errorf("shutdown in progress")191 }192 // If it fails, then the reboot started193 if _, err = inst.ssh("pwd"); err != nil {194 return nil195 }196 if time.Since(start).Seconds() > float64(timeout) {197 break198 }199 }200 return fmt.Errorf("isolated: the machine did not reboot on repair")201}202func (inst *instance) Close() {203 close(inst.closed)204}205func (inst *instance) Copy(hostSrc string) (string, error) {206 baseName := filepath.Base(hostSrc)207 vmDst := filepath.Join(inst.cfg.Target_Dir, baseName)208 inst.ssh("pkill -9 '" + baseName + "'; rm -f '" + vmDst + "'")209 args := append(inst.sshArgs("-P"), hostSrc, inst.target+":"+vmDst)210 cmd := exec.Command("scp", args...)211 if inst.debug {212 Logf(0, "running command: scp %#v", args)213 cmd.Stdout = os.Stdout214 cmd.Stderr = os.Stdout215 }216 if err := cmd.Start(); err != nil {217 return "", err218 }219 done := make(chan bool)220 go func() {221 select {222 case <-time.After(3 * time.Minute):223 cmd.Process.Kill()224 case <-done:225 }226 }()227 err := cmd.Wait()228 close(done)229 if err != nil {230 return "", err231 }232 return vmDst, nil233}234func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (<-chan []byte, <-chan error, error) {235 args := append(inst.sshArgs("-p"), inst.target)236 dmesg, err := vmimpl.OpenRemoteConsole("ssh", args...)237 if err != nil {238 return nil, nil, err239 }240 rpipe, wpipe, err := osutil.LongPipe()241 if err != nil {242 dmesg.Close()243 return nil, nil, err244 }245 args = inst.sshArgs("-p")246 // Forward target port as part of the ssh connection (reverse proxy)247 if inst.port != 0 {248 proxy := fmt.Sprintf("%v:127.0.0.1:%v", inst.port, inst.port)249 args = append(args, "-R", proxy)250 }251 args = append(args, inst.target, "cd "+inst.cfg.Target_Dir+" && exec "+command)252 Logf(0, "running command: ssh %#v", args)253 if inst.debug {254 Logf(0, "running command: ssh %#v", args)255 }256 cmd := exec.Command("ssh", args...)257 cmd.Stdout = wpipe258 cmd.Stderr = wpipe259 if err := cmd.Start(); err != nil {260 dmesg.Close()261 rpipe.Close()262 wpipe.Close()263 return nil, nil, err264 }265 wpipe.Close()266 var tee io.Writer267 if inst.debug {268 tee = os.Stdout269 }270 merger := vmimpl.NewOutputMerger(tee)271 merger.Add("dmesg", dmesg)272 merger.Add("ssh", rpipe)273 errc := make(chan error, 1)274 signal := func(err error) {275 select {276 case errc <- err:277 default:278 }279 }280 go func() {281 select {282 case <-time.After(timeout):283 signal(vmimpl.TimeoutErr)284 case <-stop:285 signal(vmimpl.TimeoutErr)286 case <-inst.closed:287 if inst.debug {288 Logf(0, "instance closed")289 }290 signal(fmt.Errorf("instance closed"))291 case err := <-merger.Err:292 cmd.Process.Kill()293 dmesg.Close()294 merger.Wait()295 if cmdErr := cmd.Wait(); cmdErr == nil {296 // If the command exited successfully, we got EOF error from merger.297 // But in this case no error has happened and the EOF is expected.298 err = nil299 }300 signal(err)301 return302 }303 cmd.Process.Kill()304 dmesg.Close()305 merger.Wait()306 cmd.Wait()307 }()308 return merger.Output, errc, nil309}310func (inst *instance) sshArgs(portArg string) []string {311 args := []string{312 portArg, "22",313 "-o", "ConnectionAttempts=10",314 "-o", "ConnectTimeout=10",315 "-o", "BatchMode=yes",316 "-o", "UserKnownHostsFile=/dev/null",317 "-o", "IdentitiesOnly=yes",318 "-o", "StrictHostKeyChecking=no",319 "-o", "LogLevel=error",320 }321 if inst.sshkey != "" {322 args = append(args, "-i", inst.sshkey)323 }324 if inst.debug {325 args = append(args, "-v")326 }327 return args328}...

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(isolated.Ssh())4}5import (6func main() {7 fmt.Println(isolated.Ssh())8}9func Ssh() string {10}11func Ssh() string {12}13func Ssh() string {14}15func Ssh() string {16}17func Ssh() string {18}19func Ssh() string {20}21func Ssh() string {22}23func Ssh() string {24}25func Ssh() string {26}27func Ssh() string {28}29func Ssh() string {30}31func Ssh() string {32}33func Ssh() string {34}35func Ssh() string {36}37func Ssh() string {38}39func Ssh() string {40}41func Ssh() string {42}43func Ssh() string {44}

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 obj.ssh()4}5import (6func main() {7 obj.ssh()8}9import (10func main() {11 obj.ssh()12}13import (14func main() {15 obj.ssh()16}17import (18func main() {19 obj.ssh()20}21import (22func main() {23 obj.ssh()24}25import (26func main() {27 obj.ssh()28}29import (30func main() {31 obj.ssh()32}33import (34func main() {35 obj.ssh()36}37import (38func main() {39 obj.ssh()40}41import (

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, world.")4 ssh.Isolated()5}6import (7func main() {8 fmt.Println("Hello, world.")9 ssh.Isolated()10}11import (12func main() {13 fmt.Println("Hello, world.")14 ssh.Isolated()15}16import (17func main() {18 fmt.Println("Hello, world.")19 ssh.Isolated()20}21import (22func main() {23 fmt.Println("Hello, world.")24 ssh.Isolated()25}26import (27func main() {28 fmt.Println("Hello, world.")29 ssh.Isolated()30}31import (32func main() {33 fmt.Println("Hello, world.")34 ssh.Isolated()35}36import (37func main() {38 fmt.Println("Hello, world.")39 ssh.Isolated()40}41import (42func main() {43 fmt.Println("Hello, world.")44 ssh.Isolated()45}46import (47func main() {48 fmt.Println("Hello, world.")

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World!")4}5import (6type ssh struct {7}8func (s *ssh) Init() {9 fmt.Println("ssh init")10}11func (s *ssh) Connect() {12 fmt.Println("ssh connect")13}14func (s *ssh) Execute() {15 fmt.Println("ssh execute")

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, World!")4 isolated.Ssh()5}6import (7func main() {8 fmt.Println("Hello, World!")9 isolated.Isolated.Ssh()10}11import (12func main() {13 fmt.Println("Hello, World!")14 isolated.Ssh()15}16import (17func main() {18 fmt.Println("Hello, World!")19 isolated.Isolated.Ssh()20}21import (

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful