检查文件是否存在于远程服务器上

检查文件是否存在于远程服务器上

本文描述的方法是使用 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";  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值