Execute readlink on a remote machine

*爱你&永不变心* 提交于 2019-12-11 09:55:14

问题


I want to extract the attached iSCSI device of a remote machine

dev_by_path="/dev/disk/by-path/ip-10.1.1.240:3260-iscsi-iqn.2013-12.com.ryussi:swift1-lun-0"

DEVICE=`ssh -i key.pem root@10.0.0.2 'bash -s' << 'ENDSSH'
basename $(readlink $dev_by_path)
ENDSSH`

It gives error:

readlink: missing operand
Try `readlink --help' for more information.
basename: missing operand
Try `basename --help' for more information

However if I do

DEVICE=`ssh -i key.pem root@10.0.0.2 'bash -s' << 'ENDSSH'
basename $(readlink "/dev/disk/by-path/ip-10.1.1.240:3260-iscsi-iqn.2013-12.com.ryussi:swift1-lun-0")
ENDSSH`

then it executes successfully and echo $DEVICE gives sda. How should I execute this.


回答1:


By quoting the string that ends the "here document", you've disabled subsitution for the variables contained within it. Your $dev_by_path variable is defined on the local side, not in the remotely executing shell. So, you want to expand that before executing the ssh command.

DEVICE=`ssh -i key.pem root@10.0.0.2 'bash -s' <<ENDSSH
basename $(readlink $dev_by_path)
ENDSSH`


来源:https://stackoverflow.com/questions/20782344/execute-readlink-on-a-remote-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!