sshpass
       
        使
       
       
        Linux
       
       
        可以明文参数输入
       
       
        SSH
       
       
        密码
       
      
     
      
      
     
      
      
      
       
        这几天配置一台服务器,在某云平台创建云服务器后,生成了巨长、巨复杂的一串密码,在输入几十次密码后,依然是密码错误。这时候就想如果密码是非交互式输入,可以将密码做为参数或从文件输入就太好了。
       
       sshpass
       
        就是一款密码输入辅助工具,它可以从命令行明文参数、文件或环境变量中指定密码,从而避免交互式密码输入。
       
      
     
      
       
        1. sshpass安装
       
      
     
      
       
        首先从以下网址下载
       
       sshpass
       
        源码:
       
      
     
      
       https://sourceforge.net/projects/sshpass/
      
     
      
       
        下载后解码压,然后进入源码目录:
       
      
     
      
      
     
      
       tar -zxvf sshpass-1.06.tar.gz
      
     
      
       cd sshpass-1.06
      
     
      
       ./configure
      
     
      
       make &&make install
      
     
      
       
        详细操作下:
       
      
     
      
      
     
      
       [root@207_syslog src]# ls
      
     
      
       sshpass_1.06.orig.tar.gz
      
     
      
       [root@207_syslog src]# tar xf sshpass_1.06.orig.tar.gz
      
     
      
       [root@207_syslog src]# ls
      
     
      
       sshpass-1.06  sshpass_1.06.orig.tar.gz
      
     
      
       [root@207_syslog src]# cd sshpass-1.06/
      
     
      
       [root@207_syslog sshpass-1.06]# ls
      
     
      
       aclocal.m4  compile      configure.ac  INSTALL     Makefile.am  NEWS
      
     
      
       AUTHORS     config.h.in  COPYING       install-sh  Makefile.in  README
      
     
      
       ChangeLog   configure    depcomp       main.c      missing      sshpass.1
      
     
      
       [root@207_syslog sshpass-1.06]# ./configure
      
     
      
       [root@207_syslog sshpass-1.06]# make && make install
      
     
      
      
     
      
       
        在
       
       Mac
       
        系统下,也可以使用
       
       
        brew
       
       
        安装:
       
      
     
      
       brew install sshpass
      
     
      
      
     
      
       
        2. sshpass的参数
       
      
     
      
       s
      
      
       shpass
       
        安装后,可以在控制台输入
       
       
        sshpass
       
       
        命令查看所有选项参数:
       
      
     
      
       $ 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
      
     
      
       
        如上所示,
       
       command parameters
       
        为你要执行的需要交互式输入密码的命令,如:
       
       
        ssh
       
       
        、
       
       
        scp
       
       
        等。当
       
       
        sshpass
       
       
        没有指定参数时会从
       
       
        stdin
       
       
        获取密码,几个密码输入相关参数如下:
       
      
     
      
       -f filename
       
        :从文件中获取密码
       
      
     
      
       -d number
       
        :使用数字作为获取密码的文件描述符
       
      
     
      
       -p password
       
        :指定明文本密码输入(安全性较差)
       
      
     
      
       -e
       
        :从环境变量
       
       
        SSHPASS
       
       
        获取密码
       
      
     
      
      
     
      
       
        3. sshpass的使用
       
      
     
      
      
      
       
        现有一台服务器登录密码是
       
       AHDXyjs2012
       
        ,登陆的
       
       
        IP
       
       
        为
       
       
        172.17.9.200
       
       
        。使用
       
       
        sshpass
       
       
        的几种录方式如下。
       
      
     
      
       1.
      
      
       
        将密码写入文件,并从文件获取登录密码:
       
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass echo “AHDXyjs2012” > user.passwd
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass -f user.passwd ssh root@172.17.9.200
      
     
      
       Last login: Wed Dec  7 10:40:13 2016 from 172.17.9.206
      
     
      
      
     
      
       2.
      
      
       
        以明文的方式输入密码:
       
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass -p AHDXyjs2012 ssh root@172.17.9.200
      
     
      
       Last login: Wed Dec  7 11:00:12 2016 from 172.17.9.206
      
     
      
      
     
      
       3.
      
      
       
        从环境变量中获取密码:
       
      
     
      
       [root@207_syslog sshpass-1.06]#  export SSHPASS=”AHDXyjs2012″
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass -e ssh root@172.17.9.200
      
     
      
       Last login: Wed Dec  7 11:02:34 2016 from 172.17.9.207
      
     
      
      
     
      
       4.
      
      
       
        远程执行命令
       
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass -p AHDXyjs2012 ssh root@172.17.9.200 “ifconfig|grep inet”
      
     
      
       inet 172.17.9.200  netmask 255.255.255.192  broadcast 172.17.9.255
      
     
      
       inet6 fe80::250:56ff:feb0:710e  prefixlen 64  scopeid 0x20<link>
      
     
      
       inet 127.0.0.1  netmask 255.0.0.0
      
     
      
       inet6 ::1  prefixlen 128  scopeid 0x10<host>
      
     
      
       inet 172.17.9.208  netmask 255.255.255.255
      
     
      
       inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
      
     
      
      
     
      
       5.
       
        远程执行脚本
       
      
     
      
       n
      
      
       
        远端的新建脚本
       
      
     
      
       [root@200_tomcat ~]# ll /opt/test.sh
      
     
      
       -rw-r–r–. 1 root root 61 Dec  7 11:18 /opt/test.sh
      
     
      
       [root@200_tomcat ~]# cat /opt/test.sh
      
     
      
       #!/bin/bash
      
     
      
       echo “this test for remote”
      
     
      
       ifconfig | grep inet
      
     
      
      
     
      
       n
      
      
       
        本地端执行远端程序
       
      
     
      
       [root@207_syslog sshpass-1.06]# sshpass -p AHDXyjs2012 ssh root@172.17.9.200 “sh /opt/test.sh”
      
     
      
       this test for remote
      
     
      
       inet 172.17.9.200  netmask 255.255.255.192  broadcast 172.17.9.255
      
     
      
       inet6 fe80::250:56ff:feb0:710e  prefixlen 64  scopeid 0x20<link>
      
     
      
       inet 127.0.0.1  netmask 255.0.0.0
      
     
      
       inet6 ::1  prefixlen 128  scopeid 0x10<host>
      
     
      
       inet 172.17.9.208  netmask 255.255.255.255
      
     
      
       inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
      
     
      
       
        此时说明,已经执行了远端程序。
       
      
     
      
       4.
      
      
       
        Sshpass 软件包
       
      
     
       
     
      
      
     
      
      
     
转载于:https://blog.51cto.com/caimengzhi/1880275
 
