linux互信(shell版本)

2021-10-07 06:45:11 字數 3190 閱讀 1839

#之前已經發表了乙個python版本的互信**,以及乙個打包好的互信程式,可以不用考慮安裝其他一些軟體,不如expect之類的。建議用python版本的,好用,方便。上傳這個是因為怕自己換電腦的時候**沒有挪過去,在寫的時候還要思考。

下面的是shell編寫的

#!/bin/sh

#author liwentong

#time 2023年

#$0 autossh.sh

dest_user=

$1password=

$2hosts_file=

$3if

[ $# -ne 3 ]; then

echo

"usage:"

echo

"$0 remoteuser remotepassword hostsfile"

exit 1

fissh_dir=~/.ssh

script_prefix=./tmp

echo

====

====

====

====

====

*****==

# 1. prepare directory .ssh

mkdir

$ssh_dir

chmod 700 $ssh_dir

# 2. generat ssh key

tmp_script=

$script_prefix.sh

echo

"#!/usr/bin/expect"

>

$tmp_script

echo

"spawn ssh-keygen -b 1024 -t rsa"

>>

$tmp_script

echo

"expect *key*"

>>

$tmp_script

echo

"send \r"

>>

$tmp_script

if[ -f $ssh_dir/id_rsa ]

;then

echo

"expect *verwrite*"

>>

$tmp_script

echo

"send y\r"

>>

$tmp_script

fiecho

"expect *passphrase*"

>>

$tmp_script

echo

"send \r"

>>

$tmp_script

echo

"expect *again:"

>>

$tmp_script

echo

"send \r"

>>

$tmp_script

echo

"interact"

>>

$tmp_script

chmod +x $tmp_script

/usr/bin/expect $tmp_script

rm$tmp_script

# 3. generat file authorized_keys

cat$ssh_dir/id_rsa.pub>>

$ssh_dir/authorized_keys

# 4. chmod 600 for file authorized_keys

chmod 600 $ssh_dir/authorized_keys

echo

====

====

====

====

====

*****==

# 5. copy all files to other hosts

for ip in

$(cat $hosts_file)

doif[

"x$ip"

!="x"];

then

echo -------------------------

tmp_script=$.

$ip.sh

# check known_hosts

val=

`ssh-keygen -f $ip`

if["x$val"

=="x"];

then

echo

"$ip not in $ssh_dir/known_hosts, need to add"

val=

`ssh-keyscan $ip 2>/dev/null`

if["x$val"

=="x"];

then

echo

"ssh-keyscan $ip failed!"

else

echo

$val

>>

$ssh_dir/known_hosts

fifiecho

"copy $ssh_dir to $ip"

echo

"#!/usr/bin/expect"

>

$tmp_script

echo

"spawn scp -r $ssh_dir

$dest_user@

$ip:~/"

>>

$tmp_script

echo

"expect *assword*"

>>

$tmp_script

echo

"send $password\r"

>>

$tmp_script

echo

"interact"

>>

$tmp_script

chmod +x $tmp_script

#echo "/usr/bin/expect $tmp_script" >$tmp_script.do

#sh $tmp_script.do&

/usr/bin/expect $tmp_script

rm$tmp_script

echo

"copy done."

fidone

echo done.

192.168.1.1

192.168.1.2

192.168.1.3

linux配置互信

1.首先在一台伺服器上生成公鑰和私鑰 ps 如果要設定無密碼登入,那麼在 enter passphrase empty for no passphrase enter same passphrase again 這個地方直接回車就可以設定無密碼登入。2.然後分發公鑰到要配置互信的主機位址上 root...

配置主機互信的shell指令碼

使用注意事項 格式 bash huxin.sh 主機密碼 ip位址 將下面內容儲存為 huxin.sh 檔名可以自取 ip位址最好為乙個檔案裡面儲存上要配置互信的ip yum源裡要有expect bin bash cd passwd 1 yum install y expect usr bin ex...

linux配置ssh互信

建立rsa金鑰和公鑰 登入機器 登入的使用者就是配置的使用者,不要配置錯了 這裡用hadoop使用者 hadoop001 192.168.137.200 hadoop002 192.168.137.201 hadoop003 192.168.137.202 在每台機器上執行 rm rf ssh mk...