You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
18 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
3 years ago
3 years ago
8 years ago
3 years ago
3 years ago
8 years ago
3 years ago
3 years ago
3 years ago
3 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Script to deploy certificates to remote server by SSH
  3. # Note that SSH must be able to login to remote host without a password...
  4. # SSH Keys must have been exchanged with the remote host. Validate and
  5. # test that you can login to USER@SERVER from the host running acme.sh before
  6. # using this script.
  7. #
  8. # The following variables exported from environment will be used.
  9. # If not set then values previously saved in domain.conf file are used.
  10. #
  11. # Only a username is required. All others are optional.
  12. #
  13. # The following examples are for QNAP NAS running QTS 4.2
  14. # export DEPLOY_SSH_CMD="" # defaults to "ssh -T"
  15. # export DEPLOY_SSH_USER="admin" # required
  16. # export DEPLOY_SSH_SERVER="host1 host2:8022 192.168.0.1:9022" # defaults to domain name, support multiple servers with optional port
  17. # export DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
  18. # export DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
  19. # export DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
  20. # export DEPLOY_SSH_FULLCHAIN=""
  21. # export DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
  22. # export DEPLOY_SSH_BACKUP="" # yes or no, default to yes or previously saved value
  23. # export DEPLOY_SSH_BACKUP_PATH=".acme_ssh_deploy" # path on remote system. Defaults to .acme_ssh_deploy
  24. # export DEPLOY_SSH_MULTI_CALL="" # yes or no, default to no or previously saved value
  25. # export DEPLOY_SSH_USE_SCP="" yes or no, default to no
  26. # export DEPLOY_SSH_SCP_CMD="" defaults to "scp -q"
  27. #
  28. # Optional Config Sets
  29. # To run multiple ssh deployments with different configrations, define suffixes for each run:
  30. # export DEPLOY_SSH_CONFIG_SETS="_QNAP _UNIFI"
  31. #
  32. # Then define the configuration for each set by suffixing the above configuration values, e.g.:
  33. # export DEPLOY_SSH_USER_QNAP="admin" # required
  34. # export DEPLOY_SSH_SERVER_QNAP="192.168.0.1:9022" # defaults to domain name, support multiple servers with optional port
  35. # ...
  36. # export DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
  37. #
  38. # export DEPLOY_SSH_USER_UNIFI="administrator" # required
  39. # export DEPLOY_SSH_SERVER_UNIFI="192.168.0.2" # defaults to domain name, support multiple servers with optional port
  40. # ...
  41. # export DEPLOY_SSH_REMOTE_UNIFI="service unifi restart"
  42. #
  43. ######## Public functions #####################
  44. #domain keyfile certfile cafile fullchain
  45. ssh_deploy() {
  46. _cdomain="$1"
  47. _ckey="$2"
  48. _ccert="$3"
  49. _cca="$4"
  50. _cfullchain="$5"
  51. _deploy_ssh_servers=""
  52. _debug _cdomain "$_cdomain"
  53. _debug _ckey "$_ckey"
  54. _debug _ccert "$_ccert"
  55. _debug _cca "$_cca"
  56. _debug _cfullchain "$_cfullchain"
  57. _migratedeployconf Le_Deploy_ssh_user DEPLOY_SSH_USER
  58. _getdeployconf DEPLOY_SSH_USER
  59. _getdeployconf DEPLOY_SSH_CONFIG_SETS
  60. if [ -z "$DEPLOY_SSH_USER" ] && [ -z "$DEPLOY_SSH_CONFIG_SETS" ]; then
  61. _err "DEPLOY_SSH_USER or DEPLOY_SSH_CONFIG_SETS must be defined."
  62. return 1
  63. fi
  64. if [ -n "$DEPLOY_SSH_USER" ]; then
  65. _info "Running with base env (no config suffixes)"
  66. if ! _ssh_load_config; then
  67. return 1
  68. fi
  69. _deploy_ssh_servers="$_sshServer"
  70. for _sshServer in $_deploy_ssh_servers; do
  71. _ssh_deploy
  72. done
  73. fi
  74. if [ -n "$DEPLOY_SSH_CONFIG_SETS" ]; then
  75. _debug2 DEPLOY_SSH_CONFIG_SETS "$DEPLOY_SSH_CONFIG_SETS"
  76. _savedeployconf DEPLOY_SSH_CONFIG_SETS "$DEPLOY_SSH_CONFIG_SETS"
  77. for _config_suffix in $DEPLOY_SSH_CONFIG_SETS; do
  78. _info "Running with config suffix $_config_suffix"
  79. if ! _ssh_load_config "$_config_suffix"; then
  80. return 1
  81. fi
  82. _deploy_ssh_servers="$_sshServer"
  83. for _sshServer in $_deploy_ssh_servers; do
  84. _ssh_deploy
  85. done
  86. done
  87. fi
  88. }
  89. _ssh_load_config() {
  90. _config_suffix="$1"
  91. _deploy_ssh_servers=""
  92. # USER is required to login by SSH to remote host.
  93. _migratedeployconf Le_Deploy_ssh_user"${_config_suffix}" DEPLOY_SSH_USER"${_config_suffix}"
  94. _getdeployconf DEPLOY_SSH_USER"${_config_suffix}"
  95. _sshUser=$(eval echo \$DEPLOY_SSH_USER"${_config_suffix}")
  96. _debug2 DEPLOY_SSH_USER"${_config_suffix}" "$_sshUser"
  97. if [ -z "$_sshUser" ]; then
  98. _err "DEPLOY_SSH_USER${_config_suffix} not defined."
  99. return 1
  100. fi
  101. _savedeployconf DEPLOY_SSH_USER"${_config_suffix}" "$_sshUser"
  102. # SERVER is optional. If not provided then use _cdomain
  103. _migratedeployconf Le_Deploy_ssh_server"${_config_suffix}" DEPLOY_SSH_SERVER"${_config_suffix}"
  104. _getdeployconf DEPLOY_SSH_SERVER"${_config_suffix}"
  105. _sshServer=$(eval echo \$DEPLOY_SSH_SERVER"${_config_suffix}")
  106. _debug2 DEPLOY_SSH_SERVER"${_config_suffix}" "$_sshServer"
  107. if [ -z "$_sshServer" ]; then
  108. _sshServer="$_cdomain"
  109. fi
  110. _savedeployconf DEPLOY_SSH_SERVER"${_config_suffix}" "$_sshServer"
  111. # CMD is optional. If not provided then use ssh
  112. _migratedeployconf Le_Deploy_ssh_cmd"${_config_suffix}" DEPLOY_SSH_CMD"${_config_suffix}"
  113. _getdeployconf DEPLOY_SSH_CMD"${_config_suffix}"
  114. _sshCmd=$(eval echo \$DEPLOY_SSH_CMD"${_config_suffix}")
  115. _debug2 DEPLOY_SSH_CMD"${_config_suffix}" "$_sshCmd"
  116. if [ -z "$_sshCmd" ]; then
  117. _sshCmd="ssh -T"
  118. fi
  119. _savedeployconf DEPLOY_SSH_CMD"${_config_suffix}" "$_sshCmd"
  120. # BACKUP is optional. If not provided then default to previously saved value or yes.
  121. _migratedeployconf Le_Deploy_ssh_backup"${_config_suffix}" DEPLOY_SSH_BACKUP"${_config_suffix}"
  122. _getdeployconf DEPLOY_SSH_BACKUP"${_config_suffix}"
  123. _sshBackup=$(eval echo \$DEPLOY_SSH_BACKUP"${_config_suffix}")
  124. _debug2 DEPLOY_SSH_BACKUP"${_config_suffix}" "$_sshBackup"
  125. if [ -z "$_sshBackup" ]; then
  126. _sshBackup="yes"
  127. fi
  128. _savedeployconf DEPLOY_SSH_BACKUP"${_config_suffix}" "$_sshBackup"
  129. # BACKUP_PATH is optional. If not provided then default to previously saved value or .acme_ssh_deploy
  130. _migratedeployconf Le_Deploy_ssh_backup_path"${_config_suffix}" DEPLOY_SSH_BACKUP_PATH"${_config_suffix}"
  131. _getdeployconf DEPLOY_SSH_BACKUP_PATH"${_config_suffix}"
  132. _sshBackupPath=$(eval echo \$DEPLOY_SSH_BACKUP_PATH"${_config_suffix}")
  133. _debug2 DEPLOY_SSH_BACKUP_PATH"${_config_suffix}" "$_sshBackupPath"
  134. if [ -z "$_sshBackupPath" ]; then
  135. _sshBackupPath=".acme_ssh_deploy"
  136. fi
  137. _savedeployconf DEPLOY_SSH_BACKUP_PATH"${_config_suffix}" "$_sshBackupPath"
  138. # MULTI_CALL is optional. If not provided then default to previously saved
  139. # value (which may be undefined... equivalent to "no").
  140. _migratedeployconf Le_Deploy_ssh_multi_call"${_config_suffix}" DEPLOY_SSH_MULTI_CALL"${_config_suffix}"
  141. _getdeployconf DEPLOY_SSH_MULTI_CALL"${_config_suffix}"
  142. _multiCall=$(eval echo \$DEPLOY_SSH_MULTI_CALL"${_config_suffix}")
  143. _debug2 DEPLOY_SSH_MULTI_CALL"${_config_suffix}" "$_multiCall"
  144. if [ -z "$_multiCall" ]; then
  145. _multiCall="no"
  146. fi
  147. _savedeployconf DEPLOY_SSH_MULTI_CALL"${_config_suffix}" "$_multiCall"
  148. # KEYFILE is optional.
  149. # If provided then private key will be copied to provided filename.
  150. _migratedeployconf Le_Deploy_ssh_keyfile"${_config_suffix}" DEPLOY_SSH_KEYFILE"${_config_suffix}"
  151. _getdeployconf DEPLOY_SSH_KEYFILE"${_config_suffix}"
  152. _keyFile=$(eval echo \$DEPLOY_SSH_KEYFILE"${_config_suffix}")
  153. _debug2 DEPLOY_SSH_KEYFILE"${_config_suffix}" "$_keyFile"
  154. if [ -n "$_keyFile" ]; then
  155. _savedeployconf DEPLOY_SSH_KEYFILE"${_config_suffix}" "$_keyFile"
  156. fi
  157. # CERTFILE is optional.
  158. # If provided then certificate will be copied or appended to provided filename.
  159. _migratedeployconf Le_Deploy_ssh_certfile"${_config_suffix}" DEPLOY_SSH_CERTFILE"${_config_suffix}"
  160. _getdeployconf DEPLOY_SSH_CERTFILE"${_config_suffix}"
  161. _certFile=$(eval echo \$DEPLOY_SSH_CERTFILE"${_config_suffix}")
  162. _debug2 DEPLOY_SSH_CERTFILE"${_config_suffix}" "$_certFile"
  163. if [ -n "$_certFile" ]; then
  164. _savedeployconf DEPLOY_SSH_CERTFILE"${_config_suffix}" "$_certFile"
  165. fi
  166. # CAFILE is optional.
  167. # If provided then CA intermediate certificate will be copied or appended to provided filename.
  168. _migratedeployconf Le_Deploy_ssh_cafile"${_config_suffix}" DEPLOY_SSH_CAFILE"${_config_suffix}"
  169. _getdeployconf DEPLOY_SSH_CAFILE"${_config_suffix}"
  170. _caFile=$(eval echo \$DEPLOY_SSH_CAFILE"${_config_suffix}")
  171. _debug2 DEPLOY_SSH_CAFILE"${_config_suffix}" "$_caFile"
  172. if [ -n "$_caFile" ]; then
  173. _savedeployconf DEPLOY_SSH_CAFILE"${_config_suffix}" "$_caFile"
  174. fi
  175. # FULLCHAIN is optional.
  176. # If provided then fullchain certificate will be copied or appended to provided filename.
  177. _migratedeployconf Le_Deploy_ssh_fullchain"${_config_suffix}" DEPLOY_SSH_FULLCHAIN"${_config_suffix}"
  178. _getdeployconf DEPLOY_SSH_FULLCHAIN"${_config_suffix}"
  179. _fullChain=$(eval echo \$DEPLOY_SSH_FULLCHAIN"${_config_suffix}")
  180. _debug2 DEPLOY_SSH_FULLCHAIN"${_config_suffix}" "$_fullChain"
  181. if [ -n "$_fullChain" ]; then
  182. _savedeployconf DEPLOY_SSH_FULLCHAIN"${_config_suffix}" "$_fullChain"
  183. fi
  184. # REMOTE_CMD is optional.
  185. # If provided then this command will be executed on remote host.
  186. _migratedeployconf Le_Deploy_ssh_remote_cmd"${_config_suffix}" DEPLOY_SSH_REMOTE_CMD"${_config_suffix}"
  187. _getdeployconf DEPLOY_SSH_REMOTE_CMD"${_config_suffix}"
  188. _remoteCmd=$(eval echo \$DEPLOY_SSH_REMOTE_CMD"${_config_suffix}")
  189. _debug2 DEPLOY_SSH_REMOTE_CMD"${_config_suffix}" "$_remoteCmd"
  190. if [ -n "$_remoteCmd" ]; then
  191. _savedeployconf DEPLOY_SSH_REMOTE_CMD"${_config_suffix}" "$_remoteCmd"
  192. fi
  193. # USE_SCP is optional. If not provided then default to previously saved
  194. # value (which may be undefined... equivalent to "no").
  195. _getdeployconf DEPLOY_SSH_USE_SCP"${_config_suffix}"
  196. _useScp=$(eval echo \$DEPLOY_SSH_USE_SCP"${_config_suffix}")
  197. _debug2 DEPLOY_SSH_USE_SCP"${_config_suffix}" "$_useScp"
  198. if [ -z "$_useScp" ]; then
  199. _useScp="no"
  200. fi
  201. _savedeployconf DEPLOY_SSH_USE_SCP"${_config_suffix}" "$_useScp"
  202. # SCP_CMD is optional. If not provided then use scp
  203. _getdeployconf DEPLOY_SSH_SCP_CMD"${_config_suffix}"
  204. _scpCmd=$(eval echo \$DEPLOY_SSH_SCP_CMD"${_config_suffix}")
  205. _debug2 DEPLOY_SSH_SCP_CMD"${_config_suffix}" "$_scpCmd"
  206. if [ -z "$_scpCmd" ]; then
  207. _scpCmd="scp -q"
  208. fi
  209. _savedeployconf DEPLOY_SSH_SCP_CMD"${_config_suffix}" "$_scpCmd"
  210. if [ "$_useScp" = "yes" ]; then
  211. _multiCall="yes"
  212. _info "Using scp as alternate method for copying files. Multicall Mode is implicit"
  213. elif [ "$_multiCall" = "yes" ]; then
  214. _info "Using MULTI_CALL mode... Required commands sent in multiple calls to remote host"
  215. else
  216. _info "Required commands batched and sent in single call to remote host"
  217. fi
  218. }
  219. _ssh_deploy() {
  220. _err_code=0
  221. _cmdstr=""
  222. _backupprefix=""
  223. _backupdir=""
  224. _local_cert_file=""
  225. _local_ca_file=""
  226. _local_full_file=""
  227. case $_sshServer in
  228. *:*)
  229. _host=${_sshServer%:*}
  230. _port=${_sshServer##*:}
  231. ;;
  232. *)
  233. _host=$_sshServer
  234. _port=
  235. ;;
  236. esac
  237. _info "Deploy certificates to remote server $_sshUser@$_host:$_port"
  238. if [ "$_sshBackup" = "yes" ]; then
  239. _backupprefix="$_sshBackupPath/$_cdomain-backup"
  240. _backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
  241. # run cleanup on the backup directory, erase all older
  242. # than 180 days (15552000 seconds).
  243. _cmdstr="{ now=\"\$(date -u +%s)\"; for fn in $_backupprefix*; \
  244. do if [ -d \"\$fn\" ] && [ \"\$(expr \$now - \$(date -ur \$fn +%s) )\" -ge \"15552000\" ]; \
  245. then rm -rf \"\$fn\"; echo \"Backup \$fn deleted as older than 180 days\"; fi; done; }; $_cmdstr"
  246. # Alternate version of above... _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf; $_cmdstr"
  247. # Create our backup directory for overwritten cert files.
  248. _cmdstr="mkdir -p $_backupdir; $_cmdstr"
  249. _info "Backup of old certificate files will be placed in remote directory $_backupdir"
  250. _info "Backup directories erased after 180 days."
  251. if [ "$_multiCall" = "yes" ]; then
  252. if ! _ssh_remote_cmd "$_cmdstr"; then
  253. return $_err_code
  254. fi
  255. _cmdstr=""
  256. fi
  257. fi
  258. if [ -n "$_keyFile" ]; then
  259. if [ "$_sshBackup" = "yes" ]; then
  260. # backup file we are about to overwrite.
  261. _cmdstr="$_cmdstr cp $_keyFile $_backupdir >/dev/null;"
  262. if [ "$_multiCall" = "yes" ]; then
  263. if ! _ssh_remote_cmd "$_cmdstr"; then
  264. return $_err_code
  265. fi
  266. _cmdstr=""
  267. fi
  268. fi
  269. # copy new key into file.
  270. if [ "$_useScp" = "yes" ]; then
  271. # scp the file
  272. if ! _scp_remote_cmd "$_ckey" "$_keyFile"; then
  273. return $_err_code
  274. fi
  275. else
  276. # ssh echo to the file
  277. _cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $_keyFile;"
  278. _info "will copy private key to remote file $_keyFile"
  279. if [ "$_multiCall" = "yes" ]; then
  280. if ! _ssh_remote_cmd "$_cmdstr"; then
  281. return $_err_code
  282. fi
  283. _cmdstr=""
  284. fi
  285. fi
  286. fi
  287. if [ -n "$_certFile" ]; then
  288. _pipe=">"
  289. if [ "$_certFile" = "$_keyFile" ]; then
  290. # if filename is same as previous file then append.
  291. _pipe=">>"
  292. elif [ "$_sshBackup" = "yes" ]; then
  293. # backup file we are about to overwrite.
  294. _cmdstr="$_cmdstr cp $_certFile $_backupdir >/dev/null;"
  295. if [ "$_multiCall" = "yes" ]; then
  296. if ! _ssh_remote_cmd "$_cmdstr"; then
  297. return $_err_code
  298. fi
  299. _cmdstr=""
  300. fi
  301. fi
  302. # copy new certificate into file.
  303. if [ "$_useScp" = "yes" ]; then
  304. # scp the file
  305. _local_cert_file=$(_mktemp)
  306. if [ "$_certFile" = "$_keyFile" ]; then
  307. cat "$_ckey" >>"$_local_cert_file"
  308. fi
  309. cat "$_ccert" >>"$_local_cert_file"
  310. if ! _scp_remote_cmd "$_local_cert_file" "$_certFile"; then
  311. return $_err_code
  312. fi
  313. else
  314. # ssh echo to the file
  315. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $_certFile;"
  316. _info "will copy certificate to remote file $_certFile"
  317. if [ "$_multiCall" = "yes" ]; then
  318. if ! _ssh_remote_cmd "$_cmdstr"; then
  319. return $_err_code
  320. fi
  321. _cmdstr=""
  322. fi
  323. fi
  324. fi
  325. if [ -n "$_caFile" ]; then
  326. _pipe=">"
  327. if [ "$_caFile" = "$_keyFile" ] ||
  328. [ "$_caFile" = "$_certFile" ]; then
  329. # if filename is same as previous file then append.
  330. _pipe=">>"
  331. elif [ "$_sshBackup" = "yes" ]; then
  332. # backup file we are about to overwrite.
  333. _cmdstr="$_cmdstr cp $_caFile $_backupdir >/dev/null;"
  334. if [ "$_multiCall" = "yes" ]; then
  335. if ! _ssh_remote_cmd "$_cmdstr"; then
  336. return $_err_code
  337. fi
  338. _cmdstr=""
  339. fi
  340. fi
  341. # copy new certificate into file.
  342. if [ "$_useScp" = "yes" ]; then
  343. # scp the file
  344. _local_ca_file=$(_mktemp)
  345. if [ "$_caFile" = "$_keyFile" ]; then
  346. cat "$_ckey" >>"$_local_ca_file"
  347. fi
  348. if [ "$_caFile" = "$_certFile" ]; then
  349. cat "$_ccert" >>"$_local_ca_file"
  350. fi
  351. cat "$_cca" >>"$_local_ca_file"
  352. if ! _scp_remote_cmd "$_local_ca_file" "$_caFile"; then
  353. return $_err_code
  354. fi
  355. else
  356. # ssh echo to the file
  357. _cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $_caFile;"
  358. _info "will copy CA file to remote file $_caFile"
  359. if [ "$_multiCall" = "yes" ]; then
  360. if ! _ssh_remote_cmd "$_cmdstr"; then
  361. return $_err_code
  362. fi
  363. _cmdstr=""
  364. fi
  365. fi
  366. fi
  367. if [ -n "$_fullChain" ]; then
  368. _pipe=">"
  369. if [ "$_fullChain" = "$_keyFile" ] ||
  370. [ "$_fullChain" = "$_certFile" ] ||
  371. [ "$_fullChain" = "$_caFile" ]; then
  372. # if filename is same as previous file then append.
  373. _pipe=">>"
  374. elif [ "$_sshBackup" = "yes" ]; then
  375. # backup file we are about to overwrite.
  376. _cmdstr="$_cmdstr cp $_fullChain $_backupdir >/dev/null;"
  377. if [ "$_fullChain" = "yes" ]; then
  378. if ! _ssh_remote_cmd "$_cmdstr"; then
  379. return $_err_code
  380. fi
  381. _cmdstr=""
  382. fi
  383. fi
  384. # copy new certificate into file.
  385. if [ "$_useScp" = "yes" ]; then
  386. # scp the file
  387. _local_full_file=$(_mktemp)
  388. if [ "$_fullChain" = "$_keyFile" ]; then
  389. cat "$_ckey" >>"$_local_full_file"
  390. fi
  391. if [ "$_fullChain" = "$_certFile" ]; then
  392. cat "$_ccert" >>"$_local_full_file"
  393. fi
  394. if [ "$_fullChain" = "$_caFile" ]; then
  395. cat "$_cca" >>"$_local_full_file"
  396. fi
  397. cat "$_cfullchain" >>"$_local_full_file"
  398. if ! _scp_remote_cmd "$_local_full_file" "$_fullChain"; then
  399. return $_err_code
  400. fi
  401. else
  402. # ssh echo to the file
  403. _cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $_fullChain;"
  404. _info "will copy fullchain to remote file $_fullChain"
  405. if [ "$_multiCall" = "yes" ]; then
  406. if ! _ssh_remote_cmd "$_cmdstr"; then
  407. return $_err_code
  408. fi
  409. _cmdstr=""
  410. fi
  411. fi
  412. fi
  413. # cleanup local files if any
  414. if [ -f "$_local_cert_file" ]; then
  415. rm -f "$_local_cert_file"
  416. fi
  417. if [ -f "$_local_ca_file" ]; then
  418. rm -f "$_local_ca_file"
  419. fi
  420. if [ -f "$_local_full_file" ]; then
  421. rm -f "$_local_full_file"
  422. fi
  423. if [ -n "$_remoteCmd" ]; then
  424. _cmdstr="$_cmdstr $_remoteCmd;"
  425. _info "Will execute remote command $_remoteCmd"
  426. if [ "$_multiCall" = "yes" ]; then
  427. if ! _ssh_remote_cmd "$_cmdstr"; then
  428. return $_err_code
  429. fi
  430. _cmdstr=""
  431. fi
  432. fi
  433. # if commands not all sent in multiple calls then all commands sent in a single SSH call now...
  434. if [ -n "$_cmdstr" ]; then
  435. if ! _ssh_remote_cmd "$_cmdstr"; then
  436. return $_err_code
  437. fi
  438. fi
  439. # cleanup in case all is ok
  440. return 0
  441. }
  442. #cmd
  443. _ssh_remote_cmd() {
  444. _cmd="$1"
  445. _ssh_cmd="$_sshCmd"
  446. if [ -n "$_port" ]; then
  447. _ssh_cmd="$_ssh_cmd -p $_port"
  448. fi
  449. _secure_debug "Remote commands to execute: $_cmd"
  450. _info "Submitting sequence of commands to remote server by $_ssh_cmd"
  451. # quotations in bash cmd below intended. Squash travis spellcheck error
  452. # shellcheck disable=SC2029
  453. $_ssh_cmd "$_sshUser@$_host" sh -c "'$_cmd'"
  454. _err_code="$?"
  455. if [ "$_err_code" != "0" ]; then
  456. _err "Error code $_err_code returned from ssh"
  457. fi
  458. return $_err_code
  459. }
  460. # cmd scp
  461. _scp_remote_cmd() {
  462. _src=$1
  463. _dest=$2
  464. _scp_cmd="$_scpCmd"
  465. if [ -n "$_port" ]; then
  466. _scp_cmd="$_scp_cmd -P $_port"
  467. fi
  468. _secure_debug "Remote copy source $_src to destination $_dest"
  469. _info "Submitting secure copy by $_scp_cmd"
  470. $_scp_cmd "$_src" "$_sshUser"@"$_host":"$_dest"
  471. _err_code="$?"
  472. if [ "$_err_code" != "0" ]; then
  473. _err "Error code $_err_code returned from scp"
  474. fi
  475. return $_err_code
  476. }