Skip to content

Instantly share code, notes, and snippets.

@satokjp
Created March 28, 2014 06:46
Show Gist options
  • Save satokjp/9826735 to your computer and use it in GitHub Desktop.
Save satokjp/9826735 to your computer and use it in GitHub Desktop.
expect ssh sample script
#!/usr/bin/expect --
# for cygwin64 Windows
#
# timeout sec
#set timeout 10
set timeout -1
# HOST & ID
set HOST "[email protected]"
set PASSWD "common-pas"
if { $argc == 0 } {
puts "comannd line error"
exit
}
puts $argv
set list { 1 2 3 }
foreach elm $list {
puts $elm
}
# FILE output
set out_file [open ex_out.txt w]
puts $out_file "1 Line-"
puts $out_file "2 Line-"
flush $out_file
# FILE input
set in_file [open ex_in.txt r]
puts "start --------"
# FILE IN
while {[gets $in_file line] !=-1} {
puts $line
puts $out_file $line
}
sleep 2
spawn ssh $HOST
sleep 1
expect "(yes/no)?" {
send "yes\n"
expect "password: "
send "$PASSWD\n"
} "password:" {
send "$PASSWD\n"
}
#expect "password:"
#send "$PASSWD\r"
log_file -noappend aa.log
expect "]\\$ "
send "ls -l\r"
expect "]\\$ "
send "w\r"
expect "]\\$ "
send "uname \r"
expect -re "\r\n(.*)\r\n"
set moji $expect_out(1,string)
expect "]\\$ "
send "cat /proc/version \r"
expect -re "\r\n(.*)\r\n"
set moji1 $expect_out(1,string)
expect "]\\$ "
send "bye\r"
puts ""
set res "res: '"
append res $moji
append res "' '"
append res $moji1
append res "'"
puts $res
# FILE OUT
puts $out_file $res
puts $out_file "last line ----------------"
flush $out_file
close $out_file
close $in_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment