How Can I send the content of a file in Expect? Do I have to use cat command in a way? if yes how? lets say my file is called 1.txt.
example:
expect "Enter command to send:" {send "???? \r"} ???? --> content of the file 1.txt.
The target of the following simple expect script is to get the hostname name on the remote machine
Sometimes expect script fail to perform ssh to $IP_ADDRESS ( because remote machine not active , etc )
so in this case the expect script will break after 10 second (timeout 10) , this is OK but......
There are two options
On both cases expect will exit
but I don’t know if expect script perform ssh successfully or not?
is it possible to identify timeout process ? or to verify that expect ended because timeout?
Remark my Linux machine version - red-hat 5.1
Expect script
[TestLinux]# get_host_name_on_remote_machine=`cat << EOF
> set timeout 10
> spawn ssh $IP_ADDRESS
> expect {
> ")?" { send "yes\r" ; exp_continue }
>
> word: {send $PASS\r}
> }
> expect > {send "hostname\r"}
> expect > {send exit\r}
> expect eof
> EOF`
Example in case we not have connection to the remote host
[TestLinux]# expect -c "$get_host_name_on_remote_machine"
spawn ssh 10.17.180.23
[TestLinux]# echo $?
0
I want to su to another user, using expect, and then run a graphical program as that user. I am using kcalc to test with:
#!/usr/bin/expect
set timeout 20
spawn su dummy
expect "Password:"
send "PASS\r";
send "kcalc\r"
I also tried eliminating the last line and changing the su to 'su dummy -c kcalc', but that didn't work either. I appreciate any help.
I write the active.ksh script (based on expect) in order to login automatically to some Solaris machine and execute the hostname command (login to virtual IP in order to verify which hostname is the active machine - I have two cluster solaris machines )
The problem is with expect
; expect sends the password string (pass123) and it misses the Password question, and it still waits for the password.
So actually the password (pass123) was entered after password question. On most cases the expect script works fine but sometimes it missed the password.
EXAMPLE OF THE PROBLEM
./active.ksh
spawn ssh 10.10.18.61
sh: /usr/local/bin/stty: not found
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
Password: * my remark - pass123 string was missed the Password Question pass123
Password:
#!/bin/ksh
VIP_ADDRESS=10.10.18.61
expect_for_verify_which_active_machine=`cat << EOF
set timeout -1
spawn ssh $VIP_ADDRESS
expect {
")?" { send "yes\r" ; exp_continue }
Password: {send "pass123\r"}
}
expect > {send "hostname\r"}
expect > {send exit\r}
expect eof
EOF`
expect -c "$expect_for_verify_which_active_machine"
./active.ksh
[Friday, February 24, 2012 2:32:06 PM IST] INFO Verify which is active SMU machine
spawn ssh 10.10.18.61
sh: /usr/local/bin/stty: not found
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
yes
Password:
Last login: Fri Feb 24 14:32:06 2012 from smu1a
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
solaris1:/ ROOT > hostname
solaris1
solaris1:/ ROOT > exit
logout
Connection to 10.10.18.61 closed.
How do I add timeout for "expect" in the following script? I want to set it as 120 seconds.
#!/bin/bash
HOST="localhost"
USER="myuname"
PASS="mypassword"
VAR=$(expect -c "
spawn ssh $USER@$HOST
expect \"password:\"
send \"$PASS\r\"
expect \"\\\\$\"
send \"ls\r\"
expect -re \"$USER.*\"
send \"logout\"
")
echo "==============="
echo "$VAR"