代码之家  ›  专栏  ›  技术社区  ›  MT-FreeHK

不能在python子进程中使用gcloud compute ssh命令

  •  0
  • MT-FreeHK  · 技术社区  · 7 年前

    我已经设置了一个计算引擎,我可以在命令提示符中使用ssh命令,比如

    gcloud compute ssh test-instance@cloud-engine-noapi --zone us-central1-f --command "rm -f test.txt"
    

    并成功删除服务器中的test.txt。

    但是,当我在Python中调用这些函数时。

    subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"cd /home"'], shell=True)
    subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"ls -l"'], shell=True)
    subprocess.call(['gcloud', 'compute', '--project', 'test-project','ssh', temp_instance, '--zone', zone, '--command', '"rm -f /home/test.txt"'], shell=True)
    

    回报总是像

    bash: /command : No such file or directory
    

    为了命令 ls

    bash: /command : command not found
    

    我有什么事要先做吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   MT-FreeHK    7 年前

    with open(os.path.join(__location__, 'gcloud_command.bat'), 'w') as bat:
                command_arr = []
                for instance in all_instances_name:
                    temp_instance = user_name + "@" + instance
                    temp_file_path = '/home/' + user_name + '/'
                    command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' --command "cd ' + temp_file_path + '; rm -rf ' + temp_file_path + projectname.split('.')[0] + '; rm -f ' + temp_file_path + projectname.split('.')[0] + '*"\n')
                    command_arr.append('call gcloud compute scp "' + fullpath_projectname + '" ' + instance + ':' + temp_file_path + '\n')
                    if is_zip:
                        command_arr.append('call gcloud compute ssh ' + temp_instance + ' --zone ' + zone + ' ' + ' --command "cd ' + temp_file_path + '; unzip ' + temp_file_path + projectname + '"' + '\n')
                bat.writelines(command_arr)
    

    subprocess.Popen(os.path.join(__location__, 'gcloud_command.bat'))