Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatically generate the public key and automatically write it to a specific server script

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Automatically generate the public key and automatically write it to a specific server script

The sudo permission required for a general account

Zhi.yang ALL= (ALL:ALL) NOPASSWD:/usr/bin/tee-a / root/.ssh/authorized_keys,/bin/mkdir / log/*

# expect/usr/local/bin/sshkey.exp 10.0.0.3 shaa01 zhi.yang xxxxx

#! / usr/bin/expect

Set ip [lindex $argv 0]

Set hostname [lindex $argv 1]

Set user [lindex $argv 2]

Set password [lindex $argv 3]

Spawn ssh-keygen

Expect {

"* id_rsa):" {send "\ r"; exp_continue}

"* passphrase):" {send "\ r"; exp_continue}

"* again:" {send "\ r"}

}

Expect eof

Spawn scp-r / root/.ssh/id_rsa.pub $user@$ip:/tmp/

Expect {

"* (yes/no) *" {send "yes\ r"; exp_continue}

"* Password:" {send "$password\ r"}

}

Expect eof

Spawn ssh $user@$ip

Expect {

"* Password:" {send "$password\ r"}

}

Expect eof

Expect "$*"

Send "sudo mkdir / log/$hostname\ r"

Send "cat / tmp/id_rsa.pub | sudo tee-a / root/.ssh/authorized_keys\ r"

Send "rm-rf / tmp/id_rsa.pub\ r"

Send "exit\ r"

Expect eof

Exec sh-c {/ bin/sh / usr/local/bin/diskG9.sh}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report