欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简洁的一键SSH脚本

程序员文章站 2022-06-06 16:32:56
...
  这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy,
使用起来也不复杂,在此还是简单贴出来说说。

noscp.exp

  #!/usr/bin/expect

#noscp.exp

if {$argc<4} { 
 puts stderr "Usage: $argv0 localfile  remotefile user passwd " 
 exit 1 
}

set localfile [ lindex $argv 0 ] 
set remotefile  [ lindex $argv 1 ] 
set user  [ lindex $argv 2 ] 
set pwd  [ lindex $argv 3 ]

set timeout 30

spawn scp ${localfile}  ${user}@${remotefile}

expect { 
 "*yes/no" { send "yes\r"; exp_continue } 
 "*password:" { send "$pwd\r" } 
}

expect eof  

ssh_distribute.py

#!/usr/bin/python

import subprocess
import os

file_dir='/home/hosts'

with open(file_dir) as data:
    for each_line in data.readlines():
        if each_line != '':
            (ip,passwd)=each_line.split(':',2)
            print('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('\n'))
            subprocess.Popen('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('\n'),shell=True)
          #  subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \\| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
        else:
            pass
#subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)

ssh_setup.py

#!/usr/bin/python

import subprocess
import os

file_dir='/home/hosts'

with open(file_dir) as data:
    for each_line in data.readlines():
        if each_line != '':
            (ip,passwd)=each_line.split(':',2)
            print('./sshkey.exp '+ip+' root '+passwd.strip('\n')+' | grep ssh-rsa >> ~/.ssh/authorized_keys')
            subprocess.Popen('./sshkey.exp '+ip+' root '+passwd.strip('\n')+' | grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
          #  subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \\| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
        else:
            pass
subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)
#subprocess.Popen('/home/ssh_distribute.py',shell=True) 

sshkey.exp

#!/usr/bin/expect

#sshkey.exp

if {$argc<3} { 
 puts stderr "Usage: $argv0 host  user  passwd " 
 exit 1 
}

set host [ lindex $argv 0 ] 
set user  [ lindex $argv 1 ] 
set pwd  [ lindex $argv 2 ]

set timeout 30



#spawn ssh  ${user}@${host} "rm -rf ~/.ssh/id_rsa*" 
# 
#expect { 
# "*yes/no" { send "yes\r"; exp_continue } 
# "*password:" { send "$pwd\r"; exp_continue  } 
#}


spawn ssh  ${user}@${host} "ssh-****** -t rsa"

expect { 
 "*yes/no" { send "yes\r"; exp_continue } 
 "*password:" { send "$pwd\r"; exp_continue  } 
 "Enter file in which to save the key*" { send "\n\r"; exp_continue } 
 "Overwrite*" { send "y\n"; exp_continue }  
 "Enter passphrase (empty for no passphrase):" { send "\n\r"; exp_continue } 
 "Enter same passphrase again:" { send "\n\r" } 
}

spawn ssh  ${user}@${host} "cat ~/.ssh/id_rsa.pub"

expect { 
 "*yes/no" { send "yes\r"; exp_continue } 
 "*password:" { send "$pwd\r"  } 
}

expect eof
   多看两眼代码应该能够看出,expect的功能是能够等待一些Linux反馈 通过这个的反馈做出推断并能够分类进行兴许的动作,非常黄非常暴力。

也就是利用了这个原理。过程例如以下: 1.首先运行 ./ssh_setup.py 首先收集全部机器的公钥,然后定向到运行这个脚本的authorized_keys文件中边,自己主动赋予755权限。 2.运行./ssh_distribute.py 分发authorized_keys文件到全部的机器上。

下载连接在下方,详细用法里边有readme.txt

 http://download.csdn.net/detail/u012886375/9453810