Python 检查";“差别”;使用shell脚本的命令
我使用以下命令将linux计算机上的一个文件与远程计算机上的另一个文件(两个AWS实例)进行比较:Python 检查";“差别”;使用shell脚本的命令,python,linux,amazon-web-services,shell,ubuntu,Python,Linux,Amazon Web Services,Shell,Ubuntu,我使用以下命令将linux计算机上的一个文件与远程计算机上的另一个文件(两个AWS实例)进行比较: diff-s main首先,我建议使用cmp而不是diff(我认为它更有效),但这种解决方案应该可以用任何一种方法 您只需编写一个bash脚本,其中包含if语句。如果cmp或diff命令未返回任何内容,则无需执行任何操作。在另一种情况下,您只需要将当前的scp主文件转移到远程主机 如果您决定使用cmp,则If语句只需如下所示: if cmp -s main <(ssh -i /home/ub
diff-s main首先,我建议使用cmp
而不是diff
(我认为它更有效),但这种解决方案应该可以用任何一种方法
您只需编写一个bash脚本,其中包含if语句。如果cmp
或diff
命令未返回任何内容,则无需执行任何操作。在另一种情况下,您只需要将当前的scp
主
文件转移到远程主机
如果您决定使用cmp
,则If语句只需如下所示:
if cmp -s main <(ssh -i /home/ubuntu/sai_key.pem ubuntu@w.x.y.z 'cat /home/ubuntu/c1')
then
echo "Match!"
else
echo "No match!"
scp ...
fi
确保您
chmod
脚本也具有正确的权限 首先,我建议使用cmp
而不是diff
(我认为它更有效),但这种解决方案应该是双向的
您只需编写一个bash脚本,其中包含if语句。如果cmp
或diff
命令未返回任何内容,则无需执行任何操作。在另一种情况下,您只需要将当前的scp
主文件转移到远程主机
如果您决定使用cmp
,则If语句只需如下所示:
if cmp -s main <(ssh -i /home/ubuntu/sai_key.pem ubuntu@w.x.y.z 'cat /home/ubuntu/c1')
then
echo "Match!"
else
echo "No match!"
scp ...
fi
确保您
chmod
脚本也具有正确的权限 不需要bash、diff、cmd、cron。。。Python可以在ssh的帮助下完成一切:
import subprocess
import time
key_pair = 'AWS_Linux_key_pair.pem'
remote_name = 'ec2-user@ec2-3-16-214-98.us-east-2.compute.amazonaws.com'
file_name = 'fibonacci.py'
cat_string = "cat " + file_name
while True:
command = 'ssh -i ' + key_pair + ' ' + remote_name + " '" + cat_string + "'"
remote_lines = subprocess.getoutput(command)
local_lines = subprocess.getoutput(cat_string)
if remote_lines != local_lines:
print("update")
command = 'scp -i ' + key_pair + ' ' + file_name + ' ' + remote_name + ':'
subprocess.getoutput(command)
else:
print("the same")
time.sleep(30)
不需要bash、diff、cmd、cron。。。Python可以在ssh的帮助下完成一切:
import subprocess
import time
key_pair = 'AWS_Linux_key_pair.pem'
remote_name = 'ec2-user@ec2-3-16-214-98.us-east-2.compute.amazonaws.com'
file_name = 'fibonacci.py'
cat_string = "cat " + file_name
while True:
command = 'ssh -i ' + key_pair + ' ' + remote_name + " '" + cat_string + "'"
remote_lines = subprocess.getoutput(command)
local_lines = subprocess.getoutput(cat_string)
if remote_lines != local_lines:
print("update")
command = 'scp -i ' + key_pair + ' ' + file_name + ' ' + remote_name + ':'
subprocess.getoutput(command)
else:
print("the same")
time.sleep(30)
我知道OP标记了这一点,但即使如此,这也很难比本质上的shell单行程序有所改进。shell one衬里(有7条线),这只是一个草图:-)再也没有了。我刚刚提出的替代方案也适用于iMac和Windows。
scp
和ssh
不是Windows上的标准安装。Python也是。我猜你会在Windows上痛击:-)@AlexanderLopatin谢谢你提供的信息!我还需要实现与Python非常相似的东西。这将是有帮助的,我知道OP标记了这一点,但即使如此,这也很难比本质上的shell一行程序有所改进。shell one衬里(有7条线),这只是一个草图:-)再也没有了。我刚刚提出的替代方案也适用于iMac和Windows。scp
和ssh
不是Windows上的标准安装。Python也是。我猜你会在Windows上痛击:-)@AlexanderLopatin谢谢你提供的信息!我还需要实现与Python非常相似的东西。这将是有帮助的