1.使用linux shell脚本自动登录
#!/bin/bash
ftp -n <<!
open 10.0.2.90 远程主机IP
user smart redhat 用户及密码
binary 二进制传输
prompt Interactive mode off
#put $i 可以用位置参数指定
#cat b | mput 可以将需要上传的文件写到文件中,且文件名要在一行
mput *.sh 批量上传
close
bye
!
2.使用expect工具自动登录
Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。系统管理员可以创建脚本来实现对命令或程序提供输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。 Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。
/usr/bin/expect -f
spawn ftp [lindex $argv 0] spawn命令来启动脚本和命令的会话,这里启动ftp命令
# set ip [lindex $argv 0] 接受第一个参数,并设置ip。后面的user和passwd也可以设变量
# spawn ftp $ip 使用变量 ftp连接
expect "Name (*):"
send "[lindex $argv 1]\r"
expect "Password:*"
send "[lindex $argv 2]\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "hash\r"
expect "ftp>"
send "put [lindex $argv 3]\r"
expect "complete."
send "exit\r"
3.实现ssh自动登录
#!/usr/bin/expect -f
set ip [lindex $argv 0] 设置变量
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set timeout 20 设置超时时间
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "#"
send "ls\r"
interact 交互模式,用户会停留在远程主机上面,此时timeout限制失效。如果是
非交互模式,则远程登录上去不能执行命令
#send "exit\r"
expect eof 当不上交换模式时,如果没有此项,上面的ls命令就不会被执行
转载于:https://blog.51cto.com/smartest/1331820