1:use echo -e
example:
echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+1024M\nw\n"|fdisk /dev/vdb
\n代表回车
[root@server7 ~]# fdisk -l /dev/vdb
Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+1024M\nw\n"|fdisk /dev/vdb &> /dev/null
[root@server7 ~]# fdisk -l /dev/vdb
Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x99657b78
Device Boot Start End Blocks Id System
/dev/vdb1 1 204 102784+ 83 Linux
/dev/vdb2 205 2286 1049328 83 Linux
[root@server7 ~]#
ok
2.expect:
notice: you should use “yum install expect” at begining。
1 #!/usr/bin/expect 2 spawn fdisk /dev/vdb 3 expect Command 4 send "n\r" 5 send "p\r" 6 send "1\r" 7 send "\r" 8 send "+100M\r" 9 expect "Command" 10 11 12 send "n\r" 13 send "p\r" 14 send "2\r" 15 send "\r" 16 send "+1G\r" 17 send "w\r" 18 expect eof
两者异曲同工!~~~~~~~~~~~~
选择自己喜欢的把 对了,在bash里面调用expect的话格式如下
/usr/bin/expect <<-EOF
具体内容~~~~~
EOF
是不是很方便呢 ~~
这样我们再来一个例子:使用lftp下载一个文档:
[root@server7 ~]# lftp 172.25.254.250 lftp 172.25.254.250:~> cd pub/linux/pub/docs/lftp 172.25.254.250:/pub/linux/pub/docs> ls vi.png lftp 172.25.254.250:/pub/linux/pub/docs> ls vi.png-rwxrwxrwx 1 0 0 310741 Dec 25 2013 vi.png lftp 172.25.254.250:/pub/linux/pub/docs> get vi.png
ok,这是我们常规的操作,此时我们首先使用方法一也就 echo -e
example:
[root@server7 ~]# rm vi.png rm: remove regular file `vi.png'? yes
先删掉
[root@server7 ~]# echo -e "cd pub/linux/pub/docs/\nls vi.png\nget vi.png\n"|lftp 172.25.254.250 -rwxrwxrwx 1 0 0 310741 Dec 25 2013 vi.png [root@server7 ~]# ls vi.png vi.png
ok 就这么easy
下来我们bash里调用expect 再来解决
1 ######################################################################### 2 # File Name: get.sh 3 # Author: dty 4 # mail: [email protected] 5 # Created Time: Fri 14 Aug 2015 03:15:55 PM CST 6 ######################################################################### 7 #!/bin/bash 8 /usr/bin/expect <<-EOF 9 spawn lftp 172.25.254.250 10 expect lftp 11 send "cd pub/linux/pub/docs\r" 12 sleep 1 13 send "ls vi.png\r" 14 send "get vi.png\r" 15 send "quit\r" 16 expect eof 17 EOF
~
[root@server7 ~]# ./get.sh spawn lftp 172.25.254.250 lftp 172.25.254.250:~> cd pub/linux/pub/docs cd ok, cwd=/pub/linux/pub/docs lftp 172.25.254.250:/pub/linux/pub/docs> ls vi.png -rwxrwxrwx 1 0 0 310741 Dec 25 2013 vi.png lftp 172.25.254.250:/pub/linux/pub/docs> get vi.png 310741 bytes transferred lftp 172.25.254.250:/pub/linux/pub/docs> quit [root@server7 ~]#
okok顺利完成
很简单吧 get起来 感谢开源 -单纯酱