respberry_pi树莓派

利用Ansible获取树莓派IP地址

2017-02-05  本文已影响418人  forrestsun

网上提到在安装完树莓派后需要通过扫描网段找到系统IP以便于通过SSH登录的方式。如果你的树莓派在同一个路由下通过登录路由也可以查看到相关信息。
软件扫描方式可以参考:

sudo pip install ansible

2.建立文件夹存放相关文件

mkdir ~/rpi-cluster-demo
nano ~/rpi-cluster-demo/hosts
[cluster]
pi1 ansible_ssh_host=pi1.local ansible_ssh_port=22
pi2 ansible_ssh_host=pi2.local ansible_ssh_port=22
pi3 ansible_ssh_host=pi3.local ansible_ssh_port=22
nano ~/rpi-cluster-demo/ansible.cfg
[defaults]
hostfile=hosts
remote_user=sunsl
host_key_checking=Fasle

3.输出IP信息

$ ansible clusters -a "ip addr show dev eth0"
pi2 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:de:a0:c1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.123.32/24 brd 192.168.123.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:fede:a0c1/64 scope link 
       valid_lft forever preferred_lft forever、
pi1 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:5f:5d:43 brd ff:ff:ff:ff:ff:ff
    inet 192.168.123.85/24 brd 192.168.123.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:fe5f:5d43/64 scope link 
       valid_lft forever preferred_lft forever
pi3 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:b1:48:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.123.242/24 brd 192.168.123.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:feb1:4802/64 scope link 
       valid_lft forever preferred_lft forever
pi4 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:c8:bf:d0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.123.147/24 brd 192.168.123.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:fec8:bfd0/64 scope link 
       valid_lft forever preferred_lft forever

主机数量少或者仅单纯是进行SSH登录的话可以直接通过ssh root@pi1.local实现,这主要归功于Hypriot团队在镜像中已经启动了avahi-daemon服务。,通过avahi-daemon服务实现了Zero configuration networking(zeroconf)零配置网络服务规范.这样我们只要在同一网段中不需要知道主机IP也可以进行通信。

安装过程可以阅读:树莓派3 HypriotOS安装

在网上发现Malcolm Jones的一篇博文中也有相关思路:

$ function getip() { (traceroute $1 2>&1 | head -n 1 | cut -d( -f 2 | cut -d) -f 1) }
$ export MASTER=$(getip scarlett-kub-master.local)
$ echo $MASTER
$ export SLAVE_1=$(getip scarlett-kub-slave.local)
$ echo $SLAVE_1
$ export SLAVE_1=$(getip scarlett-kub-slave1.local)
$ echo $SLAVE_1
$ export SLAVE_2=$(getip scarlett-kub-slave2.local)
$ echo $SLAVE_2
$ export SLAVE_3=$(getip scarlett-kub-slave3.local)
$ echo $SLAVE_3

参考:

上一篇下一篇

猜你喜欢

热点阅读