分享

三种方法实现scp拷贝时无需输入密码

 cupid 2023-03-22 发布于广东

作为一个运维人员,我经常要将一些文件传输到另外一个服务器上,而且都是Linux的命令行环境,那么对于我来讲scp就是最直接有效的方法了,但是,使用scp需要指定远端服务器的账号并手动输入密码,那么如何避免每次都需要输入密码这个操作呢?话不多说,以下就是解决办法。

方法一:配置信任关系

配置信任关系,要建立2个文件,ssh公钥(~/.ssh/id_rsa.pub)和公钥授权文件(~/.ssh/authorized_keys),~/.ssh/id_rsa.pub文件中包含了认证的公钥信息,而且该文件可以被任何人读取;而~/.ssh/authorized_keys文件中则列举了登录用户的公钥信息

以下是步骤

[root@Node2 ~]# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:e7:d4:ae:f7:90:7d:6a:41:57:79:bc:45:0e:a9:5a:3e root@Node2The key's randomart image is:+--[ RSA 2048]----+|              .+o||              .+=||             .  *||           .o ...||        S o+.. . ||         +..Eo.  ||          . +....||           ....o ||          .. oo  |+-----------------+[root@Node2 ~]# cd .ssh[root@Node2 .ssh]# ls -l总用量 12-rw-------. 1 root root 1675 2月  27 13:13 id_rsa-rw-r--r--. 1 root root  392 2月  27 13:13 id_rsa.pub-rw-r--r--. 1 root root  400 2月  27 12:51 known_hosts[root@Node2 .ssh]# cat id_rsa.pub  >> authorized_keys把这两个文件上传到另外一个服务器上,然后验证[root@Node2 .ssh]# scp id_rsa.pub authorized_keys root@zxy:/root/.ssh/id_rsa.pub                                                                        100%  392     0.4KB/s   00:00    authorized_keys                                                                   100%  392     0.4KB/s   00:00    [root@Node2 .ssh]# ssh zxyLast login: Thu May 17 10:10:02 2018 from 192.168.10.1[root@zxy ~]# exit

方法二:使用sshpass工具

一般系统上没有sshpass工具,这个需要额外安装,可以在这边下载http:///projects/sshpass/,下载完之后,上传文件,以下是安装步骤

[root@Node2 ~]# tar -xvf sshpass-1.06.tar.gzsshpass-1.06/sshpass-1.06/main.csshpass-1.06/install-shsshpass-1.06/AUTHORSsshpass-1.06/config.h.insshpass-1.06/depcompsshpass-1.06/aclocal.m4sshpass-1.06/configuresshpass-1.06/NEWSsshpass-1.06/compilesshpass-1.06/ChangeLogsshpass-1.06/INSTALLsshpass-1.06/Makefile.amsshpass-1.06/sshpass.1sshpass-1.06/COPYINGsshpass-1.06/READMEsshpass-1.06/Makefile.insshpass-1.06/configure.acsshpass-1.06/missing[root@Node2 ~]# cd sshpass-1.06[root@Node2 sshpass-1.06]# ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking whether make supports nested variables... yeschecking for style of include used by make... GNUchecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables...checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking whether gcc understands -c and -o together... yeschecking dependency style of gcc... gcc3checking how to run the C preprocessor... gcc -Echecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking minix/config.h usability... nochecking minix/config.h presence... nochecking for minix/config.h... nochecking whether it is safe to define __EXTENSIONS__... yeschecking for gcc... (cached) gccchecking whether we are using the GNU C compiler... (cached) yeschecking whether gcc accepts -g... (cached) yeschecking for gcc option to accept ISO C89... (cached) none neededchecking whether gcc understands -c and -o together... (cached) yeschecking dependency style of gcc... (cached) gcc3checking for ANSI C header files... (cached) yeschecking for sys/wait.h that is POSIX.1 compatible... yeschecking fcntl.h usability... yeschecking fcntl.h presence... yeschecking for fcntl.h... yeschecking for stdlib.h... (cached) yeschecking for string.h... (cached) yeschecking sys/ioctl.h usability... yeschecking sys/ioctl.h presence... yeschecking for sys/ioctl.h... yeschecking for unistd.h... (cached) yeschecking termios.h usability... yeschecking termios.h presence... yeschecking for termios.h... yeschecking for an ANSI C-conforming const... yeschecking for pid_t... yeschecking for ssize_t... yeschecking vfork.h usability... nochecking vfork.h presence... nochecking for vfork.h... nochecking for fork... yeschecking for vfork... yeschecking for working fork... yeschecking for working vfork... (cached) yeschecking whether gcc needs -traditional... nochecking for stdlib.h... (cached) yeschecking for GNU libc compatible malloc... yeschecking sys/select.h usability... yeschecking sys/select.h presence... yeschecking for sys/select.h... yeschecking sys/socket.h usability... yeschecking sys/socket.h presence... yeschecking for sys/socket.h... yeschecking types of arguments for select... int,fd_set *,struct timeval *checking return type of signal handlers... voidchecking for select... yeschecking for posix_openpt... yeschecking for strdup... yeschecking that generated files are newer than configure... doneconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating config.hconfig.status: executing depfiles commands[root@Node2 sshpass-1.06]# make && make installmake  all-ammake[1]: Entering directory `/root/sshpass-1.06'gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cmv -f .deps/main.Tpo .deps/main.Pogcc  -g -O2   -o sshpass main.omake[1]: Leaving directory `/root/sshpass-1.06'make[1]: Entering directory `/root/sshpass-1.06'/bin/mkdir -p '/usr/local/bin'/usr/bin/install -c sshpass '/usr/local/bin'/bin/mkdir -p '/usr/local/share/man/man1'/usr/bin/install -c -m 644 sshpass.1 '/usr/local/share/man/man1'make[1]: Leaving directory `/root/sshpass-1.06'[root@Node2 sshpass-1.06]# cd
验证:[root@Node2 ~]# sshpass -p root ssh zxyLast login: Thu May 17 10:47:30 2018 from node2[root@zxy ~]#
但是这种方法,直接暴露了密码,不安全~

3、使用工具expect工具

首先下载http:///projects/expect/files/Expect/5.45/ ,上传到服务器

安装expect的时候需要安装tcl-devel,要不然会报错

[root@Node2 ~]# yum install tcl-devel[root@Node2 ~]# tar -xvf expect5.45.3.tar.gz[root@Node2 ~]# cd expect5.45.3[root@Node2 ~]# ./configure[root@Node2 ~]# make && make install
下面就是一个使用expect来完成scp时无需输入密码的脚本:[plain] view plain copy#!/usr/bin/expectset timeout 10set host [lindex $argv 0]set username [lindex $argv 1]set password [lindex $argv 2]set src_file [lindex $argv 3]set dest_file [lindex $argv 4]spawn scp $src_file $username@$host:$dest_fileexpect {"(yes/no)?"{send "yes\n"expect "*assword:" { send "$password\n"}}"*assword:"{send "$password\n"}}expect "100%"expect eof
注意代码刚开始的第一行,指定了expect的路径,与shell脚本相同,这一句指定了程序在执行时到哪里去寻找相应的启动程序。代码刚开始还设定了timeout的时间为10秒,如果在执行scp任务时遇到了代码中没有指定的异常,则在等待10秒后该脚本的执行会自动终止。
从以上代码刚开始的几行可以看出,我为这个脚本设置了5个需要手动输入的参数,分别为:目标主机的IP、用户名、密码、本地文件路径、目标主机中的文件路径。如果将以上脚本保存为cs文件,则在shell下执行时需要按以下的规范来输入命令:
[root@Node2 ~]# ./cs.sh 192.168.10.110 root root /root/sshpass-1.06.tar.gz /root/
以上的命令执行后,将把本地/root目录下的src_file文件拷贝到用户名为root,密码为123456的主机192.168.75.130中的/root下,同时还将这个源文件重命名为dest_file。
spawn代表在本地终端执行的语句,在该语句开始执行后,expect开始捕获终端的输出信息,然后做出对应的操作。expect代码中的捕获的(yes/no)内容用于完成第一次访问目标主机时保存密钥的操作。有了这一句,scp的任务减少了中断的情况。代码结尾的expect eof与spawn对应,表示捕获终端输出信息的终止。
使用expect需要了解的一点是:用expect速度会比较慢,因为需要等待返回的数据,然后输入命令执行,没有ssh密钥登录的快速。

后面总结来自:https://blog.csdn.net/nfer_zhuang/article/details/42646849

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多