judge remote file exist

 # -*- coding:utf-8 -*-
import paramiko,os,sys,time
print '''
*****判断远端服务器上的某个文件是否存在*****
'''
ip = raw_input("请输入目标主机的IP:》》")
password = raw_input("请输入目标主机的密码:》》")
#remote_file_path =raw_input("请输入文件所在目录的绝对路径:》》")
#remote_file_name = raw_input("请输入要查询的文件名称:》》")
print '''
*****正在连接远程主机,请稍等******
'''
time.sleep(1)
while True:
remote_file_path =raw_input("请输入文件所在目录的绝对路径:》》 按q退出程序")
if remote_file_path == 'q':
print "感谢使用,再见"
exit()
remote_file_name = raw_input("请输入要查询的文件名称:》》")
print '''
******正在判断,请稍等******
'''
'''def file_exist(ip,port,user,password):
result=""
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,user,password)
stdin, stdout, stderr = ssh.exec_command('ls %s'%remote_file_path )
list_str = stdout.read().split()
#print list_str
except:
print "未知错误"
return result b = file_exist(ip,22,'root',password)
print b
#if remote_file_name in file_exist(ip,22,'root',password):
# print "%s 文件存在"%remote_file_name
#else:
# print "%s 不存在"%remote_file_name'''
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,'root',password,timeout=3)
except:
print "未知错误"
stdin,stdout,stderr = ssh.exec_command('ls %s'%remote_file_path)
dir_list = stdout.read().split()
if remote_file_name in dir_list:
print "%s 存在"%remote_file_name
else:
print "%s 不存在"%remote_file_name
上一篇:Creating a Unique File Name


下一篇:[20170629]带过滤的复制项UI操作导致订阅全部初始化问题