![如何使用 Shell 脚本登录 ISP 登录页面?](https://linux22.com/image/1134671/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Shell%20%E8%84%9A%E6%9C%AC%E7%99%BB%E5%BD%95%20ISP%20%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%EF%BC%9F.png)
我的 ISP 会随机将我注销,然后我的连接就会丢失。所以我想编写一个脚本,在注销时自动登录,无需用户交互(我可以使用 ping 退出状态进行测试)。但问题是我对 Web 编程一无所知。我试过 curl,但搞不清楚。试过 elinks,但它不能作为脚本或没有用户交互的 cronjob 工作
我到目前为止已经尝试过
cat ~/bin/elink
elinks -auto-submit http://192.168.1.2:9088/WISHN/Login.jsp
通过运行此命令,它会自动登录(通过编辑 ~/.elinks/elinks.conf)
但它只是停留在那里,直到用户交互并退出应用程序,但按下“问”
这就是这个脚本不起作用的原因
#!/bin/bash
while [ 1 -eq 1 ]
do
. ~/bin/elink
slleep 3
pkill-9 elinks
sleep 5
done
下一个命令直到 elinks 退出后才会运行
timeout 1m elinks -auto-submit http://192.168.1.2:9088/WISHN/Login.jsp
这也不起作用什么也没有发生
我尝试使用 curl 但像往常一样不起作用
curl -d "Username=someuser&Password=shhhitsasecret" -s http://192.168.1.2:9088/WISHN/loginUI.do2
和这个
curl -d "Username=someuser&Password=shhhitsasecret" -s http://192.168.1.2:9088/WISHN/Login.jsp
我发现登录UI.do2 在 行动部分源代码,但它也失败了
我几乎没有任何想法,任何帮助都将不胜感激
这是页面的源代码,我删除了一些可能导致隐私侵犯的部分
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>somewebsite</title>
<link href="templatemo_style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="stylesheet/styles.css" />
<link rel="stylesheet" type="text/css" href="stylesheet/style_pop.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
<!--
<script language="javascript" type="text/javascript">
function clearText(field)
{
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>-->
<script language="JavaScript">
function newCookie(Username,value,days) {
var days = 30; // the number at the left reflects the number of days for the cookie to last
// modify it according to your needs
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString(); }
else var expires = "";
document.cookie = Username+"="+value+expires+"; path=/"; }
function readCookie(Username) {
var nameSG = Username + "=";
var nuller = '';
if (document.cookie.indexOf(nameSG) == -1)
return nuller;
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
return null; }
function eraseCookie(Username) {
newCookie(Username,"",1); }
function toMem(a) {
newCookie('theName', document.login.Username.value); // add a new cookie as shown at left for every
newCookie('theEmail', document.login.Password.value); // field you wish to have the script remember
}
function delMem(a) {
eraseCookie('theName'); // make sure to add the eraseCookie function for every field
eraseCookie('theEmail');
document.login.Username.value = ''; // add a line for every field
document.login.Password.value = ''; }
function remCookie() {
document.login.Username.value = readCookie("theName");
document.login.Password.value = readCookie("theEmail");
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
remCookie();
});
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
if ((version >= 5.5) && (document.body.filters))
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
}
}
}
}
/* ------- Trim ------- */
function trim(str) {
return ltrim(rtrim(str));
}
function ltrim(str) {
return str.replace(new RegExp("^[\\s]+", "g"), "");
}
function rtrim(str) {
return str.replace(new RegExp("[\\s]+$", "g"), "");
}
/* ------- End of Trim ------- */
function validateForm(){
var input=true;
document.login.Username.value = trim(document.login.Username.value);
if(document.login.Username.value.length==0){
alert("User Name is required.");
document.login.Username.focus();
input=false;
return false;
}
if(document.login.Password.value.length==0){
alert("Password is required.");
document.login.Password.focus();
input=false;
return false;
}
var uname = "?"+document.login.Username.value+"+/@";
var pwd = "?"+document.login.Password.value+"+/@";
document.login.LoginName.value=encodeURIComponent(uname);
document.login.LoginPassword.value=encodeURIComponent(pwd);
if (input==true&&document.login.checker.checked)
toMem(this);
}
window.attachEvent("onload", correctPNG);
</script>
</head>
<!--Main Container Starts here-->
<form name="login" method="post" action="loginUI.do2" onSubmit="if (this.checker.checked) toMem(this)" id="login-form">
<body>
<div id="templatemo_wrapper">
<div id="templatemo_menu">
<marquee scrollamount="3" behavior="slide" direction="left" height="20" width="100%" style="color:#00162b"><p> Your Default Password is <strong style="color:#FFFFFF">wnpl123</strong> for first time login only. Please change your password after first login.</p></marquee>
</div> <!-- end of templatemo_menu -->
<div id="templatemo_header_bar">
<div id="header"><!--<div class="right"></div>-->
<h1><a href="#">
<img src="images/templatemo_logo.png" alt="Site Title" width="254" />
<span> </span>
</a></h1>
</div>
<div style=" font-size:22px; color:#000; margin-top:50px; float:right; font-family:Arial, Helvetica, sans-serif; left: 878px; top: 0px; width: 285px;">
Toll Free : 1800 419 4244 </div>
</div> <!-- end of templatemo_header_bar -->
<div class="cleaner"></div>
<div id="sidebar"><div class="sidebar_top"></div><div class="sidebar_bottom"></div>
<div class="sidebar_section">
<h2>Login Form</h2>
<div style="color:#FFFF00; font-size:12px; margin-top:45px; margin-bottom:10px;"><b>
</b></div>
<label>Username</label>
<input type="text" name="Username" size="20" class="input_field" />
<label>Password</label>
<input type="password" value="" name="Password" class="input_field" title="password" />
<input type="hidden" name="LoginName" />
<input type="hidden" name="LoginPassword" />
<label for="checkbox"><br><input type="checkbox" name="checker" id="checker" /> Remember Me</label>
<br />
<input type="submit" value="Login" id="submit_btn" onClick="return validateForm()" />
</div>
</div> <!-- end of sidebar -->
<div id="templatmeo_content">
<div id="latest_product_gallery">
<img src="images/bannerr.png" />
<a href="index1.html" target="_blank"> <div class="intro-texttt" align="right"></div>
</a>
</div> <!-- end of latest_content_gallery -->
</div>
Z<!-- end of templatmeo_content -->
</div> <!-- end of templatemo_wrapper -->
<!--<div class="popupbox3" id="popuprel3">
<div id="intabdiv3" align="left">
<table border="0" align="left">
<tr height="16px"><td></td></tr>
<tr height="16px"><td></td></tr>
<tr><td><h2> Steps to Install:-</h2> </td><tr>
<tr><td><Font color="#000000"><strong>1.</strong> <a href="http://172.18.1.60/gamestore/UrbanTerror.rar" style="color:#0000CC">Download</a> UrbanTerror -> Extract UrbanTerror.rar-></font></td></tr><tr><td><Font color="#000000" > Install UrbanTerror.exe</font></td></tr><tr height="14px"><td></td></tr>
<tr><td><Font color="#000000">
<strong> 2.</strong><b>Play :</b><i>Online </i>|<b>Source :</b><i>Favorites </i>| <b> Type : </b><i>On/All</i></font> </td></tr>
<tr><td><Font color="#000000" >
<b>Create Favourite Name : </b><i>WISH-UT </i></font></td></tr>
<tr><td><Font color="#000000" >
<b> IP Address :</b><i>172.18.1.60 |</i>
<b> Guides: </b><i>On/All</i></font></td></tr>
<tr><td><Font color="#000000">
<b>OK & CONNECT</b></font></td></tr>
</table>
</div>
</div>
-->
<div id="templatemo_footer_wrapper">
<div id="templatemo_footer">
Copyright © 2099 <a href="#">somewebsite</a> |
Designed by <a href="http://www.templatemo.com" target="_parent">Wish Net</a>
</div> <!-- end of footer -->
</div> <!-- end of footer_wrapper -->
<!--<div id="fade"></div>-->
<!--</form>
<script type="text/javascript"> Cufon.now(); </script>-->
</body>
</html>
答案1
由于没有人回答,所以我找到了一种非常基本但可以完成工作的方法。因为我无法弄清楚 curl,所以我找到了一种使用 elinks 的方法,即制作一个脚本并获取它(而不是使其可执行,因为它在后台 shell 中执行 elinks 命令,但它不起作用。
#!/bin/bash
arg1="set document.browse.forms.show_formhist = 1"
arg2="set document.browse.forms.confirm_submit = 0"
count=0
terminal=`tty`
dpkg -s zenity > /dev/null 2>&1
if ! [ $? -eq 0 ]
then
echo "\033[1mzenity is not installed do you want to install y/n \033[0m\c: "
read choice
if [ "$choice" = "y" -o "$choice" = "Y" ]
then
sudo apt-get install zenity
else
echo "OK Sorry For Asking Jeez"
sleep 3
exit 100
fi
if ! [ $? -eq 0 ]
then
echo "something went wrong with apt-get so exiting byebye"
sleep 3
exit 200
fi
fi
dpkg -s elinks > /dev/null 2>&1
if ! [ $? -eq 0 ]
then
echo "elinks is not installed you will be prompted for installation in a sec"
sleep 3
zenity --question --title="Query" --text="Would you Like to Install Elinks ?"
if [ $? -eq 0 ]
then
sudo apt-get install elinks
else
zenity --error --title="Hmmmmm" --text="OK Sorry For Asking Jeez"
exit 100
fi
if ! [ $? -eq 0 ]
then
echo "something went wrong with apt-get so exiting byebye"
sleep 3
exit 200
fi
fi
zenity --question --title="Query" --text="Do you wanna login to isp page now using elinks to save password if you havent already ?"
if [ $? -eq 0 ]
then
zenity --info --title="Soooooo" --text="elink will open enter username and password save them so we can do autologin later elink will autoexit in 20 seconds so hurry up"
timeout 20s elinks http://192.168.1.2:9088/WISHN/Login.jsp
fi
newf () {
while true
do
echo -e "\033[42m Script For Wishnet AutoLogin By Shantanu Running......\033[0m"
sleep 10m
timeout 3s wget -O /dev/null http://cachefly.cachefly.net/100mb.test > /dev/null 2>&1
condition=$?
result=8
case $condition in
$result)
echo "were logged back in"
timeout 5s elinks -auto-submit http://192.168.1.2:9088/WISHN/Login.jsp
;;
*)
echo "nothing is wrong"
;;
esac
done
}
if ! [ -e ~/.elinks/elinks.conf ]
then
touch ~/.elinks/elinks.conf
fi
exec < ~/.elinks/elinks.conf
while read -r line
do
if [ "$arg1" = "$line" -o "$arg2" = "$line" ]
then
let count+=1
fi
done
exec < "$terminal"
if [ $count -eq 2 ]
then
newf
else
echo $arg1 > ~/.elinks/elinks.conf
echo $arg2 >> ~/.elinks/elinks.conf
newf
fi