unable to spawn ssh using TCL expect in ActiveTCL - ssh

I am trying to ssh through .tcl script from ActiveState TCL 'tclsh' window.
Having WINDOWS OS system.
#!/bin/sh
# \
exec tclsh "$0" ${1+"$#"}
package require Expect
set user [lindex $argv 0]
set password [lindex $argv 1]
set DeviceIpAddr [lindex $argv 2]
set DeviceHostName [lindex $argv 3]
foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {
spawn ssh $DeviceIp
expect "login as:"
send "$user\r"
expect "Password:"
send "$password\r"
expect "$HostName ~]#"
}
I see below error while execute in tclsh(ActiveTCL)
% tclsh Test.tcl root 321 17.250.217.151 lb02va
The system cannot find the file specified.
while executing
"spawn ssh root#$DeviceIp"
("foreach" body line 3)
invoked from within
"foreach DeviceIp $DeviceIpAddr HostName $DeviceHostName {
spawn ssh root#$DeviceIp
expect "login as:"
send "$user\r"
expect "Password:"
send..."
(file "Test.tcl" line 12)
child process exited abnormally
Kindly assist me resolving this.
Thank you.

First, make sure that you have ssh installed. From the bash prompt (Mac, Linux, Cygwin) or cmd prompt (Windows), type:
ssh
If you see an error, try to fix it. The most likely cause is ssh not installed, or not in the path.
Next, in your script, you did not use the $user variable, instead you use the hard-coded root. Fix that:
spawn ssh $user#$DeviceIp
The final problem: you already specified the user name from the command line, the ssh program will not ask for user again, so you must delete these two lines:
expect "login as:"
send "$user\r"
After that, hopefully everything will go as planned.

Related

How to write the expect script to install mariadb?

