自动scp脚本,使用了bash shell和expect:
1.进行单一文件或文件夹的传输
参数1:源单一文件或文件夹
参数2:目标地址
</home/jiang/bin/Auto/SingleScp>
#!/bin/bash
if scp -r $1 $2;then
echo $1 SingleScp Success
else
echo $1 SingleScp Not Success
fi
2.expect脚本,运行SingleScp,并提供scp的密码.
参数1:源单一文件或文件夹
参数2:目标地址
参数3:密码
</home/jiang/bin/Auto/AutoScp>
#!/usr/bin/expect
spawn /home/jiang/bin/Auto/SingleScp [lindex $argv 0] [lindex $argv 1]
#The Scptime is 10hours
set timeout 36000s
expect "*@*pass*"
send "[lindex $argv 2] "
expect "*SingleScp*Success*"
3.主脚本,可以用ln -s命令链接一系列特定的服务器上传脚本.
</home/jiang/bin/SelfScp>
#!/bin/bash
#to find the number of parameter
#这是密码设定,当然是不安全,但是方便,一般的为自己的常用密码
PassWord="password"
#这是用于
case $0 in
*ff*)
Goal="usrname@address:directory"
#可以设定密码
#PassWord="password"
;;
*farm*)
Goal="usrname@address:directory"
#PassWord="password"
;;
*li*)
Goal="usrname@address:directory"
;;
*zh*)
Goal=usrname@address:directory""
;;
*Self*)
echo "Enter the Goal(name@IPname):"
read Goal
echo "Enter PassWork:"
read PassWork
;;
*)
;;
esac
for i in $@
do
if echo $i|grep ^/ ;then
way1=$i
else
way1=$PWD/$i;
fi
echo Scp $way1 To $Goal
/home/jiang/bin/Auto/AutoScp $way1 $Goal $PassWord
done
echo All Has Been Copied!
4.特定的服务器上传脚本
ln -s /home/jiang/bin/SelfScp /home/jiang/bin/farmscp
#就可以直接使用
farmscp 文件1 文件2
5.也可从服务器自动下载
</home/jiang/bin/SelfGet>
#!/bin/bash
#to find the number of parameter
PassWord="passwd"
MyD="/home/jiang/"
case $0 in
*ff*)
Goal="usrname@address:directory"
#可以设定密码
#PassWord="password"
;;
*farm*)
Goal="usrname@address:directory"
#PassWord="password"
;;
*li*)
Goal="usrname@address:directory"
;;
*zh*)
Goal=usrname@address:directory""
;;
echo "Enter the Goal(name@IPname):"
read Goal
echo "Enter the myself Main_Folder:(/home/jiang/)"
read MyD
if [ -z "$MyD" ];then
MyD="/home/jiang/"
fi
echo "Enter PassWord:"
read PassWord
if [ -z "$PassWord" ];then
PassWord="passwd"
fi
;;
*)
;;
esac
for i in $@
do
if echo $i|grep ^/ ;then
way1=$i
else
way1=$MyD$i;
fi
echo Scp $Goal$way1 To Local:$PWD
/home/jiang/bin/Auto/AutoScp $Goal$way1 $PWD $PassWord
done
echo All Has Been Copied!
6.
ln -s /home/jiang/bin/SelfGet /home/jiang/bin/farmget
总结
当然可以将密码方到farmget的$@中,但是对我的工作安全用不着.
自动SCP脚本
最新推荐文章于 2024-08-03 09:33:25 发布