如何搜索我在下面返回的文件
在下面的代码中,我使用 Paramiko 远程登录到嵌入式站点服务器并检索所有 .log 和 .txt 文件,并将它们放在本地计算机上的文件夹中,以搜索可能清晰的潜在 pin #。在第二段代码中,这是脚本的一部分,可以解压缩 .tgz 文件并执行 ascii、hex 等字符串的搜索...我发现远程获取文件并不划算,并且认为它更好登录时只搜索嵌入式设备上的所有 .log 和 .txt。但是,我仍然是一个 Python 新手,我花了很长时间才想出我现在使用的代码。由于时间原因,我请求帮助。有人可以告诉我如何使用下面的代码来实现更多 exec_commands (我已经有要搜索的代码 - 在第一个代码下面)?我只是不确定在哪里以及如何实施它。谢谢!
import paramiko
import sys
import os
import re
sim_ip = raw_input('Host: ')
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if re.match(pattern, sim_ip):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sim_ip, username='root', password='******')
apath = '/'
apattern = '"*.txt" -o -name "*.log"'
rawcommand = 'find {path} -name "*.txt" -o -name "*.log"' #{pattern}
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
ftp = ssh.open_sftp()
for afile in filelist:
(head, filename) = os.path.split(afile)
print(filename)
#ftp.get(afile, 'c:\\Extracted\\' + filename) #'./'+filename)
ftp.close()
ssh.close()
else:
print "You entered an invalid IP Address!!!"
这是我当前用于搜索日志和文本文件的代码:
print "\nDirectory to be searched: " + directory
print "\nFile result2.log will be created in: c:\Temp_log_files."
paths = "c:\\Temp_log_files\\result2.log"
temp = file(paths, "w")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ''.join(str(ord(char)) for char in userstring)
regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII )))
goby = raw_input("Press Enter to begin search (search ignores whitespace)!\n")
for root,dirname, files in os.walk(directory):
for file1 in files:
if file1.endswith(".log") or file1.endswith(".txt"):
f=open(os.path.join(root, file1))
for i,line in enumerate(f.readlines()):
result = regex.search(line)
if result:
ln = str(i)
pathnm = os.path.join(root,file1)
template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
output = template.format(ln, pathnm, result.group())
print output
temp.write(output)
break
else:
print "String Not Found in: " + os.path.join(root,file1)
temp.write("\nString Not Found: " + os.path.join(root,file1) + "\n")
f.close()
re.purge()
In this code below, I'm using Paramiko to login remotely to an embedded site server and retrieve all .log and .txt files and place them in a folder on my local machine to search for potential pin # that may be in the clear. In the second piece of code, this is part of a script that can unzip .tgz files and perform searches of strings in ascii, hex, etc.... I find that getting the files remotely is not cost effective, and think it's better to just search for all .log and .txt on the embedded device while logged in. However, I'm still a Python novice and it took me a long time to come up with the code that I use now. I'm asking for assistance for the sake of time. Can someone tell me how I would use the code below to implement more exec_commands (I already have the code to search - below the 1st code)? I'm just unsure of where and how to implement it. Thanks!
import paramiko
import sys
import os
import re
sim_ip = raw_input('Host: ')
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if re.match(pattern, sim_ip):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sim_ip, username='root', password='******')
apath = '/'
apattern = '"*.txt" -o -name "*.log"'
rawcommand = 'find {path} -name "*.txt" -o -name "*.log"' #{pattern}
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
ftp = ssh.open_sftp()
for afile in filelist:
(head, filename) = os.path.split(afile)
print(filename)
#ftp.get(afile, 'c:\\Extracted\\' + filename) #'./'+filename)
ftp.close()
ssh.close()
else:
print "You entered an invalid IP Address!!!"
Here is the code that I currently use to search log and text files:
print "\nDirectory to be searched: " + directory
print "\nFile result2.log will be created in: c:\Temp_log_files."
paths = "c:\\Temp_log_files\\result2.log"
temp = file(paths, "w")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ''.join(str(ord(char)) for char in userstring)
regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII )))
goby = raw_input("Press Enter to begin search (search ignores whitespace)!\n")
for root,dirname, files in os.walk(directory):
for file1 in files:
if file1.endswith(".log") or file1.endswith(".txt"):
f=open(os.path.join(root, file1))
for i,line in enumerate(f.readlines()):
result = regex.search(line)
if result:
ln = str(i)
pathnm = os.path.join(root,file1)
template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
output = template.format(ln, pathnm, result.group())
print output
temp.write(output)
break
else:
print "String Not Found in: " + os.path.join(root,file1)
temp.write("\nString Not Found: " + os.path.join(root,file1) + "\n")
f.close()
re.purge()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
paramiko 有内置的类,它们完全可以完成您在第一个代码部分中尝试执行的操作。
可以归结为
阅读他们的API文档: http://www.lag.net/paramiko/docs/
paramiko has inbuilt classes that do exactly what you're trying to do in your first code section.
Can be boiled down to
Read their API documentation: http://www.lag.net/paramiko/docs/
根据我在此页面中读到的内容,当想要执行多个命令时,每个命令必须在
exec_command()
中写在不同的行上。但我不知道这是否可行:
或者
由于你的问题,我刚刚发现了 Paramiko。
我可以使用哪个 SSH2 站点来运行您的代码?我没有 SFTP 或其他类型的帐户来执行真正的论文。
。
我对你在命令定义中写“find”感到困惑
我在 SSH2 和 SFTP 命令列表中没有看到此命令
“找到”对应什么?
According to what I read in this page, when one wants several commands to be executed, each command must be written in
exec_command()
on a distinct line.But I don't know if this would work:
or
I've just discovered Paramiko thanks to your question.
What SSH2 site could I use to run your code ? I have no SFTP or other type account to perform real essays.
.
I am puzzled by the fact you write 'find' in the definition of a command
I didn't see this command among lists of SSH2 and SFTP commands
What 'find' correspond to ?