Alternate to SLEEP for EXPECT within BASH script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate to SLEEP for EXPECT within BASH script?
# 1  
Old 02-06-2012
Alternate to SLEEP for EXPECT within BASH script?

Fairly new to the System Admin world, and this is my first post here, hoping to get some clarification.

I am using a BASH script to automate some Logfile Archiving (into .tars). The actual logfiles are accessed through an SSH, so I have used the following EXPECT sub-script within my main BASH script (which includes tons of other stuff) :
Code:
 expect - << EOF
 spawn ssh $serverName
 match_max 100000
 expect -exact "$SIGNEDIN@$serverName\'s password: "
 send -- "$PASS\r"
 expect -exact "\[$SIGNEDIN@$serverName ~\]# "
 send -- "cd /folder1/logfiles\r"
 expect -exact "\[$SIGNEDIN@$serverName logfiles\]# "
 send -- "tar -cvf logs.$serverName.$CURRENTDATE.tar *.log\r"
 SLEEP 30s
 expect -exact "\[$SIGNEDIN@$serverName logfiles\]# "
 send -- "mv *.tar LOG_ARCHIVE\r"
 expect -exact "\[$SIGNEDIN@$serverName logfiles\]# "
 send -- "exit\r"
 EOF

All of the variables are set earlier in the script and this is actually working exactly as it should . . . but only when the filesizes are small. Some of the servers have small logfiles and some have huge logfiles. The small ones finish long before the 30s SLEEP, but yet sometimes the 30s SLEEP is not long enough for the larger servers. This causes the "mv" command to run before it finishes, and then the whole process is shot. I could increase the SLEEP timer to an even larger number to make sure the "tar -cvf" command completes . . . . but I am already impatient enough. There has got to be a better way to do this.

Anyone please chime in with some ideas, I am willing to try anything (except keys, I cannot use keys)

Last edited by Franklin52; 02-07-2012 at 03:51 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-06-2012
ssh is designed to prevent you from using stored plaintext passwords, because they're very insecure; "interactive password authentication" means "password typed by a human being in realtime authentication" and nothing else will do. That's why you've had to use a third-party brute-forcing utility to make it work at all.

If you use ssh the proper way, with keys, you won't need to sledgehammer in an insecure plaintext password with a third-party brute-forcing utility like expect, and will be able to automate things far, far easier since it can take input exactly as given with no mess, and won't cough up its own skull when the remote script reacts in an even slightly unexpected way:

Code:
#!/bin/sh
ssh -i /path/to/keys username@server <<EOF
        cd /folder1/logfiles
        tar -cvf logs.$serverName.$CURRENTDATE.tar *.log
        mv *.tar LOG_ARCHIVE
EOF

When using keys, it really is that simple, because you don't have to fight it.

google 'passwordless ssh' to find out how you're supposed to do it. There's hundreds of tutorials all over the internet.

Last edited by Corona688; 02-06-2012 at 05:43 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-06-2012
Wow, thanks for the quick response Corona. Do you get payed for this stuff, every single page I have been in today you are usually the first to respond Smilie.

I understand what you are saying, but it is not my call. They don't want keys, and I am not authorized to set up keys.

The users have this process, that is a very repetitive sign-in / .tar / sign-out over and over and over again for each server. This is a one use script, it is not something that is running constantly with a unsecured stored password. They wanted something that automates it while only needing one Sign-In/Password. I have the earlier section set up just like thier regular Sign-In/Password prompt. The password has echo off and is stored in $PASS only for the current session (which is closed afterwards).

Thanks for the ideas, and believe me I wish I could talk them into keys, but it just isn't in the cards. Hopefully someone can shove me in the right direction, or point me towards something I haven't figured out yet.
# 4  
Old 02-06-2012
If they have to type a password anyway, why not let ssh do the password work? You can do as I did above without the keys, and ssh will ask for a password once to do all the commands in the here-document...

For work with multiple servers, I have a key with a password on it, which I cache with ssh-agent. It asks me once for my key password, on login, and I can use it until I log out to access any servers that were given that key. It's still password protected, but with one password entry, not 9,000.

The ultimate argument is this: If they hate the idea of keys, they'd be absolutely livid to know you were using a third-party brute-force hacking utility to sledgehammer in passwords in an insecure manner instead.

Last edited by Corona688; 02-06-2012 at 06:05 PM..
# 5  
Old 02-06-2012
No no. I do not have the password stored on the script. They enter thier sign-in/password in the beginning of the script. And it is pulled into a variable while the script is running. As far as I know the variable isn't saved within the text of the script, it is cleared once the script exits and the session closes (I could be incorrect, cause I haven't been doing this too long).
I just need an alternate way, I am not authorized to use keys.

EDIT: Oh you must have snipe edited, your post said something a minute ago.
# 6  
Old 02-06-2012
Sorry, I'm known to do that sometimes.
# 7  
Old 02-06-2012
Hah, no problem. I tried it without the expect, just basic command line and it gets to the point of asking username and PW, then when entered just sits there. The script doesn't continue on if I remember correctly. Thats why I had to dig down in the depths for expect/send.
Perhaps I am misunderstanding your post though. Please clarify a bit if possible
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Bash script + expect

im very happy to back for this forum I have servers with alias of double dns extentions: sample: servera.test.com servera.test1.com serverb.test.com serverb.test1.com I need to login to that severs and executing the set of commands if test.com failed then try to login via... (0 Replies)
Discussion started by: prakash0106
0 Replies

2. Shell Programming and Scripting

Bash script with expect

Dear all Hi I want use expect in bash so that we can not use these with each other /bin/bash. With. /usr/bin/expect How can use these with on script or how can call a script from other script #!/bin/bash clear echo "================================== " echo "Enter your Esxi IP"... (3 Replies)
Discussion started by: Baber
3 Replies

3. Shell Programming and Scripting

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows: #!/bin/bash HOST=$1 /usr/bin/expect -c " spawn ssh -i /root/.ssh/id_rsa root@$HOST expect -exact "Enter... (3 Replies)
Discussion started by: John Wilson
3 Replies

4. Shell Programming and Scripting

Acces Variable from expect-Script in bash-Script

Hi all, I have a little problem with a expect in a bash Script. The hull of my script: #!/bin/sh ( expect -c ' set a \"eee\"; # the variable a ' ) echo $a; # using the variable out of the expect script I would like to use the variable out of the expect script(in bash),... (3 Replies)
Discussion started by: gandalfthepink
3 Replies

5. Shell Programming and Scripting

Expect script called in loop from Bash Script

Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass). Here is... (0 Replies)
Discussion started by: cbo0485
0 Replies

6. Shell Programming and Scripting

Cannot get this bash/expect script to run under a crontab

#!/bin/bash # # RAP configuration script # # Usage: ./rap.sh # # Requires: expect, tcl # # Script expects to find a file called rap.csv located in the same directory as the script. If the file is placed # in a different directory, modify the custom entries section to specify the absolute... (8 Replies)
Discussion started by: mrkool
8 Replies

7. Shell Programming and Scripting

expect in bash script

Hi, I'm writing a shell script that calls a few commands that prompt the user for two simple yes/no questions. if the answers are consistent (the first is a yes, the second is a no), what would my expect script look like? Google is only giving me answers for scripts where I telnet or ssh. right now... (3 Replies)
Discussion started by: js741
3 Replies

8. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

9. Shell Programming and Scripting

question about "sleep" command in expect script

I wrote some expect script to telnet to some device to execute some commands.Firstly,I can't get full result some time,then I try to add some "sleep" command in it.Fortunately it works. My idea about it is that it uses sleep command to wait the result to be displayed.Am I right or correct the... (4 Replies)
Discussion started by: robbiezr
4 Replies

10. Shell Programming and Scripting

Wants alternate to the Sleep option??

I am new to Shell Scripting and I need help to write the following script in a different format... This is the current script: #!/usr/bin/ksh environment=rms export environment . $AW_HOME/RETEK/exec/RETEK_ENVAR ls -ltr $MMPOS/RTLOG* | tr -s " " | cut -d " " -f9... (20 Replies)
Discussion started by: satyajit007
20 Replies
Login or Register to Ask a Question