2011年10月4日 星期二

[Linux] Telnet tftpput ssh - expect操作

首先先安裝EXPECT
sudo apt-get install expect

Login telnet : #!/usr/bin/expect spawn telnet 192.168.1.1 expect Login: send root\r expect Password: send password\r interact tftpput : #!/usr/bin/expect spawn tftp 192.168.1.1 expect tftp> send bin\r expect tftp> send "put $argv\r" expect tftp> send quit\r interact 這樣使用 tftpput FILENAME SSH LOGIN : #!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lindex $argv 0] set ipaddr [lindex $argv 1] set scriptname [lindex $argv 2] set arg1 [lindex $argv 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@$ipaddr $scriptname $arg1 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" #expect eof expect "#"

這樣使用 ./ssh.sh password ipaddr
解釋: expect "#": 結束expect 返回.
interact : 最後加這一行, 把控制權交給控制台,這個時候就可以手動操作了.
proc Login {username server password} { set prompt "(%|>|\#|\\\$) $" spawn /usr/bin/ssh $username@$server expect { -re "Are you sure you want to continue connecting (yes/no)?" { exp_send "yes\r" exp_continue #continue to match statements within this expect {} } -nocase "password: " { exp_send "$password\r" interact } } }
#!/usr/bin/expect set timeout 1 set cmd {uname -a} spawn ssh root@$argv expect_after eof { exit 0 } ## interact with SSH expect { "(yes/no)?" {send -- "yes\r"; exp_continue} "password:" {send -- "123456\r"} } expect "# " send "$cmd\r" expect "$cmd\r" expect "(.*)\r" send "exit\r
參考網站:
不少範例
http://linux.sheup.com/linux/linux5440.htm
http://hi.baidu.com/%B7%D7%E7%A1/blog/item/fcb146fb66b95068024f567b.html
http://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/
http://www.thegeekstuff.com/2010/10/expect-examples/
Help Expect

【下列文章您可能也有興趣】

沒有留言: