【发布时间】:2019-09-25 14:34:29
【问题描述】:
我正在使用 paramiko 远程访问各种主机。 我可以在 Putty shell 中运行类似的命令。
我正在编写一个希望能做到这一点的 python 脚本,但我正在努力使用 for 循环来
这是我的目录示例
host01
directory1
file1
file2
file3
directory2
file1
file2
file3
host02
directory1
file1
file2
file3
directory2
file1
file2
file3
import paramiko
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='host01',username='loginid',password='password')
stdin,stdout,stderr=ssh_client.exec_command('''
cd ..
cd user/ze/log
cd *05-07-2019*
grep -c 1= file1
''')
for line in stdout.readlines():
print (line.strip())
for line in stderr.readlines():
print (line.strip())
等等……
最终我想为每个目录中的每个文件以某种模式grep。
我很难理解如何将变量用于上述内容。 任何帮助将不胜感激
【问题讨论】:
-
如果您只需要
grep,请仔细查看它的标志(特别是-R和产生机器可读输出的标志)——这可能是一个更好的选择。跨度>
标签: python unix ssh putty paramiko