0

I have a setup which behaves in a way i cannot get my head around. So I have a local bash script generateTrafficOnHosts.sh which reads IP addresses from a file. It then calls a bash script getTraffic.sh on each remote host, i.e. each IP address it calls. However, after the first remote execution via ssh, it just finishes. Here's my local generateTrafficOnHosts.sh script:

#!/bin/bash
# Connects to a series of hosts and runs the get-traffic.sh script on     them. 
# The hosts are taken from a file with a list of randomly generated IDs,
# while their IDs are taken from a connectfile.

# ...
# some convenience functions here
# ...

#######################################
# runs the traffic generation script on
# a host.
# Globals:
#   
# Arguments:
#   <IP of the remote host>
# Returns:
#   None
#######################################
function run_on_host() {
    local RESULTS
    RESULTS=$(ssh -i key.pem user@"$1" '/home/user/get-traffic.sh 2>&1 >/dev/null')
}

#######################################
# Runs the load on the hosts
# Globals:
#   HOSTSFILE
# Arguments:
#   None
# Returns:
#   None
#######################################
function run_load_on_hosts() {
    while read line
    do
        local ip=$(get_ip_for_key ${line})
        echo "getting IP for key in line ${line}"
        if [[ $ip != "" ]]; then
            echo "running on host $line($ip)"
            run_on_host "${ip}"
        else
            echo "error: LINE'$line', IP'$ip'"
        fi
    done < "$HOSTSFILE"
}

function main() {
    check "$@"
    run_load_on_hosts
}

main "$@"

The remote script get-traffic.sh looks like this:

#!/bin/bash
PORT=47111

# Time limit in seconds
TIME_LIMIT=10
# Blocksize of the dd command: how much data shall be downloaded per chunk? 
BS=1M

while [[ "$SECONDS" -le "$TIME_LIMIT" ]]; do
    # $SECONDS is a shell variable
    nc "${HOST}" "${PORT}" | dd count=1 bs="${BS}" iflag=fullblock > /dev/null
done

Now for the odd behaviour: the script only runs for the first host in the hosts file. If I replace the line ssh -i ... with, say, sleep 1; echo "running...", everything runs smoothly. The same applies if I replace the remote command /home/user/get-traffic.sh with e.g. ls /home/user - everything is good. However, the script also returns after the first iteration if I replace the remote command with ls.

I have been tracking this bug for hours now, and I have run out of ideas hours ago. Anyone, please?!? :|

1 Answer 1

1

Look at the ssh manual, more specificaly the -t option.

If this doesn't work, try the -tt option. Also, I think your get-traffic.sh script should exit 0 at the end.

2
  • hm, both options unfortunately don't change a thing :(
    – Filex
    Apr 13, 2015 at 18:27
  • BUT you pointed me somehow in the right direction. Allocating a pseudo-terminal is not needed, since my script always has one attached. However, the remote script needed some stdin - hence using -n did the trick. Unfortunately, I cannot upvote you since I have a new account here. But cheers!
    – Filex
    Apr 13, 2015 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .