一、遠程登錄指令實現
1、在home下面創建文件夾my_sh,把腳本都放在這個文件夾下
mkdir my_sh
cd my_sh
2、編寫遠程登錄的expect腳本
vi my_ssh.sh
#!/usr/bin/expect
set ip [lindex $argv 0]
set pw [lindex $argv 1]
#spawn ssh -p $ip xxx@xxx
spawn ssh xxx@$ip
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$pw\r"}
"*refused" {send "exit\r"}
}
interact
這個腳本需要獲取兩個參數,一個是ip地址,一個是password
3、編寫調用上述腳本my_ssh.sh的bash腳本
vi ssh.sh
#!/bin/bash
A=$1;
if [ ! -n "$1" ];
then {
echo "no portnum"
exit
}
fi
#B=c${A#*160}
B=$2
~/my_sh/my_ssh.sh $A $B
這個腳本需要需要傳入兩個參數,一個是ip地址,一個是password,並且調用另外一個腳本
4、修改~/.bashrc文件,添加指令my_ssh,以便任何情況下打開終端都能夠使用這個指令
vi ~/.bashrc
在最後添加
alias my_ssh='~/my_sh/ssh.sh'
二、遠程拷貝指令和上面流程完全一樣,代碼如下
my_scp.sh
#!/usr/bin/expect
set ip [lindex $argv 0]
set pwd [lindex $argv 1]
set sou_dir [lindex $argv 2]
set dis_dir [lindex $argv 3]
#spawn scp -P $ip xxx@xxx:$sou_dir $dis_dir
spawn scp xxx@$ip:$sou_dir $dis_dir
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$pwd\r"}
"*refused" {send "exit\r"}
}
interact
scp.sh
#!/bin/bash
A=$1
if [ ! -n "$1" ]
then {
echo "no portnum"
exit
}
fi
B=c${A#*160}
C=$2
if [ ! -n "$2" ]
then
{
echo "no source dir"
exit
}
fi
D=$3
if [ ! -n "$3" ]
then
{
echo "no dist dir, default current dir"
D="./"
}
fi
~/my_sh/my_scp.sh $A $B $C $D