zoukankan      html  css  js  c++  java
  • 利用expect和sshpass完美非交互性执行远端命令

     
    yum install expect -y

    expect 参考:http://blog.csdn.net/snow_114/article/details/53245466

    yum install sshpass -y

    ssh登陆不能在命令行中指定密码。sshpass的出现,解决了这一问题。sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码。

    它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。

    参考:http://blog.csdn.net/wangjunjun2008/article/details/19993395

    先利用expect 传文件过去 可以实现免yes确认和指定传参密码

    
    
    #!/usr/bin/expect -f
    set timeout 10
    set host [lindex $argv 0]
    set dport [lindex $argv 1]
    set username [lindex $argv 2]
    set password [lindex $argv 3]
    set src_file [lindex $argv 4]
    set dest_file [lindex $argv 5]
    
    spawn scp -P $dport -r $src_file  $username@$host:$dest_file
     expect {
     "(yes/no)?"
      {
        send "yes
    "
        expect "*assword:" { send "$password
    "}
      }
    }
    expect eof
    expect {
     "*assword:"
      {
        send "$password
    "
      }
    }
    expect "100%"
    expect eof

    执行的时候要传参 传参格式:
    [root@sxzros ~]# ./222.sh 172.20.1.6 229 root passwd /root/222 /root/


    然后利用sshpass指定密码免交互对远端机器进行文件解压或者进行文件操作
    #sshpass -p passwd  ssh -t -p 229 root@172.20.1.6 'tar xf /root/ddd1.tar.lzma -C /tmp'
    sshpass -p passwd  ssh -t -p 229 root@172.20.1.6 'cd ~;touch 222.txt'

      

  • 相关阅读:
    Win10 UWP Tile Generator
    Win10 BackgroundTask
    UWP Tiles
    UWP Ad
    Win10 build package error collections
    Win10 八步打通 Nuget 发布打包
    Win10 UI入门 pivot multiable DataTemplate
    Win10 UI入门 导航滑动条 求UWP工作
    UWP Control Toolkit Collections 求UWP工作
    Win10 UI入门 SliderRectangle
  • 原文地址:https://www.cnblogs.com/sysk/p/8142972.html
Copyright © 2011-2022 走看看