check if file exists on remote host with ssh

后端 未结 11 721
醉酒成梦
醉酒成梦 2021-01-31 03:20

I would like to check if a certain file exists on the remote host. I tried this:

$ if [ ssh user@localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Devel         


        
11条回答
  •  日久生厌
    2021-01-31 03:21

    Here is a simple approach:

    #!/bin/bash
    USE_IP='-o StrictHostKeyChecking=no username@192.168.1.2'
    
    FILE_NAME=/home/user/file.txt
    
    SSH_PASS='sshpass -p password-for-remote-machine'
    
    if $SSH_PASS ssh $USE_IP stat $FILE_NAME \> /dev/null 2\>\&1
                then
                        echo "File exists"
                else
                        echo "File does not exist"
    
    fi
    

    You need to install sshpass on your machine to work it.

提交回复
热议问题