0

I need a shell script to get a public password for VPN from a site (which refreshes the password everyday more or less). The password is a HTML table, in a specific line of the HTML code of the web page. Once that I've retrieved the password (a word made of 5 characters) I'd like to put it at the end of a simple text file. I'd need a script like this to automatically update the password in my OpenWrt-based router's OpenVPN client.

This is the webpage I'm talking about, and this is line number 265, where the password is (there are two instances of the password, doesn't matter which one the script chooses:

<td>1<td>in1.vpnjantit.com<td>53,992,1194,25000<td><a href='http://www.vpnjantit.com/assets/in1.vpnjantit.com.zip'>in1.vpnjantit.com.zip</a><td>vpnjantit.com<td>x3bu7<td>2018-03-31 at 22:00<tr><tr><td>2<td>in2.vpnjantit.com<td>53,443,1194,25000<td><a href='http://www.vpnjantit.com/assets/in2.vpnjantit.com.zip'>in2.vpnjantit.com.zip</a><td>vpnjantit.com<td>x3bu7<td>2018-03-31 at 22:00<tr></table></div>

The file where I want to put the password it will be very simple:

vpnjantit.com
passwd

The first line is the username, and it will always be the same: "vpnjantit.com". The second line is the 5 characters password. I'd need that the script first deletes the second line of the file, and then it puts the password from the html file on the second line (replace the old password with the new one).

I looked around, and tried to do something with a sequency of awk, curl, cat and other commands, but I wasn't able to get the desired result. Really have no idea about how to realize this.

Thank you a lot in advance for any advice!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

I've used nokogiri, though there are other tools.

echo vpnjantit.com > file.txt # first line
curl http://www.vpnjantit.com/free-openvpn-india.html | nokogiri -e 'puts $_.at_css("table > tr > td:nth-child(6)").text >> file.txt # second line

This would replace the file outright (delete it and create a new one).

Please note that this could break anytime with even minor format changes.

sneep
  • 1,828
  • 14
  • 19
  • Now it works perfectly, thank you! There's only a problem: do you think it's possible to make nokogiri working on a OpenWrt machine? – Francesco Bertamini Apr 03 '18 at 15:22
  • Ah. Hmm, maybe you'd have to build nokogiri yourself in that case. There's a lot of software that you can use instead of nokogiri though, have a look at these threads for example: https://stackoverflow.com/questions/7334942/is-there-something-like-a-css-selector-or-xpath-grep and https://superuser.com/questions/528709/command-line-css-selector-tool – sneep Apr 03 '18 at 15:35