Check If File Exists on Remote Host with Ssh

check if file exists on remote host with ssh

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.

Check if a file exists on a remote server with spaces in path

ssh command accepts only one argument as an command, as described in synopsis of manual page:

SYNOPSIS
ssh [...] [user@]hostname [command]

You need to adhere with that if you want correct results. Good start is to put whole command into the quotes (and escape any inner quotes), like this:

ssh -qi ~/.ssh/key root@$1 "[[ -f \"$SONG\" ]] && echo \"File exists\" || echo \"File does not exist\""

It should solve your issue.

How do I check to see if a file exists on a remote server using shell

Assuming you are using scp and ssh for remote connections something like this should do what you want.

declare -a array1=('user1@user1.user.com');

for i in "${array1[@]}"; do
if ssh -q "$i" "test -f /home/user/directory/file"; then
scp "$i:/home/user/directory/file" /local/path
else
echo 'Could not access remote file.'
fi
done

Alternatively, if you don't necessarily need to care about the difference between the remote file not existing and other possible scp errors then the following would work.

declare -a array1=('user1@user1.user.com');

for i in "${array1[@]}"; do
if ! scp "$i:/home/user/directory/file" /local/path; then
echo 'Remote file did not exist.'
fi
done

Verify a file exists over ssh

If the server accepts sftp sessions, I wouldn't bother with pexpect, but instead use the paramiko SSH2 module for Python:

import paramiko
transport=paramiko.Transport("10.10.0.0")
transport.connect(username="service",password="word")
sftp=paramiko.SFTPClient.from_transport(transport)
filestat=sftp.stat("/opt/ad/bin/email_tidyup.sh")

The code opens an SFTPClient connection to the server, on which you can use stat() to check for the existance of files and directories.

sftp.stat will raise an IOError ('No such file') when the file doesn't exist.

If the server doesn't support sftp, this would work:

import paramiko
client=paramiko.SSHClient()
client.load_system_host_keys()
client.connect("10.10.0.0",username="service",password="word")
_,stdout,_=client.exec_command("[ -f /opt/ad/bin/email_tidyup.sh ] && echo OK")
assert stdout.read()

SSHClient.exec_command returns a triple (stdin,stdout,stderr). Here we just check for the presence of any output. You might instead vary the command or check stderr for any error messages instead.

Using JSch, is there a way to tell if a remote file exists without doing an ls?

(This is if you're using the SFTP part of the library, an assumption I made without thinking about it.)

I thought its ls(String path) would accept filenames; I can't check at the moment.

If it doesn't, you don't need to iterate manually; you can use the selector variant:

ls(String path, ChannelSftp.LsEntrySelector selector)

Paramiko / scp - check if file exists on remote host

Use paramiko's SFTP client instead. This example program checks for existence before copy.

#!/usr/bin/env python

import paramiko
import getpass

# make a local test file
open('deleteme.txt', 'w').write('you really should delete this]n')

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('localhost', username=getpass.getuser(),
password=getpass.getpass('password: '))
sftp = ssh.open_sftp()
sftp.chdir("/tmp/")
try:
print(sftp.stat('/tmp/deleteme.txt'))
print('file exists')
except IOError:
print('copying file')
sftp.put('deleteme.txt', '/tmp/deleteme.txt')
ssh.close()
except paramiko.SSHException:
print("Connection Error")

How to check if a file exists on a server with bash

How about this?

ssh user@host 'if [ -f /path/to/my/file.txt ]; then echo yes; else echo no; fi'


Related Topics



Leave a reply



Submit