0

I'm trying to figure out a way to ssh to a remote file, check whether a file exists, and if it exist ssh back to the first server, execute a command and get done. Here is a snippet of my code so far:

for file_name in $(ls $FILE_DIR/)
do
    ssh [email protected]: home/some/directory
    'if [ -f $DIRECTORY/$file_name ]
    then
        ssh [email protected]: home/some/Directory
        'scp [email protected]: $DIRECTORY/$file_name $DIRECTORY/$file_name"_"$(date +%Y%m%d%H%M%S)


    fi''
        scp [email protected] $FILE_DIR/$file_name $DIRECTORY/$file_name

When I execute the script, the connection to the remote server opens up in the command line and the rest of the script doesn't get executed.

Also, is what I'm trying to do valid? Can I ssh within an server that has been accesed through ssh?

1
  • Did you try connecting and executing the ssh command manually? I think the first time you connect to a server, ssh gives a prompt about RSA fingerprint. Commented Aug 21, 2013 at 7:07

2 Answers 2

2

So I have a couple suggestions:

  1. When running remote commands, sometimes its important to simulate a tty. In order to do that, Try ssh -t -t to force pseudo-tty allocation even if stdin is not a terminal.

  2. Have you thought about using salt-stack ? Salt allows you to run remote commands, in parallel on multiple machines across a network. Salt is an amazing solution for running remote commands on other machines, and its extremely easy to setup.

  3. Try running your remote command with bash -x before running the script, that way, you should see all output needed to help you debug the situation. If thats not enough, make sure you redirect standard out/error to a file to see what went wrong eg:

    ssh [email protected] 'bash -x ./script_2004.5664 | tee -a ~/some.log > 2>&1'

Sign up to request clarification or add additional context in comments.

2 Comments

What does tty do? I mean how would that help me?
@user2591144 if you run visudo on your server, you will be able to look at the sudoers file and see if requiretty is set( which it is by default ). certain commands will only run when the user is logged in to a real tty. To read up on why this is needed heres a good article: pentestmonkey.net/blog/ssh-with-no-tty. Btw running remote commands with salt is as easy as: salt 'someserver' cmd.run 'bash /path/to/script.sh' ( you don't have to worry about tty etc and salt is extremely lightweight ).
1

First of all, do not use ls to iterate over files. Bash can do that on its own.

for file_name in "$FILE_DIR"/*

Also use [[ ]] instead of [

Here is the script that probably does what you want:

for filename in "$FILE_DIR/"*; do
    # Check if file exists on the remote server
    if [[ $(ssh [email protected] "[[ -f $DIRECTORY/$filename ]] && echo 1") ]]; then
        # Download that file
        scp [email protected]: "$DIRECTORY/$filename" "$DIRECTORY/${filename}_$(date +%Y%m%d%H%M%S)"
    fi
done

4 Comments

What does && echo 1") part do exactly?
@user2591144 the point is that inside [[ ]] empty strings are falsey while non empty strings are not. So if the file exists, then it will echo 1 (or you can use any other string), which means that the if statement is going to be true.
$DIRECTORY is in the form of home/some/directory I run the script and the files don't get renamed despite the fact that there are matching names in the target. It seems like the script flow doesn't enter the if statement. Can you think of a reason?
@user2591144 are you sure that the files are found? Try echoing them before the if statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.