检查文件是否存在于远程服务器上
本文描述的方法是使用 ssh 访问远程主机。首先需要启用无密码的 ssh 登录到远程主机, 这样您的脚本可以在非交互式的批处理模式访问远程主机。您还需要确保 ssh 登录文件有读权限。
使用bash判断文件是否存在于远程服务器上
#!/bin/bash
ssh_host="owen@remote_machine"
file="/data/log/my_test.txt"
if ssh $ssh_host test -e $file;
then echo $file exists
else echo $file does not exist
fi
使用python判断文件是否存在于远程服务器上
#!/usr/bin/python
import pipes
import subprocess
ssh_host = 'owen@remote_machine'
file = '/data/log/my_test.txt'
resp = subprocess.call( ['ssh', ssh_host, 'test -e ' + pipes.quote(file)] )
if( 0 == resp ):
print ('{} exists'.format( file ) )
else:
print ('{} does not exist'.format( file ) )
使用perl判断文件是否存在于远程服务器上
#!/usr/bin/perl
my $ssh_host = "owen@remote_machine";
my $file = "/data/log/my_test.txt";
system "ssh", $ssh_host, "test", "-e", $file;
my $rc = $? >> 8;
if ($rc) {
print "$file does not exist\n";
} else {
print "$file exists\n";
}