Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Expect in Bash - and then compare md5sum Post 302977769 by bakunin on Friday 22nd of July 2016 02:45:25 AM
Old 07-22-2016
Quote:
Originally Posted by Don Cragun
But, whether or not they are the same topic, I don't understand why you are trying to rewrite code to perform what rsync already does??? What makes you think that rsync only compares file sizes to determine if a file has changed?
Amen to that!

In addition: whenever you start to use expect this is the confession that something went horribly wrong in your setup.

If you plan your authorisation correctly you should never have to rely on expect (which is just a way of having passwords written in cleartext into scripts typed automatically into forms) but use (preexchanged) keys or something similar. Instead of fiddling around with expect-scripts you should simply test for a possible ssh-connection and raise an error if that doesn't work (because keys are not exchanged, are invalid or some other reason).

Here is how i do that. It first tests for IP-connectivity and then - if that is possible - for ssh-connectivity:

Code:
# ------------------------------------------------------------------------------
# f_CheckConnectivity                          checking connectivity for a host
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 2016 01 25    by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   2007 01 18   Original Creation
#                       -
#
# - 1.00   2015 04 28   Production Version
#                       removed unecessary subshells
#
# - 1.01   2016 01 25   bugfix
#                       - changed ssh-call to cover for new hosts:
#                         added "-o 'CheckHostIP=no'"
#                         added "-o 'StrictHostKeyChecking=no'"
#
# ------------------------------------------------------------------------------
# Usage:
#     f_CheckConnectivity  char Hostname [ char user ]
#     checks the connectivity for the host given in $1 optionally using a
#     username given in $2. If no user name is given the current user is
#     assumed.
#
#     Example:  f_CheckConnectivity $host    # checks if $host can be worked on
#                                            # using the current user (usually
#                                            # this will be root)
#
# Prerequisites:
# -   to use this function, the FPATH variable must be set
#
# ------------------------------------------------------------------------------
# Documentation:
#     f_CheckConnectivity() checks in a successive manner. First an (IP)-ping
#     is issued. If this is successful a connection test is done by issuing
#     a command via ssh. f_CheckConnectivity() does NOT try to correct any
#     errors, merely stating them.
#
#     Parameters: char Host
#
#     returns:    0: connectivity test passed
#                 1: no ssh connection
#                 2: no IP connection
#                 3: parameter/other/internal error
#
# ------------------------------------------------------------------------------
# known bugs:
#
#     none
# ------------------------------------------------------------------------------
# .....................(C) 2007 Wolf Machowitsch ...............................
# ------------------------------------------------------------------------------

f_CheckConnectivity ()
{

$chFullDebug
                                                 # internal variables
typeset -i iRetVal=0                             # return value (see docu)
typeset    chHost="$1"                           # hostname
typeset    chUser="$2"                           # optional username

if [ -z "$1" ] ; then                            # check for prereqs
     iRetVal=3
elif [ "$chUser" == "" ] ; then
     chUser="$(who am i | cut -d' ' -f1)"
fi

if [ $iRetVal -eq 0 ] ; then
     if ! ping -c1 $chHost 1>/dev/null 2>&1 ; then
          iRetVal=2
     fi
fi
if [ $iRetVal -eq 0 ] ; then
     if ! ssh -nq                  \
              -o 'BatchMode=yes'   \
              -o 'CheckHostIP=no'  \
              -o 'StrictHostKeyChecking=no' \
                 ${chUser}@${chHost} date 1>/dev/null 2>&1; then
          iRetVal=1
     fi
fi

return $iRetVal

}
# --- EOF f_CheckConnectivity

Notice that you will have to adjust this if you deal with hosts in DMZs or behind firewalls, because they often block attempts to ping the host.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Expect in bash to get the return value

cat test.sh #!/bin/sh expect <<- EOF set timeout 5 spawn ssh -o StrictHostKeyChecking=no lyang0@128.224.178.245 -C mkdir -p /tmp expect { "Password:" {send "root\r"} } spawn scp -o StrictHostKeyChecking=no /tmp/1 lyang0@128.224.178.245:/tmp/ ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. Shell Programming and Scripting

Compare files in directories with md5sum

And not to start. I can compare files, that's easy. The problem is that I compare files in a directory, and check if these files exist in another directory. The problem is that the file names are not the same. So I have to compare with "md5sum" or something similar. How I can do? All this in... (7 Replies)
Discussion started by: Jomeaide
7 Replies

4. Shell Programming and Scripting

Expect - bash and variables

I was wondering if anyone could provide some assistance. I trying to run an expect script within bash and get the results of a variable called RESULT. I Have tried a few things but none of them have worked. I know that the child process (the expect script) in this instance cannot set a variable... (6 Replies)
Discussion started by: ylafont
6 Replies

5. Shell Programming and Scripting

Problems with expect and sftp in bash

I'm having trouble with some automated sftp pulls. I'm using expect inside bash scripts and spawning SFTP. Some times the expect seems bog down. I have tried to put sleeps in my code to give everything time to work before I move on to next step but I till continue to get issues. For example when... (2 Replies)
Discussion started by: gosteen
2 Replies

6. Shell Programming and Scripting

Compare md5sum two servers' setup

I'm trying to think of a way to compare two boxes and make sure their files will be the same. There may be extra files on one side and some on the other. I also need to make sure the file content is identical. So far I've gotten this to create a file find /directorypath/ -type f -name... (3 Replies)
Discussion started by: xgringo
3 Replies

7. 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

8. Shell Programming and Scripting

Bash expect problem

Hey there :) I have a Bash Script and I'm trying to update Roundcube, but theres a user interactive line like: bin/installto.sh /var/www/mail/rc Upgrading from 1.1.3. Do you want to continue? (y/N) I'm trying to avoid this user interaction like this: cd roundcubemail-1.2.1 >/dev/null... (5 Replies)
Discussion started by: Aeris
5 Replies

9. Shell Programming and Scripting

Compare two md5sum

Hello, First of all I want to apologize because i'm not a admin or coder and maybe all my efforts to write only this small script in my life would need one week full time reading man pages and forums but... I don't have the money to offer me to get this time and the script I want to do seems... (5 Replies)
Discussion started by: toscan
5 Replies

10. 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
All times are GMT -4. The time now is 02:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy