原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/

Linux下实现免交互登陆一般有两种:

1. SSH无密码认证方式

客户端使用ssh-keygen生成密钥对,将公钥复制到服务端(authorized_keys),SSH提供公钥登陆,当SSH访问服务端时,服务端先在本机寻找客户端的公钥,然后把客户端发来的公钥进行比较,如果一致,则用公钥加密给客户端,客户端再用私钥进行解密,实现加密所有传输的数据。

1>.在客户机上创建密钥对

# ssh-keygen -t rsa #一路回车

2>.登陆ssh服务器,创建.ssh目录及设置权限

1
2
# mkdir /root/.ssh
# chmod 700 /root/.ssh

3>.将公钥上传到服务器并重命名为authorized.keys

1
# scp /root/.ssh/id_rsa.pub root@服务端IP:/root/.ssh/authorized_keys #id_rsa.pub可以追加多个客户端的公钥

4>.设置ssh服务器

1
2
3
4
5
6
# vi /etc/ssh/sshd_config 
RSAAuthentication yes           #这三行取消注释,开启密钥对验证
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no    #关闭密码验证
# service sshd restart

5>.免交互登陆测试,并查看远程主机磁盘分区

1
# ssh root@服务端IP 'df -h'

2. 利用expect工具自动实现交互任务

Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。

CentOS安装:yum install expect

Ubuntu安装:sudo apt-get install expect

1>.免交互登陆,查看远程主机磁盘分区

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/expect
set ip 192.168.1.156
set pass 123.com    
set timeout 30
spawn ssh root@$ip
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$pass\r"}
}
expect "root@*"  {send "df -h\r"}
expect "root@*"  {send "exit\r"}
expect eof
# interact
 
 
还有就是免密码scp
[root@WEB1-live sh]# cat scp_licai_war.sh
#!/usr/bin/expect
set ip 10.9.10.2
set pass Weddvasdfa
set timeout 30
#spawn ssh root@$ip
spawn scp /root/test/esb_fabric.1611231739.tar.gz root@10.9.10.2:/data/pro_fabu/front
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$pass\r"}
}
#expect "root@*"  {send "df -h\r"}
#expect "root@*"  {send "exit\r"}
#expect eof
spawn ssh root@$ip
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$pass\r"}
}
expect "root@*"  {send "df -h\r"}
expect "root@*"  {send "exit\r"}
expect eof

2>.在Shell脚本中嵌入Expect语法

方法1:使用EOF,将内容段让expect执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
user=root
pass='123'
ip='192.168.1.154'
/usr/bin/expect << EOF
set timeout 30
spawn ssh $user@$ip   
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$pass\r"}
}
expect "root@*"  {send "df -h\r"}
expect "root@*"  {send "exit\r"}
expect eof 
EOF
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
user=root
pass='123'
ip='192.168.1.154'
expect -c "
    spawn ssh $user@$ip
    expect {
        \"(yes/no)\" {send \"yes\r\"; exp_continue}
        \"password:\" {send \"$pass\r\"; exp_continue}
        \"root@*\" {send \"df -h\r exit\r\"; exp_continue}
    }"

