原文章摘自: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. HashMap和SparseArray的性能比较。

    HashMap和SparseArray可以实现相似的功能. 但SparseArray是Android定义的,在键是整数时,他比HashMap的性能更高,因为HashMap使用的是Integer对象, ...

  2. C#操作Excell常用方法

    这是在博客园看的文章,写的很好,所以转一下,方便自己以后用到的时候可以随时查看. range.NumberFormatLocal = "@";     //设置单元格格式为文本 r ...

  3. Android自定义控件5--轮播图广告ViewPager基本实现

    本文地址:http://www.cnblogs.com/wuyudong/p/5918021.html,转载请注明源地址. 本文开始实现轮播图广告系列,这篇文章首先实现让图片滑动起来(ViewPage ...

  4. 转载文章-----Rational Rose2007(v7.0)下载地址、安装及激活详解教程(图)

    转载地址:http://www.cnblogs.com/leaven/p/3718361.html 最近需要画uml图,之前用的是Rose 2003版的,由于好久没进去了,结果发现原来的激活又失效了, ...

  5. 查看mysql数据库版本方法总结

    当你接手某个mysql数据库管理时,首先你需要查看维护的mysql数据库版本:当开发人员问你mysql数据库版本时,而恰好你又遗忘了,那么此时也需要去查看mysql数据库的版本............ ...

  6. ORACLE使用GV_$TEMP_SPACE_HEADER统计临时表空使用情况不准确的问题

    以前写了一篇ORACLE临时表空间总结的文章, 里面介绍了几个查看临时表空间使用情况的脚本,其中一个脚本如下所示: SELECT TU.TABLESPACE_NAME                  ...

  7. JVM之Class文件结构

    每一个class文件对应一个类或者接口,但是一个类或者接口不一定生成class文件,classloader直接生成. 8为字节为基础的二进制流,各个数据项按照严格的顺序排列在class文件中,没有任何 ...

  8. Tokudb 参数优化

    tokudb_row_format tokudb_fast: 使用quicklz 库的压缩模式.(推荐)tokudb_small: 使用 lzma 库的压缩模式.tokudb_zlib: 使用 zli ...

  9. 【转】MySQL批量SQL插入各种性能优化

    原文:http://mp.weixin.qq.com/s?__biz=MzA5MzY4NTQwMA==&mid=403182899&idx=1&sn=74edf28b0bd29 ...

  10. Mysql错误:Ignoring query to other database解决方法

    Mysql错误:Ignoring query to other database解决方法 今天登陆mysql show databases出现Ignoring query to other datab ...