sshpass-Linux命令之非交互SSH密码验证
参考网址:https://www.cnblogs.com/chenlaichao/p/7727554.html
ssh登陆不能在命令行中指定密码。sshpass的出现,解决了这一问题。sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码。
它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。
其默认没有安装,需要手动安装,方法如下:
下载网址:https://sourceforge.net/projects/sshpass/files/
[root@db130 ~]# ll /root/sshpass-1.06.tar.gz -rw-r--r-- 1 root root 112205 Mar 25 15:45 /root/sshpass-1.06.tar.gz [root@db130 ~]# tar -zxf sshpass-1.06.tar.gz [root@db130 ~]# cd sshpass-1.06 [root@db130 sshpass-1.06]# ./configure --prefix=/usr/local/sshpass/ [root@db130 sshpass-1.06]# make [root@db130 sshpass-1.06]# make install [root@db130 sshpass-1.06]# cp /usr/local/sshpass/bin/sshpass /usr/bin/ # 出现如下,表明安装成功 [root@db130 sshpass-1.06]# sshpass Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters - password will be taken from stdin -P prompt Which string should sshpass search for to detect a password prompt -v Be verbose about what you're doing -h Show help (this screen) -V Print version information At most one of -f, -d, -p or -e should be used [root@db130 sshpass-1.06]# # 用法简介: -p password # 后接密码 [root@db130 ~]# sshpass -p '12345678' ssh root@192.168.142.128 'ifconfig eth0' eth0 Link encap:Ethernet HWaddr 00:0C:29:B0:62:1B inet addr:192.168.142.128 Bcast:192.168.142.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb0:621b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5915 errors:0 dropped:0 overruns:0 frame:0 TX packets:533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:407552 (398.0 KiB) TX bytes:111865 (109.2 KiB) [root@db130 ~]# -f filename #后跟保存密码的文件名,密码是文件内容的第一行。 [root@db130 ~]# cat 1.txt 12345678 [root@db130 ~]# sshpass -f 1.txt ssh root@192.168.142.128 Last login: Mon Mar 25 22:36:05 2019 from 192.168.142.1 [root@xbj128 ~]# exit logout Connection to 192.168.142.128 closed. -e #将环境变量SSHPASS作为密码,是临时的 [root@db130 ~]# -e #将环境变量SSHPASS作为密码^C [root@db130 ~]# export SSHPASS=12345678 [root@db130 ~]# sshpass -e ssh root@192.168.142.128 "ifconfig eth0" eth0 Link encap:Ethernet HWaddr 00:0C:29:B0:62:1B inet addr:192.168.142.128 Bcast:192.168.142.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb0:621b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6027 errors:0 dropped:0 overruns:0 frame:0 TX packets:588 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:419116 (409.2 KiB) TX bytes:121683 (118.8 KiB) # 传出本地文件到远程 [root@db130 ~]# sshpass -f 1.txt scp /root/sshpass-1.06.tar.gz root@192.168.142.128:/root/ # 拉取远程文件到本地 [root@db130 ~]# sshpass -f 1.txt scp root@192.168.142.128:/root/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz /tmp/