第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Shell腳本:通過(guò)ssh從腳本運(yùn)行功能

Shell腳本:通過(guò)ssh從腳本運(yùn)行功能

慕田峪4524236 2019-12-26 08:45:21
是否有任何聰明的方法可以通過(guò)ssh在遠(yuǎn)程主機(jī)上運(yùn)行本地Bash功能?例如:#!/bin/bash#Definition of the functionf () {  ls -l; }#I want to use the function locallyf#Execution of the function on the remote machine.ssh user@host f#Reuse of the same function on another machine.ssh user@host2 f是的,我知道這行不通,但是有辦法實(shí)現(xiàn)嗎?
查看完整描述

3 回答

?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

您可以使用該typeset命令通過(guò)來(lái)使功能在遠(yuǎn)程計(jì)算機(jī)上可用ssh。有多個(gè)選項(xiàng),具體取決于您要如何運(yùn)行遠(yuǎn)程腳本。


#!/bin/bash

# Define your function

myfn () {  ls -l; }

要在遠(yuǎn)程主機(jī)上使用該功能:


typeset -f myfn | ssh user@host "$(cat); myfn"

typeset -f myfn | ssh user@host2 "$(cat); myfn"

更好的是,為什么還要麻煩管道:


ssh user@host "$(typeset -f myfn); myfn"

或者,您可以使用HEREDOC:


ssh user@host << EOF

    $(typeset -f myfn)

    myfn

EOF

如果要發(fā)送腳本中定義的所有函數(shù),而不僅僅是發(fā)送myfn,請(qǐng)typeset -f像這樣使用:


ssh user@host "$(typeset -f); myfn"

說(shuō)明


typeset -f myfn將顯示的定義myfn。


cat將以文本形式接收該函數(shù)的定義,$()并將在當(dāng)前的shell中執(zhí)行它,該shell將成為遠(yuǎn)程shell中的已定義函數(shù)。最后,該功能可以執(zhí)行。


最后的代碼將在ssh執(zhí)行之前將函數(shù)的定義內(nèi)聯(lián)。


查看完整回答
反對(duì) 回復(fù) 2019-12-26
?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

我個(gè)人不知道您問(wèn)題的正確答案,但是我有很多安裝腳本只是使用ssh復(fù)制自身。


讓命令復(fù)制文件,加載文件功能,運(yùn)行文件功能,然后刪除文件。


ssh user@host "scp user@otherhost:/myFile ; . myFile ; f ; rm Myfile"


查看完整回答
反對(duì) 回復(fù) 2019-12-26
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

另一種方式:


#!/bin/bash

# Definition of the function

foo () {  ls -l; }


# Use the function locally

foo


# Execution of the function on the remote machine.

ssh user@host "$(declare -f foo);foo"

declare -f foo 打印功能的定義


查看完整回答
反對(duì) 回復(fù) 2019-12-26
  • 3 回答
  • 0 關(guān)注
  • 1082 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)