2019-05-27

telnet 服务及抓包 查看

yum install -y  telnet-server
systemctl restart telnet.socket 
telnet  root@10.0.0.61  23

ssh客户端命令

ssh  -p22  oldboy@10.0.0.41  whoami
123456

ssh服务端配置文件详解

[root@m01 ~]# egrep -i  '^port|^permitroot' /etc/ssh/sshd_config
Port 52113
PermitRootLogin no
[root@m01 ~]#  systemctl reload  sshd 
[root@m01 ~]# ss -lntup |grep ssh
tcp    LISTEN     0      128       *:52113                 *:*                   users:(("sshd",pid=7129,fd=3))
tcp    LISTEN     0      128      :::52113                :::*                   users:(("sshd",pid=7129,fd=4))


[root@m01 ~]# grep -in ^listenaddress /etc/ssh/sshd_config
20:ListenAddress 172.16.1.61

案例:多个网段多端口

Port 52213
内网 外网
ssh -p 52113 10.0.0.61
scp -P 52113 10.0.0.61
sftp -P 52113 10.0.0.61

ssh -p 22 10.0.0.61
scp -P 22 10.0.0.61
sftp -P 22 10.0.0.61

[root@m01 ~]# grep -in ^listenaddress /etc/ssh/sshd_config
20:ListenAddress 10.0.0.61:52113
21:ListenAddress 172.16.1.61:22
[root@m01 ~]# systemctl reload sshd
[root@m01 ~]# ss -lntup |grep sshd
tcp LISTEN 0 128 10.0.0.61:52113 : users:(("sshd",pid=7129,fd=4))
tcp LISTEN 0 128 172.16.1.61:22 : users:(("sshd",pid=7129,fd=3))

[root@m01 ~]# ssh -p52113 10.0.0.61 hostname
root@10.0.0.61's password:
m01
[root@m01 ~]# ssh -p22 172.16.1.61 hostname
root@172.16.1.61's password:
m01

yum install -y sshpass pssh

error

创建秘钥认证:

创建秘钥对

