开发者

getting user input while SSH'ed into another box

I have a bash script that basically should work like below:

  • get build number from user and put it in buildNum var

  • prep the build on local machine by calling a local script with buildNum as it's argument

  • sftp the prepped zip file to remote server1

  • Do this:

ssh -v $server1 <<EOF
rm -rvf path1-on-remote-server1/*
cd path2-on-remote-server1
unzip ../prepped-zip-file-$buildNum.zip
exit
EOF
  • sftp the prepped zip file to remote server2

The problem i am having is that on the forth step of number 4, $buildNum is not known to the remote server and it fails.

I tried the following two solutions and both failed:

  1. use double quotes "unzip ../prepped-zip-file-$buildNum.zip" which resulted in "unzip ../prepped-zip-file-11.6.zip: Command not found.

  2. tried to get the build number again from the user during the SSH session which failed again by not even waiting for my input and looking for a zip file without the build number at the end of the name, as the var was empty,

i did开发者_如何学运维 :

ssh -v $server1 <<EOF
   rm -rvf path1-on-remote-server1/*
   cd path2-on-remote-server1
   echo "enter build num once more: "
   read bNum
   unzip ../prepped-zip-file-$bNum.zip
   exit
   EOF

Any suggestions on how to achieve what i am after.

Thanks in advance


Are you sure this doesn't work?

ssh -v $server1 <<EOF
rm -rvf path1-on-remote-server1/*
cd path2-on-remote-server1
unzip ../prepped-zip-file-$buildNum.zip
exit
EOF

When I try it on my machine, with cat instead of ssh -v $server1 for testing, the variable does get substituted into the here-document, just as if the entire document had been on a command line. The remove shell never needs to know there was a variable in the first place.

Actually, though, you may want to give the remote command on the ssh command line rather than redirecting the standard input. This would be more robust in case some of the parts of it unexpectedly decide to try reading from stdin:

ssh -v $server1 "rm -rvf path1-on-remote-server1/*
  cd path2-on-remote-server1
  unzip ../prepped-zip-file-$buildNum.zip"

(Note that multi-line double-quoted strings are okay with bash).


#!/bin/sh
printf "Enter Build Number: "
read BUILD_NUM
cat << EOF | ssh $server1
hostname
echo "${BUILD_NUM}"
uptime
EOF

That works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