将脚本或文件推送到其他服务器上并执行的Script

sshcmd脚本如下:

#!/usr/local/bin/expect


set COMD [lindex $argv 0]
set LIST [lindex $argv 1]

set timeout 60
set ADMI_PASS "PASSWD1" #远程服务器的sysadmin密码
set ROOT_PASS "PASSWD2" #远程服务器根用户密码

proc action {HOST} {
puts stdout "#############################################################################################"
puts stdout $HOST
puts stdout "#############################################################################################"

global COMD
global ADMI_PASS
global ROOT_PASS

set PASS $ADMI_PASS

#################################################################################
# -gf 拽回执行结果 sshcmd -gf mx1.dns.com.cn out.txt
if {[string equal $COMD "-gf"]==1} {
global argv
set REMOTE_FILE [lindex $argv 2]
set LOCAL_FILE [lindex $argv 3]

spawn -noecho scp sysadmin@$HOST:$REMOTE_FILE $LOCAL_FILE.$HOST
expect {
"yes/no" {
send "yes\r";
exp_continue
}
"Password:" {
if {[string equal $PASS $ROOT_PASS]==0} {
send "$PASS\r";
exp_continue
}
}
}

#################################################################################
} else {
if {[string equal $COMD "-pfc"]==1} {
global argv
set LOCAL_FILE [lindex $argv 2]
set REMOTE_FILE [lindex $argv 3]

# -pfc 推送并执行 sshcmd -pfc mx1.dns.com.cn /tmp/hello.sh
spawn -noecho scp $LOCAL_FILE sysadmin@$HOST:$REMOTE_FILE
expect {
"yes/no" {
send "yes\r";
exp_continue
}
"Password:" {
if {[string equal $PASS $ROOT_PASS]==0} {
send "$PASS\r";
exp_continue
}
}
}

set PROC "chmod +x $REMOTE_FILE ; $REMOTE_FILE &"

} else {
puts stdout "-gf 拽回结果 sshcmd -gf mx1.dns.com.cn /tmp/KevinShell/KevinShell.txt out.txt"
puts stdout "-pfc 推送并执行 sshcmd -pfc mx1.dns.com.cn /tmp/hello.sh /tmp/KevinShell/hello.sh"
exit
}

spawn -noecho ssh sysadmin@$HOST
expect {
"yes/no" {
send "yes\r";
exp_continue
}
"Password:" {
# login as sysadmin
if {[string equal $PASS $ROOT_PASS]==0} {
send "$PASS\r";
send "su\r";
set PASS $ROOT_PASS

exp_continue
} else {
# login as root
send "$PASS\r";
send "bash\r";

if {[string length $PROC]!=0} {
send "$PROC\r"
send "exit\r"
send "exit\r"
send "exit\r"

exp_continue
} else {
interact
}
}
}
}
}
return
}


if {[file exists $LIST]==1} {
set FP [open $LIST r]
while { [gets $FP HOST] && [string length $HOST]!=0 } {
action $HOST
}
close $FP
} else {
action $LIST
}


实际应用:

将脚本MailAddress.sh推送到其他服务器上/tmp目录下,但不执行。
需要注释掉sshcmd脚本的这一行 “set PROC "chmod +x $REMOTE_FILE ; $REMOTE_FILE &"”

#!/bin/sh
cd /home/sysadmin/zhaoyj/
for i in 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 51 52 53 54 55 56 57 58
#5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 51 52 53 54 55 56 57 58
do
./sshcmd -pfc mx$i.dns.com.cn MailAddress.sh /tmp/
done


./sshcmd -pfc m-g3.dns.com.cn MailAddress.sh /tmp/
./sshcmd -pfc fmx1.dns.com.cn MailAddress.sh /tmp/


------------------------------------------------------
将脚本qiantao.sh推送到其他服务器重命名为Execute.sh(自定义)上,并执行,注意前后变化。


#!/bin/sh
cd /home/sysadmin/zhaoyj/
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 51 52 53 54 55 56 57 58
do
./test.sh -pfc mx$i.dns.com.cn qiantao.sh /tmp/Execute.sh #名字是自定义的
done


./test.sh -pfc m-g3.dns.com.cn qiantao.sh /tmp/Execute.sh
./test.sh -pfc fmx1.dns.com.cn qiantao.sh /tmp/Execute.sh



-------------------------------------------
[root@ctrl /home/sysadmin/zhaoyj]# cat qiantao.sh

#!/bin/sh
cat /tmp/MailAddress.sh >> /root/crontab/statitstic/statistic.sh






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值