Environment: centos7 + mariadb5.5.64.
Let me show the installation info on screen when to run mysql_secure_installation.
# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
I write an automation expect script to install mariadb.
vim secure.exp
set timeout 60
spawn mysql_secure_installation
expect {
"Enter current password for root (enter for none): " {send "\r";exp_continue}
"Set root password? [Y/n] " {send "y\r";exp_continue}
"New password:" {send "123456\r";exp_continue}
"Re-enter new password:" {send "123456\r";exp_continue}
"Remove anonymous users? [Y/n]" {send "y\r";exp_continue}
"Disallow root login remotely? [Y/n]" {send "y\r";exp_continue}
"Remove test database and access to it? [Y/n]" {send "y\r";exp_continue}
"Reload privilege tables now? [Y/n]" {send "y\r";exp_continue}
}
To execute /usr/bin/expect secure.exp, i come across the error:
spawn mysql_secure_installation
invalid command name "Y/n"
while executing
"Y/n"
invoked from within
"expect {
"Enter current password for root (enter for none): " {send "\r";exp_continue}
"Set root password? [Y/n] " {send "y\r";exp..."
(file "secure.exp" line 3)
It is no use to write as below:
set timeout 60
spawn mysql_secure_installation
expect {
"Enter current password for root (enter for none): " {send "\r";exp_continue}
"Set root password? \\[Y/n] " {send "y\r";exp_continue}
"New password:" {send "123456\r";exp_continue}
"Re-enter new password:" {send "123456\r";exp_continue}
"Remove anonymous users? \\[Y/n]" {send "y\r";exp_continue}
"Disallow root login remotely? \\[Y/n]" {send "y\r";exp_continue}
"Remove test database and access to it? \\[Y/n]" {send "y\r";exp_continue}
"Reload privilege tables now? \\[Y/n]" {send "y\r";exp_continue}
}
Same error:
invalid command name "Y/n"
while executing
"Y/n"
invoked from within
"expect {
"Enter current password for root (enter for none): " {send "\r";exp_continue}
"Set root password? \\[Y/n] " {send "y\r";exp_conti..."
(file "secure.exp" line 3)
How to fix my exp script then?
These scripts wait to receive optional output (timeout -1 means "no timeout") and they can tell apart different responses, as it is required by yum install and mysql_secure_installation. With #!/bin/expect -f as shebang, the scripts can be executed, when they were set to chmod +x.
A) To begin with, mariadb_yum.exp (requires su or sudo):
#!/bin/expect -f
set timeout 30
if {[llength $argv] == 0} {
send_user "Usage: mariadb_yum.exp \[linux sudo password\]\n"
exit 1
}
set USERNAME "[exec whoami]"
set PASSWORD [lindex $argv 0];
# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/$USERNAME/mariadb_install.log"
spawn sudo yum -y install MariaDB-server
set yum_spawn_id $spawn_id
# On GCE it will never ask for a sudo password:
expect -ex "\[sudo\] password for $USERNAME: " {
exp_send "$PASSWORD\r"
}
expect {
# when the package was already installed
-ex "Nothing to do" {
send_user "package was already installed\n"
}
# when the package had been installed
-ex "Complete!" {
send_user "package had been installed\n"
}
}
expect eof
close $yum_spawn_id
exit 0
B) And then mariadb_sec.exp (doesn't require sudo):
#!/bin/expect -f
set timeout 1
if {[llength $argv] == 0} {
send_user "Usage: mariadb_sec.exp \[mysql root password\]\n"
exit 1
}
set PASSWORD [lindex $argv 0];
spawn mysql_secure_installation
set mysql_spawn_id $spawn_id
# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/[exec whoami]/mariadb_install.log"
# when there is no password set, this probably should be "\r"
expect -ex "Enter current password for root (enter for none): "
exp_send "$PASSWORD\r"
expect {
# await an eventual error message
-ex "ERROR 1045" {
send_user "\nMariaDB > An invalid root password had been provided.\n"
close $mysql_spawn_id
exit 1
}
# when there is a root password set
-ex "Change the root password? \[Y/n\] " {
exp_send "n\r"
}
# when there is no root password set (could not test this branch).
-ex "Set root password? \[Y/n\] " {
exp_send "Y\r"
expect -ex "New password: "
exp_send "$PASSWORD\r"
expect -ex "Re-enter new password: "
exp_send "$PASSWORD\r"
}
}
expect -ex "Remove anonymous users? \[Y/n\] "
exp_send "Y\r"
expect -ex "Disallow root login remotely? \[Y/n\] "
exp_send "Y\r"
expect -ex "Remove test database and access to it? \[Y/n\] "
exp_send "Y\r"
expect -ex "Reload privilege tables now? \[Y/n\] "
exp_send "Y\r"
expect eof
close $mysql_spawn_id
exit 0
For debugging purposes - or to validate the answer, one can run expect with log-level strace 4. This is probably as reputable as a source can get, when it comes to writing expect scripts, as it nicely displays what is going on and most importantly, in which order the things happen:
expect -c "strace 4" ./mariadb_yum.exp [linux sudo password]
expect -c "strace 4" ./mariadb_sec.exp [mysql root password]
Instruction set exp_internal 1 can be used to get output for the regex-matching.
A possible source of confusion might be, where one spawns the processes - as one can spawn several process on various hosts, eg. ssh locally and then yum and mysql_secure_installation remotely. Added $spawn_id to the script; the bottom one close call might be redundant, since it is already EOF (just to show how to spawn & close processes):
Thanks for using MariaDB!
1 close $mysql_spawn_id
1 exit 0
2 rename _close.pre_expect close
Conclusion: The mariadb_sec.exp script probably could be improved further, eg. when at first sending no password and seeing what happens - then sending the password on ERROR 1045 (when a password had already been set previously). It may be save to assume, that one has to set the password when the server just has been installed (except that yum reinstall delivers the same result). Just had no blank CentOS container to test all the cases. Unless running in a root shell, passing both kinds of passwords into one script would be required to automate this from installation until post-installation.
Probably worth noting is that on GCE, sudo would not ask for a password; there are indeed minor differences based upon the environment, as these CentOS container images behave differently. In such case (since there is no su or container-image detection in place), the mariadb_yum.exp script might get stuck for 30 seconds and then continue.
The most reputable sources I can offer are the expect manual, written by Don Libes # NIST and the TCL/TK manual for expect, along with it's SourceForge project coincidentally called expect.
Not only are square brackets used for command substitution, but they are also special for glob patterns.
You can either use the -exact switch while escaping the square brackets in quotes:
spawn mysql_secure_installation
expect {
...
-exact "Set root password? \[Y/n\] " {send "y\r";exp_continue}
...
}
Or use braces instead of quotes:
spawn mysql_secure_installation
expect {
...
{Set root password? \[Y/n\] } {send "y\r";exp_continue}
...
}
FYI you can have the expect script generated for you by using autoexpect:
autoexpect ./mysql_secure_installation
This will generate an expect script called script.exp in your present working directory.

Expect: How to use "ls -d filename" for full path name in expect?

I am trying to automate loading of image on the hardware using expect. For that I need to get full path of the image.
I am using the following syntax -
spawn ls -d $env(PWD)/build/image/bmxs.*bin
expect -re {(\S+)(\r)}
set imgpath $expect_out(1,string)
The message I get is -
spawn: returns {51875}
expect: does "" (spawn_id exp4) match regular expression "(\S+)(\r)"? no
So, it appears that the spawn does not return anything.
I've tried various syntaxes, but no use -
send "ls -d $env(PWD)/build/images/final/nxos.*bin\r"
spawn "ls -d $env(PWD)/build/image/bmxs.*bin"
puts "$LS" ### where $LS is the command.
None of these work. Am I making a mistake?
Your code suggests that
the image file is local - on same machine where you run Expect
You want the first file which matches the pattern
If this is so, you can just do
set files [glob $env(PWD)/build/image/bmxs.*bin]
set imgpath [lindex [lsort $files] 0]
On the other hand, the phrase "on the hardware" suggests that this image file is on some remote system. If so, and you already have spawned a login session there, you need to send the ls command on the existing session and then expect the output from ls. However in that case it looks strange to get the directory from $env(PWD)" as this will read the environment variable PWD on your local machine.

Expect ssh and create directory

I'm having some trouble with expect.
I'm trying to ssh onto another machine and then create a directory on that machine.
Right now this is what my code looks like:
spawn ssh username#ipAddress
expect "password"
send "password"
file mkdir directoryName
That code is giving me a "permission denied".
When I try replacing
file mkdir directoryName
with
send "mkdir directoryName"
There's no error, but it doesn't make a file.
Thanks.
This might help you :-
#!/usr/bin/expect
set timeout -1
spawn -noecho bash -c "ssh username#serveraddress 'cd /user/bill/work;<your=command>'"
expect {
-re "assword:"{
send "mypassword/r"
}eof{
wait
}
You must send the command inside ssh as it will run on remote machine.
Explanation for above script :-
set timeout -1will set this loop in infinite (but it will exit once spawn process is finished.
-re will match regex for assword:
eof will wait until spawn is finish.
After sending mkdir command, wait for eof to happen.
send "mkdir directoryName\r"
expect eof

Passing arguments to remote script from local script using except

Hello Im newbie to Expect scripting. Im trying to call remote script using ssh spawn and passing commandline arguments to the remote script.But in remote script im getting null values. Please help to solve this issue. Is the problem with passing the arguments in except script?
Local Expect script
#!/usr/bin/expect
set hana_schema [lindex $argv 1]
set table [lindex $argv 2]
set condition [lindex $argv 3]
set yyyymm [lindex $argv 4]
set targetdir [lindex $argv 5]
set split [lindex $argv 6]
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set script /path-to-script/test.sh
# here I spawn a shell that will run ssh and redirect the script to ssh's
spawn sh -c "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user#$ip bash $hana_schema $table $condition $yyyymm $targetdir $split < $script" "$hana_schema" "$table" "$condition" "$yyyymm" "$targetdir" "$split"
expect "Password:"
send "$password\r"
# and just wait for the script to finish
expect eof
Remote script test.sh
hana_schema=$1
table=$2
condition=$3
yyyymm=$4
targetdir=$5
split=$6
echo "$hana_schema"
echo "$table"
echo "$condition"
echo "$yyyymm"
echo "$targetdir"
echo "$split"
In this case, you're just passing the expect scripts parameters through to the remote script untouched. There's really no point saving them in separate variables. Also wrapping the ssh call with sh is not needed. I'd do this:
#!/usr/bin/expect
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no}
set script /path-to-script/test.sh
spawn ssh {*}$ssh_opts $user#$ip bash $script {*}$argv
expect "Password:"
send "$password\r"
expect eof
The {*} syntax expands a list into its individual elements. See http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm

Expect script to run Ansible playbooks

I have written an Ansible playbook which prompts me to enter password interactively for SSH and SUDO like below.
$ ansible-playbook -i test --limit dev app_name.yml -vv --tags=stop
SSH password:
SUDO password[defaults to SSH password]:
There are various options available with Ansible like defining password in ansible_ssh_password under group_vars but it don't seem to work for me since I can't have sshpass installed in my target server nor I am allowed to make any changes to my sudoers file.
I tried to execute ansible-playbook from a little expect script below
#!/usr/bin/expect -f
set password PASSWORD
set where_to_execute [lindex $argv 0]
set which_app_to_execute [lindex $argv 1]
set what_to_execute [lindex $argv 2]
send "ansible-playbook -i test --limit $where_to_execute $which_app_to_execute -vv --tags=$what_to_execute \r"
expect "SSH password:"
send "$password \r"
expect "SUDO password*"
send "$password \r"
expect "$"
send "exit \r"
Unfortunately this is also not working may be because SSH process is not spawned by expect. Did anyone try this method and got things working. Please suggest. Thanks.
The problem with your expect scripts is that you aren't actually running the ansible command there (or any command for that matter).
You use
send "ansible-playbook -i test --limit $where_to_execute $which_app_to_execute -vv --tags=$what_to_execute \r"
which sends that string to ... nowhere as far as I know. There's nowhere for it to go.
What you want to be doing is spawning that ansible command and then using expect to communicate with it.
Something like this:
spawn ansible-playbook -i test --limit $where_to_execute $which_app_to_execute -vv --tags=$what_to_execute
You may also want to set the timeout value if the ansible command can take a little while (to prevent expect from killing it when it doesn't return quickly enough).
It works for me using the python implementation of expect. pexpect
install pexpect using pip: pip install pexpect
You can use this code as an workaround for your expect script:
#!/usr/bin/python
import pexpect
def main(args):
#Setup variables
password, where, which, what = args
cmd = "ansible-playbook -i test --limit %s %s -vv --tags=%s" % (where, which, what)
child = pexpect.spawn(cmd)
child.sendline(password)
child.expect('SSH password:')
child.sendline(password)
child.expect('SUDO password*')
child.expect(pexpect.EOF)
print child.before
if __name__ == '__main__':
main(sys.argv[1:])
This is the most simple example but it's working fine for me.
./myscript.py mypassword dev app_name.yml stop
As #Etan Reisner pointed out, the main difference between your code that isn't working and my pexpect code is the spawn ansible command. The above code on expect also works fine:
#!/usr/bin/expect -f
spawn /usr/bin/ansible -m ping myserver --ask-pass
expect "SSH password:"
send "mypassword\r"
expect "$ "