Linux下非交互式sshpass登录

摘要

在命令行 非交互的SSH登录的时候,一般我们可以借助于生成用户的公钥私钥对,然后把公钥添加到远程主机的authorized_keys文件,可以实现非交互无密码登录。

其实这里也可以有另外一种方式实现,即用sshpass命令。

这种情况比较适合Mac下用iterm2 SSH登录到远程主机的时候,长时间不操作导致
packet_write_wait: Connection to 192.168.xxx.xxx port 22: Broken pipe问题的解决办法

安装sshpass

#!/usr/bin/env bash
#coding:    utf-8
#author:    Colin
#date:      2016-12-27
#desc:      install sshpass command
# 

## download
cd /tmp

wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz

## extract
tar -zxvf sshpass-1.06.tar.gz

## compile and install
cd sshpass-1.06

sudo ./configure --prefix=/usr/local/sshpass
Password:
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
...

sudo make install

## cp it into path
cp /usr/local/sshpass/bin/sshpass /usr/bin/

## verify
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

配置使用

从上面的显示可以看到,sshpass可以使用-p,-f-e参数

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
  • -p password 直接跟密码
  • -f filename 把密码明文写入到一个文件中通过文件来读取
  • -e 默认从 SSHPASS 系统变量取值,注意是系统变量

举例

使用-p 参数

root@pts/1 $ /usr/bin/sshpass -p 'root' ssh root@192.168.200.23
Warning: Permanently added '192.168.200.23' (ECDSA) to the list of known hosts.
Last login: Tue Dec 27 17:20:58 2016 from 192.168.200.1
apollo-web-test [~] 2016-12-27 20:16:53
root@pts/9 $
apollo-web-test [~] 2016-12-27 20:17:16
root@pts/9 $
apollo-web-test [~] 2016-12-27 20:17:16
root@pts/9 $ exit
登出
Connection to 192.168.200.23 closed.


使用-f参数

QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:17:25
root@pts/1 $ vim /tmp/200.23password
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:17:33
root@pts/1 $ /usr/bin/sshpass -f /tmp/200.23password ssh root@192.168.200.23
Warning: Permanently added '192.168.200.23' (ECDSA) to the list of known hosts.
Last login: Tue Dec 27 20:16:53 2016 from 192.168.200.9
apollo-web-test [~] 2016-12-27 20:17:52
root@pts/9 $ exit
登出
Connection to 192.168.200.23 closed.


使用-e参数,注意 SSHPASS系统变量

使用非系统变量报错,不能正确执行,如下:

QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:17:54
root@pts/1 $ SSHPASS='root'
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:18:08
root@pts/1 $ /usr/bin/sshpass -e ssh root@192.168.200.23
sshpass: -e option given but SSHPASS environment variable not set
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
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:18:18

检查系统变量

root@pts/1 $ env |grep -i SSHPASS
PWD=/tmp/sshpass-1.06
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:19:32

通过export设置系统变量

root@pts/1 $ export SSHPASS='root'
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:19:41
root@pts/1 $ env |grep -i SSHPASS
PWD=/tmp/sshpass-1.06
SSHPASS=root
QA-DB [/tmp/sshpass-1.06] 2016-12-27 20:19:42

再使用 -e 连接

root@pts/1 $ /usr/bin/sshpass -e  ssh root@192.168.200.23
Warning: Permanently added '192.168.200.23' (ECDSA) to the list of known hosts.
Last login: Tue Dec 27 20:17:52 2016 from 192.168.200.9
apollo-web-test [~] 2016-12-27 20:19:45
root@pts/9 $


执行远程命令

root@pts/1 $ /usr/bin/sshpass -f /tmp/.password/pass200.23 ssh root@192.168.200.23 'ls -l /tmp'
总用量 6196
drwxr-xr-x 3 root       root          4096 12月 14 21:01 audit-live-log
drwxr-xr-x 7        600        600    4096 12月 27 17:09 child-code

Mac下iterm2配置定制profile

在Mac下可以定制profile,也就类似windows的xshell下主机的定义,具体如下图:



简书地址:Linux下非交互式sshpass登录


    公众号: DailyJobOps    

时间: 2024-08-17 21:55:13

Linux下非交互式sshpass登录的相关文章

Linux下非交互式远程执行命令脚本

  Linux下非交互式远程执行命令脚本(比ssh更好的方式) openssh在每台机器上都有,ssh与scp经常出现在我们的生活中. 然而当要管理的机器规模越来越大时,ssh登陆到目标机器进行管理就变得不现实了. 虽然可以直接在ssh后面接命令的方式,进行命令的执行. 但是ssh的严格的权限认证使得我们不得不输入密码,或是建立信任关系,很难去自定义一个特定的认证方式. 基于ssh的这种不足.于是笔者就写了一个jetfire,这个工具.比ssh多的一个重要的功能就是可以自定义认证方式,顺便避免了

两种Linux下非交互式命令的实现

一.概述 在Linux环境,有多种实现自动化的脚本语言,如Shell.Python.Perl.Tcl等.Shell语言因与Shell外壳结合紧密,是最常见的实现自动化的脚本语言. 同时,在Linux环境中存在大量功能单一的小工具--通常它们在指定输入后,立即就可获得输出,例如echo.cat等字符串/文本打印工具,又或者是如sed.awk等最常用的字符串编辑工具.通过编写Shell脚本,我们可以反复利用这些小工具来实现一些自动化的批处理. 在少数情况下,我们也需要用到一些交互式的工具,例如pas

linux的tomcat配置-linux下非root用户在MyEclipse2014配置tomcat无法启动的问题!!!

问题描述 linux下非root用户在MyEclipse2014配置tomcat无法启动的问题!!! 我在linux下的MyEcpLise配置类tomcat7,但是启动时却显示: 二月 21, 2015 12:38:08 下午 org.apache.catalina.startup.Catalina load 警告: Unable to load server configuration from [/opt/tomcat7.0/conf/server.xml] 二月 21, 2015 12:3

自己写的一个java程序,怎么在linux下设置成用户登录后启动?

问题描述 自己写的一个java程序,怎么在linux下设置成用户登录后启动? 我自己目前的办法是自己写了一个脚本,内容是:#!/bin/bashcd /ablationjava -jar newablation130222_fat.jar &然后在桌面终端下vi ~/.bash_profile 按i在文档的最后加入sh /ablation/auto.sh按esc然后按:w 回车键 但是这个办法不是很好,我希望通过纯代码实现,或者写一个脚本让java调用这个脚本在实现,请问各位该怎么办啊

Linux下实现免密码登录(超详细)_Linux

1.Linux下生成密钥 ssh-keygen的命令手册,通过"man ssh-keygen"命令: 通过命令"ssh-keygen -t rsa" 生成之后会在用户的根目录生成一个 ".ssh"的文件夹 进入".ssh"会生成以下几个文件 authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥 id_rsa : 生成的私钥文件 id_rsa.pub : 生成的公钥文件 know_hosts

linux下ssh免密码登录远程服务器配置

在linux下使用ssh登录远程主机是非常容易的事,但对于懒人来说,经常输入密码却是一个非常痛苦的事,下面告诉你怎么免输入远程主机密码而ssh登录到服务器. 以下操作均在本地主机上进行,非root用户,系统是debian wheezy. 本地主机: ssh-keygen -b 1024 -t rsa 连续按Enter回车键即可,就会在~/.ssh/下生成两个文件: id_rsa  id_rsa.pub 我们需要做的就是上传id_rsa.pub到远程主机. #远程主机添加.ssh目录 ssh le

如何让 Linux 下非 root 用户程序使用小于 1024 端口

在 Linux 下,默认情况下1024 以下的端口是要在 root 下才能使用的,在其他用户下,如果尝试使用将会报错.在有的时候,我们可能考虑程序运行在 root 帐户下,但这可能会给 Linux 系统带来安全风险.那如何能够让非 root 用户运行的程序能够对外启用小于 1024 的端口呢? 本文尝试给出一些方法:  (题图来自: wordpress.com) 第一种方法:SetUID 给用户的应用程序在执行位设置用户 ID 能够使程序可以以 root 权限来运行,这个方法让程序能够像在 ro

Linux下交互式与非交互式修改用户密码的例子_linux shell

最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将qa帐号的密码改为1234,后来还为了脚本化,很方便的执行,还使用了非交互式地修改用户的密码.简单记录一下吧. 1. 交互式配置本地用户的密码:passwd 命令 复制代码 代码如下: [root@host_221-81 ~]# passwd qa Changing password for user qa. New password: BAD PASSWORD: it is too short BAD PASSWORD: is

区分交互式和非交互式shell、登录和非登录shell之间不同

交互式shell和非交互式shell.登录shell和非登录shell的区别. 首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(interactive shell and non-interactive shell) 交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.退出.当你退出后,shell也终止了