我正在尝试执行下面的python脚本来获取远程服务器中的文件数。
此处的问题是根据日期创建的最后一个文件夹。为获取日期值而创建的grep命令不会在远程服务器中转换为日期。评论的查找代码将运行良好,但我希望将日期作为参数传递。任何帮助表示赞赏。
#!/usr/local/b2s/bin/python
import paramiko
import datetime
now = datetime.datetime.now()- datetime.timedelta(days = 1)
date= now.strftime("%Y%m%d")
print "date is" +date
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('host', username='username')
# grepCommand='find /usr/local/folder1/folder2/folder3/20170712_060013 -type f |wc -l'
grepCommand='find /usr/local/folder1/folder2/folder3/${date}_* -type f |wc -l'
stdin,stdout,stderr = client.exec_command(grepCommand)
data=stdout.readlines()
for i in data:
print(i.encode('ascii','replace'))
# print data
client.close()
答案 0 :(得分:0)
如果date
包含您希望其显示的日期,只需将该值插入grepCommand
字符串,如下所示:
grepCommand = 'find /usr/local/folder1/folder2/folder3/{}_* -type f |wc -l'.format(date)