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