方法2:将expect脚本独立出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# vi login.exp      #免交互登陆脚本
#!/usr/bin/expect 
set ipaddress [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
if { $argc != 3 } {
puts "Usage: expect login.exp ipaddress username password"
exit 1
}
set timeout 30
spawn ssh $username@$ipaddress
expect {
        "(yes/no)" {send "yes\r"; exp_continue}
        "password:" {send "$password\r"}
}
expect "$username@*"  {send "df -h\r"}
expect "$username@*"  {send "exit\r"}
expect eof
1
2
3
4
5
6
7
8
9
10
11
# vi user_info     #用户信息文件
192.168.1.156   user    user
192.168.1.154   root    123.com
# vi expect.sh     #读取用户信息并赋值到变量
#!/bin/bash
for ip in `awk '{print $1}' user_info`
do
    user=`awk -v I="$ip" '{if(I==$1)print $2}' user_info`
    pass=`awk -v I="$ip" '{if(I==$1)print $3}' user_info`
    expect login.exp $ip $user $pass
done

参数说明:

set:可以设置超时,也可以设置变量

timeout:expect超时等待时间,默认10S

spawn:执行一个命令

expect "":匹配输出的内容

exp_continue:继续执行下面匹配

\r:可以理解为回车

$argc:统计位置参数数量

[lindex $argv 0]:脚本后第一个参数,类似于shell中$1,以此类推

puts:打印字符串,类似于echo

awk -v I="$ip":赋值变量

expect{...}:输入多行记录

其他参数说明:

timeout -1:永不超时退出

log_file /var/log/expect.log:记录交互信息,一般crontab时使用

interact:交互后不退出远程终端,如果加要把expect "root@*" {send "exit\r"}注释掉,如果不加,就直接退出

将spawn ssh root@$ip换成spawn ssh -o StrictHostKeyChecking=no root@ip既不会再提示是否将服务器计算机密钥加入本地known_hosts

linux免交互登陆远程主机并执行命令(密钥对和Expect)的更多相关文章

  1. ssh 免交互登录 ,远程执行命令脚本。

    ##免交互SSH登录auto_login_ssh () {    expect -c "set timeout -1;                spawn -noecho ssh -o ...

  2. shell脚本批量ssh登陆主机并执行命令

    shell脚本批量ssh登陆主机并执行命令 今天在客户现场遇到了这个问题,客户没有管理工具,无法批量登陆主机下发命令,几个个C段啊,让我一个一个登陆,.................. 所以写了个s ...

  3. Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x)

    Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x) 转自:http://jakfruit.blog.163.com/blog/stat ...

  4. expect实现远程主机自动执行命令脚本

    2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...

  5. Linux免密登陆配置(互信配置)

    Linux免密登陆配置(互信配置) 1.生成当前用户的秘钥文件 [oracle@localhost .ssh]$ ssh-keygen -t rsa 2.配置远程登录用户的公钥文件 将公钥文件拷贝至另 ...

  6. linux 免密码登陆

    1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...

  7. Linux使用expect实现免手动密码输入,linux免密码登陆

    使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理.  脚本代码如下:  ###### ...

  8. ansible 批量在远程主机上执行命令

    ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...

  9. Linux免密登陆设置了免密登陆为啥还需要输入密码

    一.设置了免密码登陆但是还是需要输入密码: 权限保证:1.authorized-keys 的权限为 600 2.home.账户所在的目录如hadoop..ssh这三个文件的权限都必须设置为700,缺少 ...

随机推荐

  1. 解决xcode升级之后安装的插件失效

    title: 解决xcode升级之后安装的插件失效date: 2015-08-23 11:07:53categories: 编辑工具 tags: xcode 我的博客:http://daycoding ...

  2. Android自定义控件7--自定义开关--绘制界面内容

    本文实现全自定义控件--自定义开关 本文地址:http://www.cnblogs.com/wuyudong/p/5922316.html,转载请注明源地址. 自定义开关 (View),本文完成下面内 ...

  3. android gradle NDK简介

    本章介绍在Android开发中,关于NDK,gradle相关的知识点. 1.NDK简介 (1)NDK是一系列工具的集合 NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将s ...

  4. php关键词替换的类(避免重复替换,保留与还原原始链接)

    转载:http://www.169it.com/blog_article/601549531.html 本节主要内容:一个关键词替换的类 主要可以用于关键词过滤,或关键词查找替换方面. 实现过程分析: ...

  5. mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

    AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...

  6. [Java入门笔记] Java语言基础(四):流程控制

    流程控制指的是在程序运行的过程中控制程序运行走向的方式.主要分为以下几种: 顺序结构 顺序结构,顾名思义,是指程序从上往下逐步顺序执行.中间没有任何的判断和跳转. 分支结构 Java提供两种分支结构: ...

  7. CST时间转换成 yyyy-MM-dd格式

    将 "Tue Oct 28 12:12:10 CST 2010" 时间格式转成 "2010-10-28 12:12:10" 格式: + (NSString *) ...

  8. Xamarin Error cannot find ‘aapt.exe’

    Problem:     solution:   A workaround is to copy your files to the old directory. Just copy the aapt ...

  9. 学习大神笔记之“MyBatis学习总结(三)”

    一.连接数据库的配置单独放在一个properties文件中          创建db.peoperties----保存数据库配置信息      driver=com.mysql.jdbc.Drive ...

  10. java设计模式之原型模式

    原型模式概念 该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.java中复制通过clone()实现的.clone中涉及深.浅复制.深.浅复制的概念如下: ⑴浅复制 ...