linux expect自动登录ssh,ftp

linux expect自动登录ssh,ftp

expect是一种能够按照脚本内容里面设定的方式与交互式程序进行“会话”的程序。根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及 什么是正确的应答。它是一种可以提供“分支和嵌套结构”来引导程序流程的解释型脚本语言

shell功能很强大,但是不能实现有交互功能的多机器之前的操作,例如sshftp.expect可以帮助我们来实现.

,安装expect

1 yum install expect  

其实expectbash形势上差不多的.

,实例

1,ssh实现自动登录,并停在登录服务器上

1 #!/usr/bin/expect -f  

2 set ip [lindex $argv 0 ]     //接收第一个参数,并设置IP

3 set password [lindex $argv 1 ]   //接收第二个参数,并设置密码

4 set timeout 10                   //设置超时时间

5 spawn ssh root@$ip       //发送ssh请�E

6 expect {                 //返回信息匹配

7 "*yes/no" { send "yes\r"; exp_continue}  //第一次ssh连接会提示yes/no,继续

8 "*password:" { send "$password\r" }      //出现密码提示,发送密码

9 }  

10 interact          //交互模式,用户会停留在远程服务器上面.

运行结果如下:

11 root@ubuntu:/home/zhangy# ./test.exp 192.168.1.130 admin  

12 spawn ssh [email protected]  

13 Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142  

14 [root@linux ~]#  

这个例子有统一的接口,根据IP和密码可以连接到不同的机器.如果你嫌输入IP和密码麻烦,看下面的例子

15 #!/usr/bin/expect -f  

16 set ip 192.168.1.130  

17 set password admin  

18 set timeout 10  

19 spawn ssh root@$ip  

20 expect {  

21 "*yes/no" { send "yes\r"; exp_continue}  

22 "*password:" { send "$password\r" }  

23 }  

24 interact  

运行结果如下:

25 root@ubuntu:/home/zhangy# ./web.exp  

26 spawn ssh [email protected]  

27 Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142  

28 [root@linux ~]#  

2,ssh远程登录到服务器,并且执行命令,执行完后并退出

29 #!/usr/bin/expect -f  

30 set ip 192.168.1.130  

31 set password admin  

32 set timeout 10  

33 spawn ssh root@$ip  

34 expect {  

35 "*yes/no" { send "yes\r"; exp_continue}  

36 "*password:" { send "$password\r" }  

37 }  

38 expect "#*"

39 send "pwd\r"

40 send  "exit\r"

41 expect eof  

运行结果如下:

42 root@ubuntu:/home/zhangy# ./test3.exp  

43 spawn ssh [email protected]  

44 [email protected]'s password:  

45 Last login: Fri Sep  7 14:05:07 2012 from 116.246.27.90  

46 [root@localhost ~]# pwd  

47 /root  

48 [root@localhost ~]# exit  

49 logout  

50 Connection to 192.168.1.130 closed.  

3,远程登录到ftp,并且下载文件

51 #!/usr/bin/expect -f  

52 set ip [lindex $argv 0 ]  

53 set dir [lindex $argv 1 ]  

54 set file [lindex $argv 2 ]  

55 set timeout 10  

56 spawn ftp $ip  

57 expect "Name*"

58 send "zwh\r"

59 expect "Password:*"

60 send "zwh\r"

61 expect "ftp>*"

62 send "lcd $dir\r"

63 expect {  

64 "*file"  { send_user "local $_dir No such file or directory";send "quit\r" }  

65 "*now*"  { send "get $dir/$file $dir/$file\r"}  

66 }  

67 expect {  

68 "*Failed" { send_user "remote $file No such file";send "quit\r" }  

69 "*OK"     { send_user "$file has been download\r";send "quit\r"}  

70 }  

71 expect eof  

运行结果如下:

72 root@ubuntu:/home/zhangy# ./test2.exp 192.168.1.130 /var/www/www aaa.html  

73 spawn ftp 192.168.1.130  

74 Connected to 192.168.1.130.  

75 220 (vsFTPd 2.0.5)  

76 Name (192.168.1.130:root): zwh  

77 331 Please specify the password.  

78 Password:  

79 230 Login successful.  

80 Remote system type is UNIX.  

81 Using binary mode to transfer files.  

82 ftp> lcd /var/www/www  

83 Local directory now /var/www/www  

84 ftp> get /var/www/www/aaa.html /var/www/www/aaa.html  

85 local: /var/www/www/aaa.html remote: /var/www/www/aaa.html  

86 200 PORT command successful. Consider using PASV.  

87 150 Opening BINARY mode data connection for /var/www/www/aaa.html (66 bytes).  

88 226 File send OK.  

89 66 bytes received in 0.00 secs (515.6 kB/s)  

90 quit aaa.html has been download  

91 221 Goodbye.  

原文地址 http://blog.51yip.com/linux/1462.html

你可能感兴趣的:(inux,ftp,expect自动登录ssh)