Hi,
I am writing a menu driven program using shell script. THe script will be collecting data by logging into the other servers and bringing back the data to home server to process it and accordingly issue commands. TO automate commands execution , I am using expect script. However I am not able to understand , how to do error handling with expect script
I tried using the code tags . Somehw there is some issue . Hence pasting the code
#!/bin/awk -f
#set user [lindex $argv 0]
#set pswd [lindex $argv 1]
INTSRVUSR=$1;export INTSRVUSR
CGCUSERID=$2;export CGCUSERID
CGCPASS=$3;export CGCPASS
LABIP=$4;export LABIP
LABUSERID=$5;export LABUSERID
LABPASS=$6;export LABPASS
CURRENTDIR=`pwd`;export CURRENTDIR
FNAME="command";export FNAME
scptocgc(){
###---------------- scping the command file to the cgcuser
expect -c '
##set timeout 10
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CURRENTDIR)/AUTOBR/other_scripts/$env(FNAME) $env(CGCUSERID)@$env(INTSRVUSR):.
expect "assword:"
send "$env(CGCPASS)\r"
expect {
"directory" {
return "No such directory"
}
"ecamolx1820" {
return "error: file sent ## I am not able to display the
##error on prompt and also stop
## the function from further execution
}
}
expect sleep 1
spawn ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CGCUSERID)@$env(INTSRVUSR)
expect -nocase "assword:"
send "$env(CGCPASS)\r"
'
}
logintocgc(){
expect -c '
set timeout 60
###---------------- login in to the cgcuser
spawn ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CGCUSERID)@$env(INTSRVUSR)
expect -nocase "assword:"
send "$env(CGCPASS)\r"
expect {
"Access denied" {send "echo access denied;exit 1" }
"] " {send "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /home/vse/$env(CURRENTUSER)/$env(FNAME) mtc@10.1.77.148:/opt/swd/incoming/$env(FNAME)\r"
expect "assword:"
send "CDMA@123\r" }
expect {
"directory" {send "echo The file is not found\r";exit 1}
"] " {send "echo. The file is present\r"}
}
}
##It should not proceed if there are errors and stop the script here
expect "] "
send "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no mtc@10.1.77.148\r"
expect -nocase "assword:" {send "CDMA@123\r"}
expect "cli>" {send "exit\r" } # CLI
expect "] "
send "exit\r"
expect eof
'
sleep 1;
}
scptocgc
logintocgc
Sorry for the incomplete code. I had sent fragments of my code and hence only awk was present.
#!/bin/bash
#!/bin/expect -f
#set user [lindex $argv 0]
#set pswd [lindex $argv 1]
INTSRVUSR=$1;export INTSRVUSR
CGCUSERID=$2;export CGCUSERID
CGCPASS=$3;export CGCPASS
LABIP=$4;export LABIP
LABUSERID=$5;export LABUSERID
LABPASS=$6;export LABPASS
CURRENTDIR=`pwd`;export CURRENTDIR
FNAME="command";export FNAME
scptocgc(){
###---------------- scping the command file to the cgcuser
expect -c '
##set timeout 10
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CURRENTDIR)/AUTOBnR/other_scripts/$env(FNAME) $env(CGCUSERID)@$env(INTSRVUSR):.
expect "assword:"
send "$env(CGCPASS)\r"
expect {
"directory" {
send "touch ~/errorfile"; ## It doesn't touch the file here. It
breaks the expect script. But how
But I have to check at the end of
the file whether the operation got
completed or faced an error
return error;
}
"ecamolx1820" {
send "touch ~/filesent";
}
}
sleep 1
###---------------- login in to the cgcuser
spawn ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CGCUSERID)@$env(INTSRVUSR)
expect -nocase "assword:"
send "$env(CGCPASS)\r"
'
if [ -f ~/error >/dev/null ]
then
echo "We faced some error"
else
echo "no error faced";
fi
}
I'm not sure what you mean by "only awk was present". You do realize that if you invoke the script shown in your last post by name (i.e., ./scriptname where scriptname is the name of the file containing the code you showed us), the only line (of those shown above from the start of your script) that makes any difference in how the script is run, is the first line. Every other line (including: #!/bin/expect -f ) is just a comment. And, if you invoke this script by naming the interpreter to run the script and giving the script's name as an operand to that interpreter (i.e. ksh scriptname ), every single line shown here is just a comment. (And note that even though the 1st line implies that this script should be executed by bash , running it with ksh scriptname will run the script with ksh ; not bash .)
I got your point Don. Thanks for explaining that.
However I have seen that if I have to use awk in my shell script. I need to mention it to the system with statement
#!/bin/awk -f
other wise it throws error. I generally run the script as input to the interpreter. Even then awk statements throw an error. Hence I had used it.
Secondly my main query is still not resolved which is in relation to how to fulfill the functional requirement using expect script.
As I have mentioned in the between the code, I want expect to perform 2 operations on observing keyword directory.
1)touch ~/error
2)break from the expect function to execute the if condition.
It breaks from the expect script to check the if condition, however it does not create the error file
Can you help me out with the coding for expect ?
You can call other scripts (files) of other languages, if you really need multiple script languages
Bash, awk and expect are not compatible shebangs to mix the code, though, awk can be invoked as often you like when using bash.
However, you can use neither (afaik) bash nor awk from an expect script.
Aside from that, I dont see any awk invocation in your script at all anyway...
Are you aware that your variable 'FNAME' contains the hardcoded STRING 'command'?
And that you'r asking for an assword, rather than a password, several times?
Last but not least, have to admit i do not 'know' expect - AFAIK, isnt expect to be used so one does not have to pass passwords in plaintext to 'scripts'?
Yet you expect the user to pass his password to the script, which makes expect obsolete in some way.