shell脚本内使用ssh连接远程机器, 如何使用变量

问题遇到的现象和发生背景

如题, linux环境下, shell脚本内如何用ssh连接远程机器后, 使用shell脚本内的变量
同时, 远程机器执行命令后输出的结果, 如何赋值到变量里面
下面是我预想中的代码, 自己测了下是行不通的, 麻烦指正下两台机器之间如何变量互通, 或者单向通也行, 这样可以让代码变得简单
如果都不行, 我可以在远程机器把输出内容写文件里, scp回来再处理

问题相关代码,请勿粘贴截图
#!/bin/sh

path_list=(/root/path1 /root/path2 /root/path3)
ret=""
ssh -Tq root@192.168.0.100 << EOF
for path in ${path_list[*]}
do
  output=`echo $path`
  ret=${ret}${output}
done
exit
EOF

echo $ret

可以把要执行的命令写在shell脚本中,然后通过python的subprocess获取脚本执行结果

status, _ = subprocess.getstatusoutput('sh test.sh')

可以把要添加的环境变量写入到用户的环境变量脚本中,但是我试过各种方法在ssh命令中加载环境变量,好像都没有成功。这个你可以再研究一下


#!/bin/sh
path_list=(/root/path1 /root/path2 /root/path3)
ret=""
host=$1
for path in ${path_list[*]}
do
  output=`echo $path`
  ret=${ret}${output}
done
ssh -Tq $host << EOF
echo 'export ret='$ret >> ~/.bash_profile
echo '.  ~/.bash_profile' > ~/a.sh
source ~/a.sh
EOF

trap "echo $ret" EXIT