批量ssh免密shell脚本
1.创建同目录级文本文件ip.txt
IP:密码 冒号分割
IP:password
2.创建脚本文件password_free_login_ssh.sh
#!/bin/bash
expect -c "set timeout -1;
spawn ssh-keygen;
expect {
*(/root/.ssh/id_rsa)* {send -- \r;exp_continue;}
*passphrase)* {send -- \r;exp_continue;}
*again* {send -- \r;exp_continue;}
*(y/n)* {send -- y\r;exp_continue;}
*password:* {send -- $PASSWORD\r;exp_continue;}
eof {exit 0;}
}"
for i in `cat ip.txt`
do
ip=$(echo "$i"|cut -f1 -d":")
password=$(echo "$i"|cut -f2 -d":")
cd ~/.ssh/
expect -c " set timeout -1;
spawn ssh-copy-id -i id_rsa.pub root@$ip;
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
} "
done
pexpect
Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。 Pexpect 的使用范围很广,可以用来实现与 ssh、ftp 、telnet 等程序的自动交互;可以用来自动复制软件安装包并在不同机器自动安装;还可以用来实现软件测试中与命令行交互的自动化。
import pexpect
import sys
# 通过spawn类启动和控制子应用程序
child = pexpect.spawn('ssh root@192.168.1.22')
# 将pexpect的输入输出信息写到mylog.txt文件中
child.logfile = sys.stdout
# 将pexpect的输入输出信息输出到标准输出
#fout = file('mylog.txt', 'w')
#child.logfile = fout
# expect方法用来判断子程序产生的输出,判断是否匹配相应字符串
child.expect('password:')
# 字符串匹配则使用sendline进行回应-----send:发送命令,不回车、sendline:发送命令,回车、sendcontrol:发送控制符,如:sendctrol('c')等价于‘ctrl+c'、sendeof:发送eof
child.sendline('std20')
child.expect('std20.*')
child.sendline('ls /')
child.expect('std20.*') # .*正则
child.sendline('exit')
#coding=utf_8
# 另一种实现思路,比上一方法更为简单
from pexpect import pxssh
import sys
s = pxssh.pxssh()
s.logfile = sys.stdout # 将操作信息打印到标准输出,如果不加不显示内容
hostname = '123.57.211.212'
username = 'std20'
password = 'std20'
s.login(hostname, username, password)
s.sendline('ls /')
s.prompt() # 匹配系统提示符
s.sendline('whoami')
s.prompt()
s.logout()
fabric
项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上。
修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A,B,C,D,E(1分钟),分别ssh到ABCDE五台机器,逐一重启(8-10分钟) = 13-15分钟
其中郁闷的是,每次操作都是相同的,命令一样,要命的是在多个机器上,很难在本机一个脚本搞定,主要时间都浪费在ssh,敲命令上了,写成脚本,完全可以一键执行,花两分钟看下执行结果。
安装
此教程为python2.7使用,如果使用python3具体可参考此帮助文档 fabric-chs.readthedocs.io/zh_CN/chs/t…
pip install fabric
入门示例
#fabfile.py
from fabric.api import run
def host_type():
run('uname -s')
启动
Wxc@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type
[127.0.0.1] Executing task 'host_type'
[127.0.0.1] run: uname -s
[127.0.0.1] Login password for 'itcast':
[127.0.0.1] out: Linux
[127.0.0.1] out:
Done.
Disconnecting from 127.0.0.1... done.
Wxc@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type
[127.0.0.1] Executing task 'host_type'
[127.0.0.1] run: uname -s
[127.0.0.1] Login password for 'itcast':
[127.0.0.1] out: Linux
[127.0.0.1] out:
fabric常用参数
- -l : 显示定义好的任务函数名
- -f : 指定fab入口文件,默认入口文件名为fabfile.py
- -H : 指定目标主机,多台主机用","号分割,可以使本地机器或者目标远程机器
fabric常用API
- local : 执行本地命令,如:local('uname -s')
- lcd : 切换本地目录,如:lcd('/home')
- cd : 切换远程目录,如:cd('/etc')
- run : 执行远程命令,如:run('free -m')
- sudo : sudo方式执行远程命令,如:sudo('touch /abc')
- put : 上传本地文件到远程主机,如:put('/hello', '/home/root/hello')
- get : 从远程主机下载文件到本地,如:get('/home/python/world', '/home/root/world')
- reboot : 重启远程主机,如:reboot()
- @task : 函数装饰器,标识的函数为fab可调用的,非标记的对fab不可见,纯业务逻辑
- @runs_once : 函数装饰器,标识的函数只会执行一次,不受多台主机影响
fabric全局属性设定
- env.host : 定义目标主机,如:env.host=['192.168.17.192', '192.168.17.193']
- env.user : 定义用户名,如:env.user="root"
- env.port : 定义目标主机端口,默认为22,如:env.port="22"
- env.password : 定义密码,如:env.password="chuanzhi"
- env.passwords : 不同的主机不同的密码,如:env.passwords={'root@192.168.17.192:22':'chuanzhi', 'root@192.168.17.193:22':'python'}
示例1:动态获取远程目录列表
from fabric.api import *
env.hosts=['192.168.17.192', '192.168.17.193']
#env.password='python'
env.passwords = {
'itroot@192.168.17.192:22':'python',
'itroot@192.168.17.193:22':'python',
}
@runs_once
def input_raw():
return prompt("please input directory name:", default="/home")
def workask(dirname):
run('ls -l ' + dirname)
@task
def go():
print('start ...')
getdirname = input_raw()
workask(getdirname)
print('end ...')
示例2:上传文件并执行
from fabric.api import *
env.user = 'root'
env.hosts = ['192.168.17.192', '192.168.17.193']
env.password = 'python'
@task
@runs_once
def tar_task():
with lcd('/home/root/testdemo'):
local('tar zcvf demo.tar.gz demo.py')
@task
def put_task():
run('mkdir -p /home/root/testdemo')
with cd('/home/root/testdemo'):
put('/home/root/testdemo/demo.tar.gz', '/home/root/testdemo/demo.tar.gz')
@task
def check_task():
lmd5 = local('md5sum /home/root/testdemo/demo.tar.gz', capture=True).split(' ')[0]
rmd5 = run('md5sum /home/root/testdemo/demo.tar.gz').split(' ')[0]
if lmd5 == rmd5:
print('OK ...')
else:
print('ERROR ...')
@task
def run_task():
with cd('/home/root/testdemo'):
run('tar zxvf demo.tar.gz')
run('python demo.py')
@task
def go():
tar_task()
put_task()
check_task()
run_task()
代码自动化部署
from fabric.api import *
env.user = 'root'
env.hosts = ['192.168.17.192', '192.168.17.193']
env.password = 'python'
@runs_once
@task
def local_update():
with lcd("/home/root/tmp/hello"):
local("git add -A")
local("git commit -m 'update'")
local("git pull origin master")
local("git push origin master")
@task
def remote_update():
with cd("/home/root/tmp/roothello"):
run("git checkout master")
run("git pull origin master")
@task
def deploy():
local_update()
remote_update()
文章规划
-
shell学习之自动化部署(九)
好了各位,以上就是这篇文章的全部内容了,能看到这里人啊,都是人才。
如果这个文章写得还不错,觉得「王采臣」我有点东西的话 求点赞👍求关注❤️求分享👥 对耿男我来说真的非常有用!!!
白嫖不好,创作不易,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!
王采臣 | 文 【原创】
如果本篇博客有任何错误,请批评指教,不胜感激 !
微信公众号: