首页   >   代码编程

安装sshpass工具图文教程

几种快速上传文件到Linux服务器的方法一文中,提到了SCP方式上传,这种方式由于没有图形化界面,所以速度非常的快,但是有一个不爽的地方就是需要输入密码,简单的密码还好,如果是那种加密算法生成的很长一串的密码,那简直要人老命。

查了一下资料,可以利用sshpass工具来避免每次输入密码,而是直接将密码带着一起去执行命令,这样就方便很多了。

felixdeMacBook-Pro:5.1.34 felix$ sshpass -p test scp /Users/felix/workspace2/test/target/test.jar root@147.198.153.112:/usr/local/test

但是当我使用sshpass命令的时候,却报错“-bash: sshpass: command not found”,原来这个脚手架工具不是系统自带的,需要我们自己安装。

老规矩,去官网下载sshpass的二进制安装包

官网地址:http://sourceforge.net/projects/sshpass/files

下载完毕之后,解压目录如下:

安装sshpass工具图文教程

有configure,直接./configure

felixdeMacBook-Pro:Cellar felix$ ./configure
-bash: ./configure: No such file or directory
felixdeMacBook-Pro:Cellar felix$ cd sshpass-1.06/
felixdeMacBook-Pro:sshpass-1.06 felix$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking whether gcc understands -c and -o together... (cached) yes
checking dependency style of gcc... (cached) gcc3
checking for ANSI C header files... (cached) yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking sys/ioctl.h usability... yes
checking sys/ioctl.h presence... yes
checking for sys/ioctl.h... yes
checking for unistd.h... (cached) yes
checking termios.h usability... yes
checking termios.h presence... yes
checking for termios.h... yes
checking for an ANSI C-conforming const... yes
checking for pid_t... yes
checking for ssize_t... yes
checking vfork.h usability... no
checking vfork.h presence... no
checking for vfork.h... no
checking for fork... yes
checking for vfork... yes
checking for working fork... yes
checking for working vfork... (cached) yes
checking whether gcc needs -traditional... no
checking for stdlib.h... (cached) yes
checking for GNU libc compatible malloc... yes
checking sys/select.h usability... yes
checking sys/select.h presence... yes
checking for sys/select.h... yes
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking types of arguments for select... int,fd_set *,struct timeval *
checking return type of signal handlers... void
checking for select... yes
checking for posix_openpt... yes
checking for strdup... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

make

felixdeMacBook-Pro:sshpass-1.06 felix$ make
/Library/Developer/CommandLineTools/usr/bin/make  all-am
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc  -g -O2   -o sshpass main.o

make install

felixdeMacBook-Pro:sshpass-1.06 felix$ make install
 ./install-sh -c -d '/usr/local/bin'
  /usr/bin/install -c sshpass '/usr/local/bin'
 ./install-sh -c -d '/usr/local/share/man/man1'
 /usr/bin/install -c -m 644 sshpass.1 '/usr/local/share/man/man1'

文件上传了,但是报了一个错

felixdeMacBook-Pro:sshpass-1.06 felix$ sshpass -p test scp /Users/felix/workspace2/reader_v2/target/reader.jar root@147.198.153.112:/usr/local/test
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

虽然报错了,但是还是可以上传。。。

原因:本机的$LC_CTYPE和服务器的$LC_CTYPE不一致

本地查看是UTF-8

felixdeMacBook-Pro:sshpass-1.06 felix$ echo $LC_CTYPE

服务器查看是空

[root@gitlab ~]# echo $LC_CTYPE

[root@gitlab ~]# 

直接查看一下/etc/locale.conf文件

[root@gitlab ~]# cat /etc/locale.conf
LANG=en_US.utf8

[root@gitlab ~]# 

只有LANG属性,需要我们手动给它配置一个LC_CTYPE属性

[root@st test]# cat /etc/locale.conf 
LC_CTYPE=en_US.UTF-8
LANG=en_US.UTF-8

[root@st test]# 

想让配置生效,还需要source一下

[root@gitlab ~]# source /etc/locale.conf

这里需要注意,虽然在本地电脑上echo的时候看到的是UTF-8,但是这里最好是和LANG的值保持一致,写成“en_US.UTF-8”,我分别在两台linux服务器上做过测试,其中一台写成UTF-8的时候source失败。

配置生效之后,我们再来echo查看一下

[root@st test]# echo $LC_CTYPE
en_US.UTF-8

[root@st test]#

此时,我们在客户端再次执行scp进行上传文件,就不会再有问题了

felixdeMacBook-Pro:5.1.34 felix$ sshpass -p test scp /Users/felix/workspace2/test/target/test.jar root@147.198.153.112:/usr/local/test
felixdeMacBook-Pro:5.1.34 felix$ 

不造为什么,实现了免密之后,上传进度百分比看不见了。。。

安装包网盘下载地址:sshpass-1.06二进制安装包免费下载

QQ群: 686430774  /  718410762

站长Q: 1347384268

如果文章有帮到你,可以考虑请博主喝杯咖啡!

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2019-03-15

永久地址:https://www.jiweichengzhu.com/article/6f35bef8a0e942edb2e4a7b559a5b91f

评论