Linux實(shí)現(xiàn)遠(yuǎn)程控制機(jī)器的方法-創(chuàng)新互聯(lián)

這篇文章主要介紹Linux實(shí)現(xiàn)遠(yuǎn)程控制機(jī)器的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站從2013年成立,先為姑蘇等服務(wù)建站,姑蘇等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為姑蘇企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

在Linux下進(jìn)行遠(yuǎn)程登陸的時(shí)候,總是要進(jìn)行ssh輸入賬號密碼,相對比較繁瑣。而在工作中總會先從本地登陸到公司的中間機(jī)器(跳板機(jī))然后才能登陸到線上的機(jī)器,每次操作更加繁瑣。如果從A->B可以直接進(jìn)行建立相互的信任關(guān)系來解決面輸入密碼的問題。顯示情況如果A->B->C三臺機(jī)器,如果想從A直接到C只能通過B進(jìn)行登錄,這樣就無法建立A->C的信任關(guān)系(這種操作主要是為了保護(hù)線上機(jī)器不能隨便登錄)。該腳本就是解決這種有多個(gè)依賴的關(guān)系。

注意事項(xiàng):

1.使用實(shí)時(shí)bashversion>=4.0,因?yàn)榕渲弥行枰褂藐P(guān)聯(lián)數(shù)據(jù)

2.如果需要全局使用直接修改autologin的名字,移動(dòng)到PATH路徑下即可eg:mvautologin/usrl/local/bin/to(改為自己想要使用的名字)

腳本代碼:

#!/usr/local/bin/bash

#@Version0.3.1

#@filenameto

#修復(fù)等不需要要配置跳板機(jī)的時(shí)候執(zhí)行命令,在配置跳板機(jī)位置默認(rèn)填no即可

#@Authorpemakoa@gmail.com

#Bashversion>=4.0使用關(guān)聯(lián)數(shù)組

#Usage:hostuserpasswdportjump_hostcommand

#四種情況如下:

#1.直接登錄目標(biāo)機(jī)器如A

#2.需要中間機(jī)器登陸到目標(biāo)機(jī)器如C,其中B為中間機(jī)器,會先登錄B在從B登陸到C然后執(zhí)行command

#3.直接登錄目標(biāo)機(jī)器并執(zhí)行相應(yīng)的命令如D

declare-A_server_config

_server_config['A']="a_hosta_usera_passwda_port"

_server_config['B']="b_hostb_userb_passwdb_port"

_server_config['C']="c_hostc_userc_passwdc_portB'(commandeg)ls.'"

_server_config['D']="d_hostd_userd_passwdd_portno'cd/home&&ll'"

_config_keys=(${!_server_config[@]})

_length=${#_server_config[@]}

_login_server=$1

_config_status=false

#是否輸入登陸機(jī)器

if["$_login_server"==""];then

echo-e"\033[40m\033[31mPleaseinputloginserver,youcanchooseonefollowslist\033[0m"

foriin"${_config_keys[@]}";do

echo-e"\033[41;37m$i\033[0m"

done

exit

fi

#檢查登陸的機(jī)器是否配置

foriin"${_config_keys[@]}";do

if["$_login_server"=="$i"];then

_config_status=true

fi

done

if["${_config_status}"=="false"];then

echo-ne"\033[40m\033[31m

Notconfigserverinfo...

Pleaseconfigin_server_configlike

HostUserPasswdPortJumpCommand\033[0m"

exit

fi

#登陸如果配置跳板機(jī),先登陸跳板機(jī)在登陸到目標(biāo)機(jī)器

_host=$(echo${_server_config["${_login_server}"]}|awk'{print$1}')

_user=$(echo${_server_config["${_login_server}"]}|awk'{print$2}')

_passwd=$(echo${_server_config["${_login_server}"]}|awk'{print$3}')

_port=$(echo${_server_config["${_login_server}"]}|awk'{print$4}')

_jump=$(echo${_server_config["${_login_server}"]}|awk'{print$5}')

_command=$(echo${_server_config["${_login_server}"]}|awk-F"'"'{print$2}')

if["${_command}"!=""];then

_command="expect\"*]*\"

send\"${_command}\r\""

fi

if["${_jump}"!=""]&&["${_jump}"!="no"];then

_jump_host=$(echo${_server_config["${_jump}"]}|awk'{print$1}')

_jump_user=$(echo${_server_config["${_jump}"]}|awk'{print$2}')

_jump_passwd=$(echo${_server_config["${_jump}"]}|awk'{print$3}')

_jump_port=$(echo${_server_config["${_jump}"]}|awk'{print$4}')

expect-c"

settimeout30

spawnssh-p${_jump_port}${_jump_user}@${_jump_host}

expect{

\"yes/no\"{send\"yes\r\";exp_continue}

\"assword\"{send\"${_jump_passwd}\r\"}

}

expect\"*]*\"

send\"ssh-p${_port}${_user}@${_host}\r\"

expect\"assword:\"

send\"${_passwd}\r\"

${_command}

interact"

else

expect-c"

settimeout30

spawnssh-p${_port}${_user}@${_host}

expect{

\"yes/no\"{send\"yes\r\";exp_continue}

\"*assword:\"{send\"$_passwd\r\"}

}

${_command}

interact

"

fi

以上是“Linux實(shí)現(xiàn)遠(yuǎn)程控制機(jī)器的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文題目:Linux實(shí)現(xiàn)遠(yuǎn)程控制機(jī)器的方法-創(chuàng)新互聯(lián)
當(dāng)前地址:http://bm7419.com/article34/cdggse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、App開發(fā)網(wǎng)站改版、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)