欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

自己亲自做的 expect 判断 sftp 登录连接是否正常 有大用 有大大用 有大大大用

./sftp_login_80.131.sh  第一版

[root@localhost ~]# vim ./sftp_login_80.131.sh    
#!/bin/bash    
#    
/usr/bin/expect <<EOF    
set timeout 4    
spawn /bin/sftp  -P 22  www@192.168.80.131    
   
expect {    
  "yes/no" { send "yes\r"; exp_continue}    
  "password:" { send "123456789\r"}    
}    
expect {    
    "sftp>" { send "bye\r" }    
}    
expect {    
  #"password:" {  send "\003";expect eof;}    
  "password:" {expect eof }    
   expect_end        
}    
EOF    
echo $?    


./sftp_login_80.131.sh  第二版

#!/bin/bash
#
username=$(/usr/bin/osascript -e 'display dialog "请输入帐号" default answer "" with icon note buttons { "Continue"} default button "Continue"
    set result1 to result
    set the text_data to text returned of the result
    set the button_data to button returned of the result1
    if button_data = "Cancel" then
      return ""
    end if
    return text_data')

password=$(/usr/bin/osascript -e 'display dialog "请输入密码" default answer "" with icon note buttons { "Continue"} default button "Continue"
    set result1 to result
    set the text_data to text returned of the result
    set the button_data to button returned of the result1
    if button_data = "Cancel" then
      return ""
    end if
    return text_data')

/usr/bin/expect   <<EOF
set timeout 4
spawn /usr/bin/sftp  -P 22  $username@192.168.80.130

expect {
  "yes/no" { send "yes\r"; exp_continue}
  "password:" { send "$password\r"}
}
expect {
    "sftp>" { send "bye\r" ;expect eof;exit 0}
}
expect {
  #"password:" {  send "\003";expect eof;}   #在Expect脚本中,\003代表Ctrl-C字符,它可以用于中断正在运行的命令。如果你想在Expect脚本中发送Ctrl-C字符,你可以使用send命令和\003字符来实现。
  "password:" {expect eof ; exit 1}
}
EOF
result=$?
if [ $result = 0 ];then
  (/usr/bin/osascript -e  'display dialog "1)用户名密码正确\n2)密码在有效期内\n3)客户端ip地址已绑定到FTP服务器" buttons {"OK"}  default button "OK" ')
else
(/usr/bin/osascript -e  'display dialog "用户名密码不对\n或者密码过期\n或者客户端ip地址未绑定到FTP服务器\n请联系洪守业或史平忠" buttons {"OK"}  default button "OK" ')
fi






sftp_login_2.134.sh  第三版  


#!/bin/bash
#
host=192.168.0.3
port=22

local_ip=`ifconfig | grep -F 10. | cut -f2 -d' '`
local_ip="本机ip ${local_ip}"
echo $local_ip

ping -c 1 -W 2000 192.168.0.3
if [ $? != 0 ];then
  echo "没有网络,请检查"
  (/usr/bin/osascript -e  'display dialog "没有网络,请检查" buttons {"OK"}  default button "OK" ')
  exit 0
fi

nc -zvv $host $port
if [ $? != 0 ];then
  echo "FTP服务器网络不通,请联系洪守业或史平忠"
  (/usr/bin/osascript -e "set local_ip to  \"$local_ip\" "  -e  'display dialog local_ip & "\nFTP服务器网络不通,请联系洪守业或史平忠" buttons {"OK"}  default button "OK" ')
  exit 0
fi

username=$(/usr/bin/osascript -e 'display dialog "请输入帐号" default answer "" with icon note buttons { "Continue"} default button "Continue"
    set result1 to result
    set the text_data to text returned of the result
    set the button_data to button returned of the result1
    if button_data = "Cancel" then
      return ""
    end if
    return text_data')

password=$(/usr/bin/osascript -e 'display dialog "请输入密码" default answer "" with icon note buttons { "Continue"} default button "Continue"
    set result1 to result
    set the text_data to text returned of the result
    set the button_data to button returned of the result1
    if button_data = "Cancel" then
      return ""
    end if
    return text_data')

/usr/bin/expect   <<EOF
set timeout 4
spawn /usr/bin/sftp  -P $port $username@$host

expect {
  "yes/no" { send "yes\r"; exp_continue}
  "password:" { send "$password\r"}
}
expect {
    "sftp>" { send "bye\r" ;expect eof;exit 0}
}
expect {
  #"password:" {  send "\003";expect eof;}
  "password:" {expect eof ; exit 1}
}
EOF
result=$?
if [ $result = 0 ];then
  (/usr/bin/osascript  -e "set local_ip to  \"$local_ip\" "  -e  'display dialog local_ip & "\n1)用户名密码正确\n2)密码在有效期内\n3)客户端ip地址已绑定到FTP服务器" buttons {"OK"}  default button "OK" ')
else
  (/usr/bin/osascript  -e "set local_ip to  \"$local_ip\" "  -e  'display dialog local_ip & "\n用户名密码不对\n或者密码过期\n或者客户端ip地址未绑定到FTP服务器\n请联系洪守业或史平忠" buttons {"OK"}  default button "OK" ')
fi




普通分类: