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"'

  

  

ssh sshpass随笔的更多相关文章

  1. ssh整合随笔(注解方式,Spring 管理action)

    Web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  2. sshpass 配置密码登录ssh

    tar -zxvf sshpass-1.06.tar.gzcd sshpass-1.06./configuremake && make install sshpass -p userp ...

  3. 终端-Linux命令之非交互SSH密码验证-Sshpass

    Sshpass是使用SSH所谓的"交互式键盘密码身份验证"以非交互方式执行密码身份验证的工具 通俗来说就是 使用ssh密码登录 是需要在连接时手动输入密码的,没办法明文连接,如下图 ...

  4. ssh免秘钥登录

    简介 SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程 ...

  5. Linux-远程服务ssh

    1.远程管理服务介绍 (1)SSH是(Secure Shell Protocol)的简写,由IETF网络工作小组制定:在进行数据传输之前,SSH先对联机数据包通过加密技术进行机密处理,加密后在进行文件 ...

  6. Ubuntu16.04 Linux 下安装、配置SSH

    本人在Win7+VMware下利用两个ubuntu虚拟机安装.配置.测试了SSH. 在Server端安装openssh-server. sudo apt-get install ssh # 安装ssh ...

  7. K8S学习笔记之二进制部署Kubernetes v1.13.4 高可用集群

    0x00 概述 本次采用二进制文件方式部署,本文过程写成了更详细更多可选方案的ansible部署方案 https://github.com/zhangguanzhang/Kubernetes-ansi ...

  8. 工具用的好,下班回家早!5分钟玩转iTerm2!

    同时打开多个终端窗口,来回切换太麻烦! 能不能像IDEA一样,能够查看历史粘贴记录? 有没有办法一键登陆服务器? 工欲善其事,必先利其器!无论工作还是学习,选择好用的工具真的太重要了.今天就给大家介绍 ...

  9. Docker容器获取宿主机信息

    最近在做产品授权的东西,开始宿主机为Window,程序获取机器硬件信息相对简单些,后来部署时发现各种各样的的环境问题,所有后来改用dokcer部署,docker方式获取宿主机信息时花了些时间,特此记录 ...

随机推荐

  1. linux环境下jdk安装

    1,下载jdk版本 jdk-7u25-linux-x64.tar.gz  和windows jdk一致,jvm有区别: 2,拷贝到 /home目录下.通过tar -zxvf jdk-7u25-linu ...

  2. [转发]C++中new和malloc的区别

    原文地址:https://blog.csdn.net/linux_ever/article/details/50533149 new与malloc的10点区别 1. 申请的内存所在位置 new操作符从 ...

  3. UI5-技术篇-Expand与Deep 服务测试

    1.SEGW创建服务 2.创建Data Model 2.1Entity Types ZRICO_USR 设置主键.排序字段.过滤字段 ZRICO_USRITM设置主键  2.2Associations ...

  4. SVN上传本地项目到服务器

    1. 在服务器新建一个文件夹目录: 2. 将新建的目录在本地check out下来: 3. 将自己的项目拷贝到check out下来的文件夹下: 4. 右键点击svnàAdd,选择所有添加: 5. 右 ...

  5. Django 启动报错 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7

    pycharm 报错 cmd 报错 解决办法 首先 是计算机 编码问题  是 django 读取你的  用户host名 但是 windos 用户名 如果是中文 就会报这个错  要改成 英文

  6. Computer Vision_33_SIFT:A novel coarse-to-fine scheme for automatic image registration based on SIFT and mutual information——2014

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  7. mysql的2种备份mysqldump 和 Xtrabackup

    mysqldump备份方式 备份 mysqldump -uroot -p 数据库名 > 备份的文件名 恢复(先关闭数据库) mysql -uroot -p 数据库名 < 备份的文件名 Xt ...

  8. 写Django项目的开发工具或者编辑器

    pycharm是写django的最好的编辑器,优先使用pycharm.到官网下载pycharm →https://www.jetbrains.com/pycharm/download→ pycharm ...

  9. Linux命令——pgrep

    参考:Linux pgrep Command Tutorial for Beginners (10 Examples) Linux命令——ps.pstree bash基础——grep.基本正则表达式. ...

  10. CentOS8 NextCloud 私有云存储搭建

    本文首发:https://www.somata.work/2019/CentOS8NextCloudBuild.html 之前发现 Owncloud 越来越捞了,推出了企业版和社区版,近几日突然发现原 ...