How to use ssh method of odroid Package

Best Syzkaller code snippet using odroid.ssh

odroid.go

Source:odroid.go Github

copy

Full Screen

...39}40type instance struct {41 cfg *Config42 os string43 sshkey string44 closed chan bool45 debug bool46}47func ctor(env *vmimpl.Env) (vmimpl.Pool, error) {48 cfg := &Config{}49 if err := config.LoadData(env.Config, cfg); err != nil {50 return nil, fmt.Errorf("failed to parse odroid vm config: %v", err)51 }52 if cfg.Host_Addr == "" {53 return nil, fmt.Errorf("config param host_addr is empty")54 }55 if cfg.Slave_Addr == "" {56 return nil, fmt.Errorf("config param slave_addr is empty")57 }58 if cfg.Console == "" {59 return nil, fmt.Errorf("config param console is empty")60 }61 if cfg.Hub_Bus == 0 {62 return nil, fmt.Errorf("config param hub_bus is empty")63 }64 if cfg.Hub_Device == 0 {65 return nil, fmt.Errorf("config param hub_device is empty")66 }67 if cfg.Hub_Port == 0 {68 return nil, fmt.Errorf("config param hub_port is empty")69 }70 if !osutil.IxExist(cfg.Console) {71 return nil, fmt.Errorf("console file '%v' does not exist", cfg.Console)72 }73 pool := &Pool{74 cfg: cfg,75 env: env,76 }77 return pool, nil78}79func (pool *Pool) Count() int {80 return 1 // no support for multiple Odroid devices yet81}82func (pool *Pool) Create(workdir string, index int) (vmimpl.Instance, error) {83 inst := &instance{84 cfg: pool.cfg,85 os: pool.env.OS,86 sshkey: pool.env.Sshkey,87 closed: make(chan bool),88 debug: pool.env.Debug,89 }90 closeInst := inst91 defer func() {92 if closeInst != nil {93 closeInst.Close()94 }95 }()96 if err := inst.repair(); err != nil {97 return nil, err98 }99 // Create working dir if doesn't exist.100 inst.ssh("mkdir -p /data/")101 // Remove temp files from previous runs.102 inst.ssh("rm -rf /data/syzkaller-*")103 closeInst = nil104 return inst, nil105}106func (inst *instance) Forward(port int) (string, error) {107 return fmt.Sprintf(inst.cfg.Host_Addr+":%v", port), nil108}109func (inst *instance) ssh(command string) ([]byte, error) {110 if inst.debug {111 Logf(0, "executing ssh %+v", command)112 }113 rpipe, wpipe, err := osutil.LongPipe()114 if err != nil {115 return nil, err116 }117 args := append(vmimpl.SSHArgs(inst.debug, inst.sshkey, 22), "root@"+inst.cfg.Slave_Addr, command)118 if inst.debug {119 Logf(0, "running command: ssh %#v", args)120 }121 cmd := osutil.Command("ssh", args...)122 cmd.Stdout = wpipe123 cmd.Stderr = wpipe124 if err := cmd.Start(); err != nil {125 wpipe.Close()126 return nil, err127 }128 wpipe.Close()129 done := make(chan bool)130 go func() {131 select {132 case <-time.After(time.Minute):133 if inst.debug {134 Logf(0, "ssh hanged")135 }136 cmd.Process.Kill()137 case <-done:138 }139 }()140 if err := cmd.Wait(); err != nil {141 close(done)142 out, _ := ioutil.ReadAll(rpipe)143 if inst.debug {144 Logf(0, "ssh failed: %v\n%s", err, out)145 }146 return nil, fmt.Errorf("ssh %+v failed: %v\n%s", args, err, out)147 }148 close(done)149 if inst.debug {150 Logf(0, "ssh returned")151 }152 out, _ := ioutil.ReadAll(rpipe)153 return out, nil154}155func switchPortPower(busNum, deviceNum, portNum int, power bool) error {156 var context *C.libusb_context157 if err := C.libusb_init(&context); err != 0 {158 return fmt.Errorf("failed to init libusb: %v\n", err)159 }160 defer C.libusb_exit(context)161 var rawList **C.libusb_device162 numDevices := int(C.libusb_get_device_list(context, &rawList))163 if numDevices < 0 {164 return fmt.Errorf("failed to init libusb: %v", numDevices)165 }166 defer C.libusb_free_device_list(rawList, 1)167 var deviceList []*C.libusb_device168 *(*reflect.SliceHeader)(unsafe.Pointer(&deviceList)) = reflect.SliceHeader{169 Data: uintptr(unsafe.Pointer(rawList)),170 Len: numDevices,171 Cap: numDevices,172 }173 var hub *C.libusb_device174 for i := 0; i < numDevices; i++ {175 var desc C.struct_libusb_device_descriptor176 if err := C.libusb_get_device_descriptor(deviceList[i], &desc); err != 0 {177 return fmt.Errorf("failed to get device descriptor: %v", err)178 }179 if desc.bDeviceClass != C.USB_CLASS_HUB {180 continue181 }182 if C.libusb_get_bus_number(deviceList[i]) != C.uint8_t(busNum) {183 continue184 }185 if C.libusb_get_device_address(deviceList[i]) != C.uint8_t(deviceNum) {186 continue187 }188 hub = deviceList[i]189 break190 }191 if hub == nil {192 return fmt.Errorf("hub not found: bus: %v, device: %v", busNum, deviceNum)193 }194 var handle *C.libusb_device_handle195 if err := C.libusb_open(hub, &handle); err != 0 {196 return fmt.Errorf("failed to open usb device: %v", err)197 }198 request := C.uint8_t(C.USB_REQ_CLEAR_FEATURE)199 if power {200 request = C.uint8_t(C.USB_REQ_SET_FEATURE)201 }202 port := C.uint16_t(portNum)203 timeout := C.uint(1000)204 if err := C.libusb_control_transfer(handle, C.USB_RT_PORT, request,205 C.USB_PORT_FEAT_POWER, port, nil, 0, timeout); err < 0 {206 return fmt.Errorf("failed to send control message: %v\n", err)207 }208 return nil209}210func (inst *instance) repair() error {211 // Try to shutdown gracefully.212 Logf(1, "odroid: trying to ssh")213 if err := inst.waitForSSH(10 * time.Second); err == nil {214 Logf(1, "odroid: ssh succeeded, shutting down now")215 inst.ssh("shutdown now")216 if !vmimpl.SleepInterruptible(20 * time.Second) {217 return fmt.Errorf("shutdown in progress")218 }219 } else {220 Logf(1, "odroid: ssh failed")221 }222 // Hard reset by turning off and back on power on a hub port.223 Logf(1, "odroid: hard reset, turning off power")224 if err := switchPortPower(inst.cfg.Hub_Bus, inst.cfg.Hub_Device, inst.cfg.Hub_Port, false); err != nil {225 return err226 }227 if !vmimpl.SleepInterruptible(5 * time.Second) {228 return fmt.Errorf("shutdown in progress")229 }230 if err := switchPortPower(inst.cfg.Hub_Bus, inst.cfg.Hub_Device, inst.cfg.Hub_Port, true); err != nil {231 return err232 }233 // Now wait for boot.234 Logf(1, "odroid: power back on, waiting for boot")235 if err := inst.waitForSSH(150 * time.Second); err != nil {236 return err237 }238 Logf(1, "odroid: boot succeeded")239 return nil240}241func (inst *instance) waitForSSH(timeout time.Duration) error {242 return vmimpl.WaitForSSH(inst.debug, timeout, inst.cfg.Slave_Addr, inst.sshkey, "root", inst.os, 22)243}244func (inst *instance) Close() {245 close(inst.closed)246}247func (inst *instance) Copy(hostSrc string) (string, error) {248 basePath := "/data/"249 vmDst := filepath.Join(basePath, filepath.Base(hostSrc))250 args := append(vmimpl.SCPArgs(inst.debug, inst.sshkey, 22), hostSrc, "root@"+inst.cfg.Slave_Addr+":"+vmDst)251 cmd := osutil.Command("scp", args...)252 if inst.debug {253 Logf(0, "running command: scp %#v", args)254 cmd.Stdout = os.Stdout255 cmd.Stderr = os.Stdout256 }257 if err := cmd.Start(); err != nil {258 return "", err259 }260 done := make(chan bool)261 go func() {262 select {263 case <-time.After(3 * time.Minute):264 cmd.Process.Kill()265 case <-done:266 }267 }()268 err := cmd.Wait()269 close(done)270 if err != nil {271 return "", err272 }273 return vmDst, nil274}275func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (276 <-chan []byte, <-chan error, error) {277 tty, err := vmimpl.OpenConsole(inst.cfg.Console)278 if err != nil {279 return nil, nil, err280 }281 rpipe, wpipe, err := osutil.LongPipe()282 if err != nil {283 tty.Close()284 return nil, nil, err285 }286 args := append(vmimpl.SSHArgs(inst.debug, inst.sshkey, 22),287 "root@"+inst.cfg.Slave_Addr, "cd /data; "+command)288 if inst.debug {289 Logf(0, "running command: ssh %#v", args)290 }291 cmd := osutil.Command("ssh", args...)292 cmd.Stdout = wpipe293 cmd.Stderr = wpipe294 if err := cmd.Start(); err != nil {295 tty.Close()296 rpipe.Close()297 wpipe.Close()298 return nil, nil, err299 }300 wpipe.Close()301 var tee io.Writer302 if inst.debug {303 tee = os.Stdout304 }305 merger := vmimpl.NewOutputMerger(tee)306 merger.Add("console", tty)307 merger.Add("ssh", rpipe)308 errc := make(chan error, 1)309 signal := func(err error) {310 select {311 case errc <- err:312 default:313 }314 }315 go func() {316 select {317 case <-time.After(timeout):318 signal(vmimpl.TimeoutErr)319 case <-stop:320 signal(vmimpl.TimeoutErr)321 case <-inst.closed:...

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := odroid.InitModule()4 if err != nil {5 panic(err)6 }7 led, err := gpio.GetPin(odroid.LED)8 if err != nil {9 panic(err)10 }11 err = led.SetMode(gpio.OUTPUT)12 if err != nil {13 panic(err)14 }15 for {16 err = led.Write(gpio.HIGH)17 if err != nil {18 panic(err)19 }20 time.Sleep(time.Second)21 err = led.Write(gpio.LOW)22 if err != nil {23 panic(err)24 }25 time.Sleep(time.Second)26 }27}28import (29func main() {30 err := odroid.InitModule()31 if err != nil {32 panic(err)33 }34 led, err := gpio.GetPin(odroid.LED)35 if err != nil {36 panic(err)37 }38 err = led.SetMode(gpio.OUTPUT)39 if err != nil {40 panic(err)41 }42 for {43 err = led.Write(gpio.HIGH)44 if err != nil {45 panic(err)46 }47 time.Sleep(time.Second)48 err = led.Write(gpio.LOW)49 if err != nil {50 panic(err)51 }52 time.Sleep(time.Second)

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 odroid := ssh.NewOdroid()4 err := odroid.Connect()5 if err != nil {6 fmt.Println(err)7 }8 response, err := odroid.RunCommand("ls")9 if err != nil {10 fmt.Println(err)11 }12 fmt.Println(response)13 response, err = odroid.RunCommandByte("ls")14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println(string(response))18 response, err = odroid.RunCommandByte("ls")19 if err != nil {20 fmt.Println(err)21 }22 fmt.Println(string(response))23 err = odroid.Disconnect()24 if err != nil {25 fmt.Println(err)26 }27}28import (29func main() {30 odroid := ssh.NewOdroid()31 err := odroid.Connect()32 if err != nil {33 fmt.Println(err)34 }35 err = odroid.CopyFileFromOdroid("/home/odroid/test.txt", "test.txt")36 if err != nil {37 fmt.Println(err)38 }

Full Screen

Full Screen

ssh

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 odroid := odroid.NewOdroid()4 if odroid.Err != nil {5 fmt.Println(odroid.Err)6 }7 odroid.Connect()8 if odroid.Err != nil {9 fmt.Println(odroid.Err)10 }11 odroid.RunCommand("ls")12 if odroid.Err != nil {13 fmt.Println(odroid.Err)14 }15 fmt.Println(odroid.Result)16}17import (18func main() {19 odroid := odroid.NewOdroid()20 if odroid.Err != nil {21 fmt.Println(odroid.Err)22 }23 odroid.Connect()24 if odroid.Err != nil {25 fmt.Println(odroid.Err)26 }27 odroid.RunCommand("ls")28 if odroid.Err != nil {29 fmt.Println(odroid.Err)30 }31 fmt.Println(odroid.Result)32}33import (34func main() {35 odroid := odroid.NewOdroid()36 if odroid.Err != nil {37 fmt.Println(odroid.Err)38 }39 odroid.Connect()40 if odroid.Err != nil {41 fmt.Println(odroid.Err)42 }43 odroid.RunCommand("ls")44 if odroid.Err != nil {45 fmt.Println(odroid.Err)46 }

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