[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:VW1UamyTZ0YDIHQxi00U7DrtJAX/BN0k5cbuhNRRA58 root@m01
The key's randomart image is:
+---[DSA 1024]----+
| .ooO*BB=|
| .
+ooO==|
| .=o.oBE+|
| . +.++= |
| S + o. o |
| + o .o |
| = . |
| . |
| |
+----[SHA256]-----+

检查 秘钥

[root@m01 ~]# ll ~/.ssh/
total 12
-rw------- 1 root root 668 May 27 12:13 id_dsa
-rw-r--r-- 1 root root 598 May 27 12:13 id_dsa.pub
-rw-r--r-- 1 root root 695 May 27 11:22 known_hosts

发送公钥

[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_dsa.pub 172.16.1.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.41's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.

进行测试

[root@m01 ~]# ssh 172.16.1.41 hostname
backup

批量并行执行命令

[root@m01 ~]# cat hosts.txt
root@172.16.1.41:22
root@172.16.1.7:22
[root@m01 ~]# pssh -Ph hosts.txt hostname
172.16.1.41: backup
[1] 12:42:51 [SUCCESS] root@172.16.1.41:22
172.16.1.7: web01
[2] 12:42:51 [SUCCESS] root@172.16.1.7:22

[root@m01 ~]# prsync -A -azh hosts.txt /etc/hostname /tmp/
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 12:52:11 [SUCCESS] root@172.16.1.41:22
[2] 12:52:11 [SUCCESS] root@172.16.1.7:22
[root@m01 ~]#
[root@m01 ~]#
[root@m01 ~]# pssh -A -Ph hosts.txt cat /tmp/hostname
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
172.16.1.41: m01
[1] 12:52:32 [SUCCESS] root@172.16.1.41:22
172.16.1.7: m01
[2] 12:52:32 [SUCCESS] root@172.16.1.7:22

ssh "$@" "

      exec sh -c '
      
         cd ; 
         umask 077 ; 
         mkdir -p .ssh && 
         cat >> .ssh/authorized_keys || exit 1 ; if type restore
         con >/dev/null 2>&1 ; 
         then restorecon -F .ssh .ssh/authorized_keys ; 
         fi'"

免密连接

sshpass -p123456 ssh 172.16.1.7 hostname

ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''

-t 指定秘钥类型 das rsa

-f 指定私钥位置

-P 指定密码短语

sshpass -p 123456 ssh -oStrictHostKeyChecking=no 172.16.1.7 hostname
web01

sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.7

for循环

格式:
for 变量 in 列表(清单)
do
命令
done

for ip in 7 41 {1..6}
do
echo 172.16.1.$ip
done

批量分发秘钥到 172.16.1.7 和172.16.1.41 写出for循环

创建秘钥 :
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''

[root@m01 ~]# vim /server/scripts/fenfa.sh
#!/bin/bash

make key pair

ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''

fenfa public key

for ip in 7 41 31
do
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.$ip
done

for ip in 7 41 31 ;
do
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.ip sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.ip
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.ip sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.ip
done

/etc/ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles

[root@m01 ~]# tail -3 /etc/ansible/hosts
[oldboy]
172.16.1.7
172.16.1.41
[root@m01 ~]# ansible oldboy  -m ping 
172.16.1.41 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@m01 ~]# ansible oldboy  -m command   -a 'hostname '
172.16.1.41 | CHANGED | rc=0 >>
backup

172.16.1.7 | CHANGED | rc=0 >>
web01

[root@m01 ~]# ansible oldboy     -a 'hostname '
172.16.1.41 | CHANGED | rc=0 >>
backup

172.16.1.7 | CHANGED | rc=0 >>
web01

[root@m01 ~]# ansible 172.16.1.7     -a 'hostname '
172.16.1.7 | CHANGED | rc=0 >>
web01
[root@m01 ~]# ansible all     -a 'hostname '
172.16.1.7 | CHANGED | rc=0 >>
web01

172.16.1.41 | CHANGED | rc=0 >>
backup

[root@m01 ~]# #ansible all   -m copy -a 'src=/etc/hostname   dest=/tmp/'
[root@m01 ~]# 
[root@m01 ~]# 
[root@m01 ~]# ansible all   -a 'cat /tmp/hostname '
172.16.1.7 | CHANGED | rc=0 >>
m01

172.16.1.41 | CHANGED | rc=0 >>
m01

 ansible-doc -s +命令   查看帮助该命令的帮助信息

scipt 模块

[root@m01 ~]# #ansible all  -m script  -a "/server/scripts/yum.sh"
[root@m01 ~]# ansible all -a 'rpm -qa ipvsadm'
 [WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because
yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.

172.16.1.41 | CHANGED | rc=0 >>
ipvsadm-1.27-7.el7.x86_64

172.16.1.7 | CHANGED | rc=0 >>
ipvsadm-1.27-7.el7.x86_64

yum模块

ansible all -m yum -a 'name=sl state=present'

file模块

[root@m01 ~]# #ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g   state=directory '
[root@m01 ~]# #ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g/oldboy.txt   state=touch '
[root@m01 ~]# ansible all  -a 'tree  /tmp/ '

for n in {1..10} ;
do
echo n ; done i的初始化 ;i<=10 条件;i++ 从1开始每次增加1 大于10的时候结束循环 for((i=1;i<=10;i++)) do echoi
done

每5分钟同步系统时间

*/5 * * * * ntpdate ntp1.aliyun.com >>/tmp/ntpdate.log 2>&1

          • ntpdate ntp1.aliyun.com >>/tmp/ntpdate.log 2>&1

sync time

*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

定时任务中 识别PATH环境变量默认 /usr/bin和/bin

定时任务模块 cron

sync time
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1.

ansible all -m yum -a 'name=ntpdate state=present'
ansible all -m cron -a 'name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'

ansible all -m cron -a 'name="zwav time" state=absent '
ansible 172.16.1.7 -m mount -a "src=172.16.1.31:/data path=/backup fstype=nfs opts=defaults state=present"

nfs01

/nfs 172.16.1.0/24(rw,all_squash,anonuid=888,anongid=888)

[root@nfs01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs 172.16.1.0/24
/upload 172.16.1.0/24

web01 把nfs01 共享的/nfs 挂载到 /upload

/upload

[root@m01 ~]# #mount -t nfs  172.16.1.31:/nfs    /upload 
[root@m01 ~]# ansible 172.16.1.7 -m  mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
172.16.1.7 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/upload", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "172.16.1.31:/nfs"
}
[root@m01 ~]# ansible 172.16.1.7 -a 'df -h'
172.16.1.7 | CHANGED | rc=0 >>
Filesystem        Size  Used Avail Use% Mounted on

172.16.1.31:/nfs   99G  1.7G   98G   2% /upload

[root@m01 ~]# ansible 172.16.1.7 -a 'grep upload /etc/fstab'
172.16.1.7 | CHANGED | rc=0 >>
172.16.1.31:/nfs /upload nfs defaults 0 0

playbook

[root@m01 /etc/ansible]# cat touch.yml 
---
  - hosts: all
    tasks:
    - name: make av 
      command: mkdir -p /tmp/a/b/c/d/e/f/g/
      
ansible-playbook -C touch.yml
ansible-playbook  touch.yml

[root@m01 /etc/ansible]# ansible all -a 'crontab -l'
172.16.1.7 | CHANGED | rc=0 >>

backup

00 00 * * * sh /server/scripts/bak.sh >/dev/null 2>&1

Ansible: sync time

*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/d▽v/null 2>&1

172.16.1.41 | CHANGED | rc=0 >>

Ansible: sync time

*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/d▽v/null 2>&1

[root@m01 /etc/ansible]# cat cron-time.yml

  • hosts: all
    tasks:
    • name: sync time
      cron: name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/d▽v/null 2>&1" state=present

ansible实现 部署nfs服务 及在web01 web02 挂载 playbook

#!/bin/bash

#one key install nfs

#package install?

yum install -y rpcbind nfs-utils

#configure

cp /etc/exports{,.bak}
cat >/etc/exports<<EOF

#nfs01

/nfs 172.16.1.0/24(rw,all_squash)
EOF

#dir owner

mkdir -p /nfs
chown nfsnobody.nfsnobody /nfs

#start rpcbind nfs

systemctl start rpcbind nfs
systemctl enable rpcbind nfs

部署nfs服务

web01 web02 挂载

[web]
172.16.1.7
172.16.1.8
172.16.1.9

ansible中变量的使用

[root@m01 /etc/ansible]# cat 01-var.yml 
---
  - hosts: all
    vars:
      ip: 10.0.0.200
    tasks:
    - name: mkdir 
      file: 
        path: /oldboy/{{ ip }}
        state: directory

在nfs01 backup 创建/backup/lidao的目录 把 /etc/目录打包压缩到/backup/lidao目录下面 etc.tar.gz
目录名存放在变量中

dir=/backup/lidao
mkdir -p /backup/lidao
tar zcf /backup/lidao/xxx.tar.gz /etc

---
  - hosts: all
    vars:
      dir: /backup/lidao
    tasks:
      - name: mkdir
        file:
          path: "{{dir}}"
          state: directory
      - name: tar
        archive:
          path: /etc
          dest: "{{dir}}/etc.tar.gz "
        

ip=`hostname -I|awk '{print $NF}'`      
[root@m01 /etc/ansible]# cat 03-var-reg.yml 
---
  - hosts: all
    tasks:
      - name: ip
        shell:  hostname -I|awk '{print $NF}'
        register: ipaddr
      - name: print ip
        shell: echo {{ipaddr}}>/tmp/ip.txt

[root@m01 /etc/ansible]#
ipaddr变量的内容
[root@nfs01 ~]# cat /tmp/ip.txt
{stderr_lines: []
uchanged: True
uend: u2019-05-31 11:24:45.080356
failed: False
ustdout: u172.16.1.31,
ucmd: uhostname -I|awk '{print }',
urc: 0,
ustart: u2019-05-31 11:24:45.073817,
ustderr: u,
udelta: u0:00:00.006539,
stdout_lines: [u172.16.1.31]
}

stderr standard error
stdout standard output 标准输出

ansible 调试功能
debug 调试

[root@m01 /etc/ansible]# cat 03-var-reg.yml
---
  - hosts: all
    tasks:
      - name: ip
        shell:  hostname -I|awk '{print $NF}'
        register: ipaddr
      - name:  debug ipaddr
        debug: msg={{ipaddr.stdout}}    
[root@m01 /etc/ansible]# cat   04-var-reg-tar.yml
---
  - hosts: all
    tasks:
      - name: ip
        shell: hostname -I|awk '{print $NF}'
        register: ipaddr
      - name: date
        shell: date +%F
        register: time
 
      - name: mkdir dir
        file:
          path: /backup/{{ipaddr.stdout}}
          state: directory
      - name: tar 
        archive: 
          path: /etc
          dest: /backup/{{ipaddr.stdout}}/etc-{{time.stdout}}.tar.gz   
[root@manager ~]# cat f7.yml
---
- hosts: all
  remote_user: root
  tasks:
    - name: Installed Pkg
      yum: name={{ item }} state=present
      with_items:
        - wget
        - tree
        - lrzsz
              
for item  in  wget tree lrzsz 
do 
     yum install -y  $item 
done          
          
          
---
- hosts: all
  remote_user: root
  tasks:
    - name: Installed Pkg
      yum: name={{ item }} state=present
      with_items:
        - wget
        - tree
        - lrzsz
[root@manager ~]# cat f7.yml
- hosts: all
  remote_user: root
  tasks:
    - name: Add Users
      user: name={{ item.name }} groups={{ item.groups }} state=present
      with_items:
        - { name: 'testuser1', groups: 'bin' }
        - { name: 'testuser2', groups: 'root' }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容

  • Ansible配置管理系统 Ansible是一个配置管理系统configuration management sy...
    山有木兮_8adb阅读 271评论 0 0
  • 1,ansible介绍 ssh密钥认证+脚本批量管理,特点:简单,实用 但是看起来比较low,需要人工写脚本,类似...
    自律的生活无虑阅读 281评论 0 0
  • 舍友和我说过时间不会让我忘了他,只会让我更加喜欢他,谈忘记很难,真的很难,毕竟喜欢了5年。 有人说不接受别人的喜欢...
    薄荷女孩阅读 334评论 7 3
  • 或许,我的柔肠,一直在村外那条河塘。或许,我的梦想,只想和你一起沐浴月光。那小船无人,自横在野渡上,清风轻唱,白云...
    浪里黑条条阅读 223评论 0 0
  • 又是一年毕业季,又会有一群人失恋。 我是小树,记得那年,是她提的分手,我很难过,不知所措,拿着手机想给她打电话,祈...
    心情树洞屋阅读 230评论 0 0