Expect TCL scripting for ssh remote login into beaglebone

Expect TCL scripting for ssh remote login into beaglebone Debian OS


To install expect in Ubuntu, 
sudo apt-get install expect


**************************************
PROGRAM 1:

expect script to login into beagle bone 
and type some commands
**************************************
#!/usr/bin/expect -f

set timeout 30
spawn ssh root@192.168.1.7

expect "*password:" { send "admin123\r"}

expect "~#" { send "ls -ltr\r" }
expect "~#" { send "ls -ltr\r" }
expect "~#" { send "ls -ltr\r" }
expect "~#" { send "ls -ltr\r" }
expect "~#" { send "echo I have now successfully logged into beaglebone\r" }

interact


###############################################
Output:
###############################################
neelkanth_surekha#expect login.expect
spawn ssh root@192.168.1.7
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2016-06-15

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

root@192.168.1.7's password:
Last login: Sun Dec 24 08:26:52 2017 from thinkpad-e450.local
root@beaglebone:~# ls -ltr
total 0
root@beaglebone:~# ls -ltr
total 0
root@beaglebone:~# ls -ltr
total 0
root@beaglebone:~# ls -ltr
total 0
root@beaglebone:~# echo I have now successfully logged into beaglebone
I have now successfully logged into beaglebone
root@beaglebone:~#

********************************************************************
PROGRAM 2:
expect script to ssh login into beaglebone and change the password 
in beagle bone via shell
********************************************************************
#!/usr/bin/expect -f

set timeout 30
spawn ssh root@192.168.1.7

expect "*password:" { send "admin231\r"}

expect "~#" { send "passwd\r" }
expect "password:" {send "admin231\r" }
expect "password:" {send "admin231\r" }
expect eof

interact

********************************
Output:
********************************
neelkanth_surekha#expect login.expect
spawn ssh root@192.168.1.7
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2016-06-15

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

root@192.168.1.7's password:
Last login: Sun Dec 24 18:58:44 2017 from thinkpad-e450.local
root@beaglebone:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

***********************************************************************
PROGRAM 3:  Automate ssh using expect script
***********************************************************************
neelkanth_surekha#cat login.expect
#!/usr/bin/expect -f

set ssh_destination root@192.168.1.7
set old_pass admin231
set new_pass admin231

set timeout 30

spawn ssh $ssh_destination

expect "*password:" { send "$old_pass\r"}

expect "~#" { send "passwd\r" }
expect "password:" {send "$new_pass\r" }
expect "password:" {send "$new_pass\r" }
expect "password updated successfully\r" {
       send "exit \r"
       expect eof
}

send_user "*********************************************************\n"
send_user "\n\n Hey Neelu: Beagle bone password changed successfully\n"
send_user "*********************************************************\n"

#interact

*******************************
Output:
*******************************
neelkanth_surekha#expect login.expect
spawn ssh root@192.168.1.7
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2016-06-15

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

root@192.168.1.7's password:
Last login: Wed Jun 15 21:54:52 2016 from thinkpad-e450.local
root@beaglebone:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@beaglebone:~# exit
logout
Connection to 192.168.1.7 closed.
*********************************************************


 Hey Neelu: Beagle bone password changed successfully
*********************************************************



***************************************************************************
Using autoexpect to generate expect script based on keyboard input:

Note:
Although it looks as if nothing has happened or is happening,
 every keystroke you type
 will be recorded into script.exp . Every STDOUT response you receive 
will also be copied into that same file. Your entire session is recorded –
 but not just recorded, it is also formatted in Expect script style. To stop 
recording keystrokes to your script, press Ctrl+D on your keyboard to stop 
Autoexpect and copy the buffer to your file.
***************************************************************************
To install Autoexpect, issue a command like:

$ sudo apt-get install expect-dev

neelkanth_surekha#autoexpect
autoexpect started, file is script.exp
neelkanth_surekha#ssh root@192.168.1.7
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2016-06-15

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

root@192.168.1.7's password:
Last login: Wed Jun 15 23:59:43 2016 from thinkpad-e450.local
root@beaglebone:~#
root@beaglebone:~# exit
logout
Connection to 192.168.1.7 closed.
neelkanth_surekha#
neelkanth_surekha#
neelkanth_surekha#
neelkanth_surekha#ls
login.expect  script.exp
neelkanth_surekha#vi script.exp
neelkanth_surekha#vi script.exp

************************************************
Now, execute autodetect expect script
************************************************
neelkanth_surekha#expect script.exp
spawn /bin/bash
neelkanth_surekha#ssh root@192.168.1.7
Debian GNU/Linux 7

BeagleBoard.org Debian Image 2016-06-15

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

root@192.168.1.7's password:
Last login: Thu Jun 16 03:08:33 2016 from thinkpad-e450.local
root@beaglebone:~#