zoukankan      html  css  js  c++  java
  • shell中使用ssh

    ssh服务是不能用非交互的方式传递密码,想不输入密码,直接ssh连接到服务器有两种方法,sshpass和expect

    sshpass

    # wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.04/sshpass-1.04.tar.gz?use_mirror=cdnetworks-kr-1
    # tar zxvf sshpass-1.04.tar.gz 
    # cd sshpass-1.04
    # ./configure 
    # make && make install

    sshpass为C编写的一个小程序使用比较简单,用法如下

      sshpass  -p 密码 ssh 用户名@目标IP 要执行的命令

    此处密码为明文。sshpass也可以使用密码文件。具体可以通过‘sshpass -h’看看帮助。

    对于密码安全问题,如使用密码文件可以将文件权限修改为600。如果直接写在shell中,可以使用加密脚本的

    工具,缺点是每次修改都要编译脚本。

    expect

    1.首先确认expect的包要安置。

    [root@localhost]$ rpm -qa | grep expect

    如果没有则需要下载安装,yum -y install expect expect-devel

    安装过后会显示:

    [root@localhost] rpm -qa | grep expect

    expect-5.43.0-5.1
    expect-devel-5.42.1-1

    2.查看expect的路径,可以用

    [root@localhost] which expect 

    /usr/bin/expect

    3.确定脚本有可执行权限

    chmod +x test.sh

    #!/usr/bin/expect   //这个expect的路径就是用which expect 查看的结果

    spawn ssh -l     远程主机的用户名 远程主机的ip地址    
    expect "password:"           //提示让输入密码
    send "远程主机的密码 "       
    interact                             //操作完成

    另外需要注意的是:

    不能按照习惯来用sh test.sh来这行expect的程序,会提示找不到命令,如下:

    test.sh: line 2: spawn: command not found
    couldn't read file "password": no such file or directory
    test.sh: line 4: send: command not found
    test.sh: line 5: interact: command not found

     

    因为expect用的不是bash所以会报错。执行的时候直接./test.sh就可以了

    http://blog.csdn.net/zhuying_linux/article/details/6657020

  • 相关阅读:
    JavaFx在macOS下的文字渲染Bug
    Java多接口同名方法的冲突
    旧技术的惯性
    一点思考(1)
    slisp:编译到JVM平台上的lisp方言
    Arcee:又一个 Parser Generator 轮子
    使用Java实现一门简单的动态语言
    Hello World!
    [NOI2008]奥运物流
    [IOI2005]Riv河流
  • 原文地址:https://www.cnblogs.com/seasonsstory/p/3277473.html
Copyright © 2011-2022 走看看