ssh sshpass随笔

1: 当通过ssh连接远程服务器的时候,可能会出现以下繁琐场景,需要手工输入yes:

ssh username@ip

 这对于某些分布式集群来说是不行的,甚至导致集群都不能启动成功,对于像pssh,pscp这样的自动化工具来说,也不希望这一步的验证,如何在连接的时候不提示这个信息呢:

方法1、ssh -o "StrictHostKeyChecking no" username@hostname
方法2:修改/etc/ssh/ssh_config,在文件最后添加 StrictHostKeyChecking no,接着重启ssh服务

2:远程执行服务器上面的命令可以通过sshpass(当集群机器之间没有免密的时候),如果没有安装,,执行  yum install sshpass -y

用法:

[root@test ~]# sshpass -h
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"         -----通过设置环境变量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

Eg:

(1)使用 -f

[root@test ~]# sshpass -f passwd.txt ssh root@192.168.0.235 "free -m"
              total        used        free      shared  buff/cache   available
Mem:          96405       27169       12563        4066       56672       63775
Swap:           126          19         107

(2)使用 -p 

[root@test ~]# sshpass -p "mypasswd" ssh root@192.168.0.235 "free -m"   
              total        used        free      shared  buff/cache   available
Mem:          96405       27168       12584        4066       56652       63777
Swap:           126          19         107  

注:生产环境不要使用这种方式,不安全

(3)使用 -e

[root@test ~]# export SSHPASS="mypasswd"
[root@test ~]# echo $SSHPASS
mypasswd
[root@test ~]# sshpass -e ssh hduser@192.168.0.235 "free -m"
              total        used        free      shared  buff/cache   available
Mem:          96405       27209       12561        4066       56634       63735
Swap:           126          19         107  

当然,这种方式只针对当前shell有用,如果要配置永久生效,请修改/etc/profile文件

(4)sshpass、ssh都支持多命令调用,只要在命令之间使用&&号就行。

[root@test ~]# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50  ls /home && free -m
hadoop
mysql
yjt
zbc
              total        used        free      shared  buff/cache   available
Mem:            977         364          95          49         518         366
Swap:          4095          35        4060

  

3、如果想要远程机器调用本地脚本,那么可以如下实现

(1)ssh方式

[root@test ~]# ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh 
runstone.com

(2)sshpass方式

[root@test ~]# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh
runstone.com

4、支持sudo

      有些命令需要权限才行,当不想重复输入密码的时候,可以通过这种方式。

(1)格式:cmd ---> 'echo password | sudo -S cmd'

         eg:

[root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S  mkdir /backup'

  注:-S的意思是从标准输入读取密码

      对于echo,dd等命令,可能会出现权限不够问题,如:

[root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S  echo hello > /backup/file'
bash: /backup/file: Permission denied

       对于上述方式,解决办法如下:

(2)格式:cmd ---> 'echo password | sudo -S  sh/bash -c "cmd"'

         eg:

root@test ~]# sshpass -p 123456 ssh  -o 'StrictHostKeyChecking no' yjt@192.168.4.50  'echo 123456 | sudo -S bash -c "echo hello > /backup/file"'

  

  

原文地址:https://www.cnblogs.com/yjt1993/p/10281630.html