1

I'd like to automatically run Firefox on login. Subsequently, I'd like to go to my ISP's page and click on the login button, all done automatically.

I do not need to enter the password, since it is stored in the browser.

I am using Ubuntu 12.04. Is this kind of automation possible?

Jacob Vlijm
  • 83,767
  • 1
  • See the linked duplicate question. What you really want is for GUI to boot up, then login, and Ubuntu will start necessary GUI apps upon your logon into the system. That's how the system works. Command-line apps can use pretty much the same pattern. There's several variations, though. See the linked post – Sergiy Kolodyazhnyy Jun 27 '16 at 02:47
  • But how can I make my bowser redirect to a Site and login automatically? There is only answers about running Programs on system startup – sysadmincrispy Jun 27 '16 at 03:03
  • Well, how to you open a web-browser from command-line ? firefox google.com , ^_^ easy just like that. Use that as a command to be added on startup. – Sergiy Kolodyazhnyy Jun 27 '16 at 03:06
  • Yes. That's very simple but how to make it open a site and provide login details and make login automatically. – sysadmincrispy Jun 27 '16 at 03:07
  • For your own security , I would suggest you avoid automatic authentication. Such data stored in plain text is very bad news for you and good catch for professional hackers – Sergiy Kolodyazhnyy Jun 27 '16 at 03:40
  • You can automatically run an application on startup, make it go to a specific site, even click on a specific position in the window (making the window maximized first), but as @Serg mentioned, automatically logging in is not a good idea however, Or is the password stored in your browser? Please mention. – Jacob Vlijm Jun 27 '16 at 06:44
  • @Serg, not necessarily a dupe of the linked post. OP wants to take it further than just running an app on login. – Jacob Vlijm Jun 27 '16 at 06:48
  • Yes the password and user name is saved in the browser just needs to click in the login and move to further proceedings is it possible @JacobVlijm – sysadmincrispy Jun 27 '16 at 06:52
  • @sysadmincrispy AHA, great. then I need to know from you what is your browser. It would also be useful to have the address (won't need the password), so I can check the situation. I might also ask you the exact location on the page where to click (will probably add instruction on how to once we get there). If we manage, keep in mind that the solution I have in mind is a dirty one; if the page layout chnges, you'd need to redefine where to click... – Jacob Vlijm Jun 27 '16 at 06:56
  • I am using Firefox. the page is actually a local php page. – sysadmincrispy Jun 27 '16 at 07:01
  • @sysadmincrispy the click is just a single click on a specific location? – Jacob Vlijm Jun 27 '16 at 07:09
  • yes.@JacobVlijm – sysadmincrispy Jun 27 '16 at 07:38
  • Hi @sysadmincrispy posted my answer. Please let me know if all is clear. – Jacob Vlijm Jun 27 '16 at 11:27

1 Answers1

3

Scandalously dirty, but perfectly working

...is the script below.

I tested it to run firefox, opening the AsUbuntu site, then automatically press the link to open my profile page. Since your password is stored in your browser, in your case pressing the button is sufficient to log in.

How it works in practice

15 seconds after login (your Ubuntu user account), the script:

  • opens firefox
  • waits for the window to appear
  • moves to the url you defined
  • moves the mouse to the button's coordinates and presses the button

The script

#!/usr/bin/env python
import subprocess
import time

# --- set the link below
url = "http://askubuntu.com"
# --- set the mouseposition to click on below
xmouse = 858; ymouse = 166
# --- don't change anything below

appcommand = ["firefox", url]

def run(cmd):
    subprocess.Popen(cmd)
    time.sleep(0.2)

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

def run_firefox():
    run(appcommand)
    while True:
        time.sleep(1)
        try:
            pid = get(["pgrep", "firefox"])
        except subprocess.CalledProcessError:
            pass
        else:
            time.sleep(0.1)
            w = [l.split()[0] for l in get(["wmctrl", "-lp"]).splitlines() if pid in l][0]
            break
    return w

w = run_firefox()

cmd1 = ["xdotool", "windowsize", w, "100%", "100%"]
cmd2 = ["xdotool", "mousemove", str(xmouse), str(ymouse)]
cmd3 = ["xdotool", "click", "1"]

for cmd in [cmd1, cmd2]:
    run(cmd)
time.sleep(3)
run(cmd3)

How to setup

  • The script needs both wmctrl and xdotool

    sudo apt-get install xdotool wmctrl
    
  • Copy the script into an empty file, safe it as run_login.py

  • Now the trickiest part:

    • open your browser move to the page to log in
    • place the mouse on the button to be pressed (don't press)
    • press Ctrl+T to open a terminal
    • type the command xdotool getmouselocation
    • read the coordinates and set them in the head section of the script:


      enter image description here

      xmouse = 856; ymouse = 165
      
    • set the url of your login page:

      url = "http://askubuntu.com"
      
  • Test- run it (with no ff window open) by the command:

    python /path/to/run_login.py
    
  • If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python /path/to/run_login.py"
    

Important note

Since the button is clicked on the page by its coordinates, it will only work as long as the pagelayout is unchanged. In case of changes, you need to redefine te location, as set in the head of the script.

Jacob Vlijm
  • 83,767
  • Also, if OP wants to type username/password, then they can use xdotool type 'username' key Tab type 'password' –  Jun 27 '16 at 14:35
  • @BharadwajRaju Not sure what the "Also" means (I only see one comment) but how could you possibly suggest to have xdotool print the password? That implies storing the password somewhere in plain text. As dangrerous as it gets! – Jacob Vlijm Jun 27 '16 at 15:09
  • Well, it is a security risk, but can be worked around: Store password encrypted, xdotool type $(command_to_decrypt) –  Jun 27 '16 at 15:20
  • @BharadwajRaju anyway, OP mentions it is just a button to push in this case :) – Jacob Vlijm Jun 27 '16 at 15:26
  • @JacobVlijm thanks a lot it worked big time. after login i want to make 2-3 clicks inside that site automatically is it possible. I don't know the coding of python. Is it possible. – sysadmincrispy Jun 28 '16 at 04:02
  • These are the (x,y) co-ordinate positions i want u click after login (689,143) (714,173) (367,290) (714,323) (683,213). – sysadmincrispy Jun 28 '16 at 04:24
  • Thanks . No need sir i got the rest it' working big time. – sysadmincrispy Jun 28 '16 at 05:29
  • @JacobVlijm can you suggest me some good python tutorials – sysadmincrispy Jun 28 '16 at 07:01
  • 1
    @sysadmincrispy there are of course tutorials like this: http://www.tutorialspoint.com/python/, but most of these tutorials start with an extensive textual explanation. Horrible. I started with a shockingly easy tutorial, but unfortunately I cannot find it anymore. The best way (for me) was to set a simple goal for a script and extensively use SO to find out how to achieve that. Once the first start is done, all becomes easyer. Note by the way that the script was written in python, but could as well be in bash; many of the commands are system calls, executed via subprocess. – Jacob Vlijm Jun 28 '16 at 07:12
  • 1
    ...Python however has the advantage of being easy to quickly produce solutions and, imo, its readability. – Jacob Vlijm Jun 28 '16 at 07:15
  • 1
    @sysadmincrispy The one I learned from was: Learn Python the Hard Way (for Python 2) and some say Dive Into Python (for Python 3) is good too. –  Jun 28 '16 at 08:43