sshpass 使用方法

首先下载sshpass并且安装

$ tar -zxvf sshpass-1.06.tar.gz

$ cd sshpass-1.06

$ ./configure --prefix=/usr/local/sshpass #指定安装目录

$ make

$ make install

 

[root@qinhan ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.43.105  netmask 255.255.255.0  broadcast 172.16.43.255
        inet6 fe80::20c:29ff:fe22:1f4f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:4f  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 120 (120.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 768 (768.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.100  netmask 255.255.255.0  broadcast 192.168.27.255
        inet6 fe80::20c:29ff:fe22:1f59  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:59  txqueuelen 1000  (Ethernet)
        RX packets 12683  bytes 8934971 (8.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7687  bytes 985668 (962.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1301  bytes 132447 (129.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 132447 (129.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@qinhan ~]# sshpass -p root ssh root@192.168.27.3
Last login: Fri Jan 19 10:24:38 2018 from node2.magedu.com
[root@node1 ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.43.102  netmask 255.255.255.0  broadcast 172.16.43.255
        inet6 fe80::20c:29ff:fe53:14fc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:14:fc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 768 (768.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33554960: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.3  netmask 255.255.255.0  broadcast 192.168.27.255
        inet6 fe80::20c:29ff:fe53:1406  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:14:06  txqueuelen 1000  (Ethernet)
        RX packets 1604  bytes 166368 (162.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 610  bytes 137655 (134.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 3588  bytes 310356 (303.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3588  bytes 310356 (303.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@node1 ~]# 

问题来了如果 删除~/.ssh/known_hosts,我们在使用 sshpass -p root ssh root@192.168.27.3 可以发现没有登陆到192.168.27.3服务器上,

sshpass -p root ssh root@192.168.27.3 -o "StrictHostKeyChecking no"  才能登陆到192.168.27.3服务器上

[root@qinhan ~]# cat ~/.ssh/known_hosts 
node2,192.168.27.101 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEkmmdQBm1c0ZHKFPQqhMQvKe+g6sSD5HwYGsZapCJRUmWoyQmvI0iIax1OEMwTy+XzijD7OcQ6t8cXCRKxXva8=
192.168.27.3 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBImdbfgGnbnBPlUNTFs3UQXCVX+xq7LdjrSwB82587hmjWCA0IhYmGFSehvplOdcrlKU8bvEfIxFcZEnCvSUaLM=
node3 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBImdbfgGnbnBPlUNTFs3UQXCVX+xq7LdjrSwB82587hmjWCA0IhYmGFSehvplOdcrlKU8bvEfIxFcZEnCvSUaLM=
node1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBImdbfgGnbnBPlUNTFs3UQXCVX+xq7LdjrSwB82587hmjWCA0IhYmGFSehvplOdcrlKU8bvEfIxFcZEnCvSUaLM=
[root@qinhan ~]# rm ~/.ssh/known_hosts 
rm: remove regular file ‘/root/.ssh/known_hosts’? y
[root@qinhan ~]# 
[root@qinhan ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.43.105  netmask 255.255.255.0  broadcast 172.16.43.255
        inet6 fe80::20c:29ff:fe22:1f4f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:4f  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 120 (120.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 768 (768.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.100  netmask 255.255.255.0  broadcast 192.168.27.255
        inet6 fe80::20c:29ff:fe22:1f59  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:59  txqueuelen 1000  (Ethernet)
        RX packets 13038  bytes 8972776 (8.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8020  bytes 1029711 (1005.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1301  bytes 132447 (129.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 132447 (129.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@qinhan ~]# sshpass -p root ssh root@192.168.27.3
[root@qinhan ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.43.105  netmask 255.255.255.0  broadcast 172.16.43.255
        inet6 fe80::20c:29ff:fe22:1f4f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:4f  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 120 (120.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 768 (768.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.100  netmask 255.255.255.0  broadcast 192.168.27.255
        inet6 fe80::20c:29ff:fe22:1f59  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:1f:59  txqueuelen 1000  (Ethernet)
        RX packets 13074  bytes 8977585 (8.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8047  bytes 1038634 (1014.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1301  bytes 132447 (129.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 132447 (129.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@qinhan ~]# 

[root@qinhan ~]# sshpass -p root ssh root@192.168.27.3 -o "StrictHostKeyChecking no"
Warning: Permanently added '192.168.27.3' (ECDSA) to the list of known hosts.
Last login: Fri Jan 19 10:27:32 2018 from node2.magedu.com
[root@node1 ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.43.102 netmask 255.255.255.0 broadcast 172.16.43.255
inet6 fe80::20c:29ff:fe53:14fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:53:14:fc txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 768 (768.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eno33554960: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.27.3 netmask 255.255.255.0 broadcast 192.168.27.255
inet6 fe80::20c:29ff:fe53:1406 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:53:14:06 txqueuelen 1000 (Ethernet)
RX packets 1788 bytes 186238 (181.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 721 bytes 162387 (158.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 3588 bytes 310356 (303.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3588 bytes 310356 (303.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

原文地址:https://www.cnblogs.com/qinhan/p/8315678.html