• 脚本实现scp传输文件和ssh登陆执行命令

    发布时间:2018-06-22 17:43:15
    作者:ynkulusi
  • #!/bin/bash
    
    CMD_PROMPT="\](\$|#)"
    script="/home/kulusi/scripts/test.sh"
    username='user'
    password='pass'   #如果传递的变量中带特殊自己要加\转义,如$[*.`?)}等
    
    while read hostip; do
    expect -c "
        spawn scp $script $username@$hostip:/home/kulusi/scripts
        expect {
            *yes/no* { send -- yes\r;exp_continue;}
            *assword* { send -- $password\r;}
        }
        expect 100%
        expect eof
    "
    
    expect -c "
        set timeout 1
        spawn ssh $username@$hostip
        expect "*assword*"
        send -- $password\n
        expect -re $CMD_PROMPT
        send "$script\r"
        expect -re $CMD_PROMPT
        send {date|awk '{print \$4}'}
        send \r
        send crontab\ -l\ >\ /tmp/crontab.bak\r
        send {echo '0 * * * * /usr/bin/uptime &>/dev/null' >> /tmp/crontab.bak}
        send \r
        send crontab\ /tmp/crontab.bak\r
        send rm\ -rf\ /tmp/crontab.bak\r
        expect -re $CMD_PROMPT
        exit
    "
    done</home/kulusi/scripts/iplist
    
  • 分类:linux
    标签: ssh_cmd
    评论数:0 阅读数:1005