Run if statement on remote machine

I'm attempting to run a set of commands on a remote equipment, that consists of an if declaration. I'm utilizing this to closure a checklist of kvm circumstances. The first line needs to be great, yet can be prettier, remainder requires some aid.

ssh [email protected] 'virsh shutdown $host 2> /dev/null; virsh destroy $host 2> /dev/null; sleep 2; virsh undefine $host 2> /dev/null'

# Also needs to be run on remote machine
# to disconnect iscsi
iscsiadm -m session | grep $host
if [ $? == 0 ]; then
  iscsiadm -m node -T $stserver.$host -u
fi

# then on local machine
tid=`ssh [email protected] 'cat /proc/net/iet/volume' | grep $host | head -1 | awk '{print $1}' | awk -F: '{print $2}'`
if [ $tid ]; then
  echo "Deleting tid:$tid from $stserver."
  ssh [email protected]$stserver "ietadm --op delete --tid=$tid"
fi

I presume I could do the very same for the 2nd section, simply different all of it with ; yet if any person mosts likely to read my code later they 'd possibly be damaging their heads

Is there a means to layout all this so its legible and also still makes good sense and also have all the remote commands execute effectively?

4
2022-06-07 14:30:20
Source Share
Answers: 3

Maybe simply add discourse to clarify points that (should be) noticeable? I do this consistently in manuscripts to advise my very own oblivious butt what I depended on when I created the [censored ] point to begin with.

0
2022-06-07 14:51:39
Source

You can make use of HEREDOCS, e.g:

ssh [email protected] <<EOF
  virsh shutdown $host 2> /dev/null
  virsh destroy $host 2> /dev/null
  sleep 2
  virsh undefine $host 2> /dev/null

  # to disconnect iscsi
  iscsiadm -m session | grep $host
  if [ \$? == 0 ] ; then
    iscsiadm -m node -T $stserver.$host -u
  fi
EOF

Please realize that I ran away $? as or else it would certainly be reviewed by your neighborhood covering and also not from your remote covering. As $host appears to be specified in your area you will not need to leave it.

Simply a tiny comment though you really did not ask for it. You can from another location run virsh commands, without the demand to clearly ssh right into the host, e.g:

virsh -c qemu+ssh://[email protected]/system destroy host

will certainly function. You additionally do not require ssh as libvirt permits you to confirm with ssl certs insteads.

3
2022-06-07 14:51:20
Source
  1. Put your lengthy and also intricate series of commands in its very own shell script, allow is call it virsh-shutdown-remote.sh

  2. Run ssh and also usage input redirection to run the manuscript:

    ssh [email protected] < virsh-shutdown-remote.sh
    

Putting the commands in a manuscript is an excellent suggestion regardless.

As an added pointer, as opposed to this:

iscsiadm -m session | grep $host
if [ $? == 0 ]; then
  iscsiadm -m node -T $stserver.$host -u
fi

You can write the very same point less complex and also much shorter on a solitary line:

iscsiadm -m session | grep $host && iscsiadm -m node -T $stserver.$host -u
4
2022-06-07 14:44:25
Source