Wednesday, February 27, 2019

execute a script on a remote machine over ssh using expect


https://linux.die.net/man/1/expect
https://www.blogger.com/blogger.g?blogID=7297027245100107097#allposts/postNum=0

* clock
https://www.tcl.tk/man/tcl8.5/tutorial/Tcl41.html

on Macbook
brew install expect

on Ubuntu
sudo apt install expect

expect.sh

------------------------------

#!/bin/bash
nodeIp=192.168.1.176

(expect -c '
#exp_internal 1
#set timeout 15
spawn ssh root@'$nodeIp'
#expect {
#        ")? " { send "yes\r" ; exp_continue }
#        "# " { send "\r" }
#        }
expect "# "
send "rm /var/log/messages*\r"
expect "# "
send "reboot\r"
expect "# "
#send "exit\r"
    expect eof ')

------------------------------

#!/bin/bash
#foreach {host user password target} $argv {break}

#set command "ping -c 1 $target"
#send "$command\r"

#log_user 0                   ;# turn off the usual output
#expect -re "(yes/no)"
#send "yes\r"
#sleep 1

nodeIp=192.168.1.176

(
expect -c '
#exp_internal 1
#set timeout 20

set command "i2cget -y 1 0x1c 0x14"

spawn ssh root@'$nodeIp'
#expect {
#        ")? " { send "yes\r" ; exp_continue }
#        "# " { send "\r" }
#        }

expect "~# "
send_user "running $command\r\n"

send "$command\r"
expect "0x04"
send "\r"
# puts "$expect_out(0,string)\n"
if {$expect_out(0,string) == "0x04"} {
puts "output of i2cget -y 1 0x1c 0x14 is 0x04\n"
} else {
puts "False\n"
}
# puts "$expect_out(buffer)"

send "\r"
expect "~# "

send "exit\r"
expect eof '
)

No comments:

Post a Comment