检查文件是否存在于远程服务器上
2017-05-16 11:45
267 查看
在有些情况下,你要测试文件是否存在于远程Linux服务器的某个目录下(例如:/var/run/test_daemon.pid),而无需登录到远程服务器进行交互。例如,你可能希望你的脚本根据特定文件是否存在的远程服务器上而由不同的行为。
在本教程中,我将向您展示如何使用不同的脚本语言(如:Bash shell,Perl,Python)查看远程文件是否存在。
这里描述的方法将使用ssh访问远程主机。您首先需要启用无密码的ssh登录到远程主机,这样您的脚本可以在非交互式的批处理模式访问远程主机。您还需要确保ssh登录文件有读权限检查。假设你已经完成了这两个步骤,您可以编写脚本就像下面的例子
使用bash判断文件是否存在于远程服务器上
使用perl判断文件是否存在于远程服务器上
使用python判断文件是否存在于远程服务器上
在本教程中,我将向您展示如何使用不同的脚本语言(如:Bash shell,Perl,Python)查看远程文件是否存在。
这里描述的方法将使用ssh访问远程主机。您首先需要启用无密码的ssh登录到远程主机,这样您的脚本可以在非交互式的批处理模式访问远程主机。您还需要确保ssh登录文件有读权限检查。假设你已经完成了这两个步骤,您可以编写脚本就像下面的例子
使用bash判断文件是否存在于远程服务器上
#!/bin/bash ssh_host="xmodulo@remote_server" file="/var/run/test.pid" if ssh $ssh_host test -e $file; then echo $file exists else echo $file does not exist fi
使用perl判断文件是否存在于远程服务器上
#!/usr/bin/perl my $ssh_host = "xmodulo@remote_server"; my $file = "/var/run/test.pid"; system "ssh", $ssh_host, "test", "-e", $file; my $rc = $? >> 8; if ($rc) { print "$file doesn't exist\n"; } else { print "$file exists\n"; }
使用python判断文件是否存在于远程服务器上
#!/usr/bin/python import subprocess import pipes ssh_host = 'xmodulo@remote_server' file = '/var/run/test.pid' resp = subprocess.call( ['ssh', ssh_host, 'test -e ' + pipes.quote(file)]) if resp == 0: print ('%s exists' % file) else: print ('%s does not exist' % file)
相关文章推荐
- 检查文件是否存在于远程服务器上
- asp.net检查服务器上目录或文件是否存在示例
- C#+WPF+oracle 检查文件服务器上文件是否存在
- ajax判断远程服务器是否存在某文件
- 如何验证远程服务器上文件是否存在
- curl检查远程文件是否存在
- 动软代码生成器,出现”添加服务器配置失败,请检查是否有写入权限或文件是否存在“的错误的解决办法
- Java判断远程服务器上文件是否存在(跨服务器)
- 动软中,在连接服务器时,出现“添加服务器配置失败,请检查是否有写入权限或文件是否存在“错误
- 如何验证远程服务器上文件是否存在
- 如何验证远程服务器上文件是否存在
- asp.net检查服务器上目录或文件是否存在的方法
- Java判断远程服务器上文件是否存在(跨服务器)
- java判断远程服务器上的文件是否存在的方法
- C# 检查网络是否连通,判断远程文件是否存在
- 动软出现“添加服务器配置失败,请检查是否有写入权限或文件是否存在“错误
- 动软中,在连接服务器时,出现“添加服务器配置失败,请检查是否有写入权限或文件是否存在“错误
- asp.net检查服务器上目录或文件是否存在的方法
- C# 检查网络是否连通 判断远程文件是否存在 C#获取程序路径的方法中需要注意的地方
- PHP下常见的检查函数(变量是否存在,文件是否存在 函数是否存在 方法是否存在)