LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 07-16-2008, 07:52 AM   #1
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Rep: Reputation: 0
Check if program is running, if not start it and enter a password... easy, isn't it?


Hello,
I just installed socks5-rev26 to my xubuntu.
In order to start it, i need to execute those commands:

Code:
cd socks5-rev26/bin
./socks5 socks5.conf
"type a password"
I wish instead that this is done automatically after boot, and that once a while it checks if socks5 is running, else start it.
I probably can use something like http://www.anyexample.com/linux_bsd/...ell_script.xml that to create a script that check if the program is still running, and add it to crontab. But once i found it's not running anymore, how can i start it and let it enter the password?
Any example would be really appreciated.
 
Old 07-16-2008, 07:57 AM   #2
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
You need to get familiar with expect.
 
Old 07-16-2008, 08:00 AM   #3
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
since i've never done a script, a hint on the code would be very welcome
 
Old 07-16-2008, 08:08 AM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Sounds like a candidate for "expect".

In an expect script you can tell it what the password prompt looks like then have it type the password for you.

For example this expect script is one I use to reboot a fibre bridge here:
Code:
!/usr/local/bin/expect -f

log_file -a expect.out

set ip [lrange $argv 0 0]

spawn telnet $ip

expect "Configuration Login:"
send bridgeuser\r

expect "Password:"
send bridgepassword\r


expect "4) Reboot"
expect "Command >"
send 4

expect "Reboot! Are you sure (y/n)?"
send y
sleep 5
In the above each "expect" line is text I'm waiting to see. Once I see it then I "send" a response. bridgeuser and bridgepassword are the (obfuscated) user and password I would login with.

The argv takes input from command line so I execute above expect script with the IP address at command line so it knows which device to telnet into.

In your case you're not having to telnet. You just need to work out the expected prompt and put that in an expect line then do the send yourpassword\r.

I actually use the above to reboot 10 fibre bridges so have a shell script that calls the above 10 times (once for each IP).

One thing to remember - there are pauses - when you see prompts on the screen you'll be tempted to do input or hit enter - that will interfere with the expect script - just let it do its thing.

Another thing to remember - insure your cron script has the necessary paths to all commands you'll use. The cron job won't use the same environment you do when you test from command line.

Note: The first line is where expect is on one of my servers - it may be elsewhere on yours (you may even need to install expect). Be sure to edit the first line so it has correct location.

Finally: The expect script stores the password in clear text so it is important you set permissions on it so that only the user that runs it and/or root can read the file.
 
Old 07-16-2008, 08:31 AM   #5
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
thanks, so the final script will be something like:

Code:
#!/bin/sh
!/usr/local/bin/expect -f
SERVICE='sock5'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running"
    spawn /home/ciccio/socks5-rev26/bin/./socks5 socks5.conf
    expect "Password:"
    send somepassword\r
    sleep 2
fi
is that correct?

Last edited by darkrad; 07-16-2008 at 08:34 AM.
 
Old 07-16-2008, 08:45 AM   #6
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
No because you've made the above a shell script by making its first line the #!/bin/sh.

What you really need is to make a shell script that calls the expect script. (Alternatively you may be able to embed an expect function within the shell script but I've not done that.)

So shell script would be:
Code:
#!/bin/sh

SERVICE='sock5'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running"
    socks5.expect
fi
socks5.expect script would be:
Code:
#!/usr/local/bin/expect -f
spawn /home/ciccio/socks5-rev26/bin/./socks5 socks5.conf
expect "Password:"
send somepassword\r
sleep 2
Of course as I noted before you need to include paths since you're doing this in cron:

e.g. instead of "ps ax" you need to do "/bin/ps ax"
instead of just "grep" you'd need "/bin/grep"
instead of just socks5.expect you'd need "/pathto/socks5.expect".
 
Old 07-16-2008, 08:58 AM   #7
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Really appreciated! I will try that as soon as i come back home.
and to add it to the crontab, i just need to type "crontab -e" and add "01,16,31,46 * * * * /pathto/shellscript" so that the check is performed every 15 mins? And is there a way to execute it with my user priviledge and not as root? Because when i start it manually, if i run it with my user, it works good, if i run it as root (with sudo ./socks ...) it doesn't work properly.
thanks very much

Last edited by darkrad; 07-16-2008 at 09:06 AM.
 
Old 07-16-2008, 11:09 AM   #8
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Pretty much - make sure you test from command line before adding to cron. On occasion expect requires an extra timeout or carriage return (\r) to work properly.

The crontab entry looks fine to me but I'd add logging to it - the cronlog only has start success/failure - not output from the cron scripts:

01,16,31,46 * * * * /pathto/shellscript >/var/log/shellscript.log 2>&1

To make it run as your user instead of root you just run the crontab -e as yourself instead of as root (or as root add the -u flag to specify user). Any user can run a cron command as themselves. It will automatically create the cron file for your user the first time you add an entry.
 
Old 07-16-2008, 03:36 PM   #9
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
ok i tried to launch the script ./socks5.script after chmodding it +x. it gave me error, so i chmodded +x socks5.expect too. Now it's executed, but doesn't run as expected. The log produced is:

Code:
sock5 is not running
spawn /home/ciccio/socks5-rev26/bin/./socks5 socks5.conf.e
Enter config blowfish key:
and then exit.. i do ps ax | grep socks5 and it's not started..
if i run the socks5 program manually, the expected behaviour is:

Code:
ciccio@ubuntu:~/socks5-rev26/bin$ ./socks5 socks5.conf.e 
Enter config blowfish key: 
using default value '0' for use_ssl
using default value '' for ssl_cert
using default value '0' for no_chroot
[BIND - 2530,Wed Jul 16 22:35:02 2008] [Bind] bind to any adr
[-GETIP- - 2530,Wed Jul 16 22:35:02 2008] try to get ip for: www.xxx.com
[-GETIP- - 2530,Wed Jul 16 22:35:02 2008] resolved ip: xxx.xxxx.xxx.xxx
[-SYSTEM- - 2530,Wed Jul 16 22:35:02 2008]  - WARNING: - Could not chroot
[-SYSTEM- - 2530,Wed Jul 16 22:35:02 2008]  - WARNING: - Could not set uid!
[ACCEPT - 2530,Wed Jul 16 22:35:02 2008] [Accept] start
and then the program waits for socks connections..
What's wrong with the script? I tried to add a pause before sending the password, but nothing changes.. Maybe the script, when finishes, close the started application instead of running it in background?
Thanks
 
Old 07-16-2008, 06:29 PM   #10
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
You're not understanding expect properly. You have to tell it literally what to expect.

In what you wrote in prior post you had:
Code:
expect "Password:"
However in the output it is clear that the socks command never prompts the literal string:
Quote:
Password:
Instead it prompts the literal string:
Quote:
Enter config blowfish key:
That means what you are "expecting" is the above literal string. So your statement should be:
Code:
expect "Enter config blowfish key:"
There may be other things you "expect" to occur before or after that. The key is that the expect statement isn't expecting some general thing - it is expecting specifically what you would see on the screen when you were doing it manually.
 
Old 07-17-2008, 12:58 AM   #11
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
yeah my bad, i didn't say that i changed "password" to "key", but it still doesn't work..
To be more precise, when I run the program through a shell, i can't do anything else since it's not ran in background. so when i close the shell or i lost the connection with my server box, socks5 just terminate. I don't run it with & because i wouldn't be able to insert the password.
That's why i want to crontab it and, sometimes, check if it runs...

Last edited by darkrad; 07-17-2008 at 08:35 AM.
 
Old 07-17-2008, 12:35 PM   #12
darkrad
LQ Newbie
 
Registered: Sep 2005
Posts: 10

Original Poster
Rep: Reputation: 0
ok now i edited like that:

Code:
#!/usr/local/bin/expect -f
spawn /home/ciccio/socks5-rev26/bin/./socks5 /home/ciccio/socks5-rev26/bin/socks5.conf
expect "*key:*"
sleep 2
send -- "somepassword\r"
send -- "\r"
expect eof
and actually it starts... but after like 10 seconds it close it and return to prompt. Why that happen?
 
  


Reply

Tags
crontab, expect, script, socks5


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Check if a program is running naimslim89 Programming 12 05-08-2012 10:24 PM
How to enter a password before running script, with you irda remote controller ? frenchn00b Linux - General 1 11-17-2007 08:06 AM
Check userid/password from a script/program johann_p Programming 6 11-09-2006 05:09 AM
Check if program is running Conjurer Programming 8 11-17-2005 01:37 PM
program to check to make sure a process is running? IceNineJon Linux - Software 7 08-06-2003 02:07 PM


All times are GMT -5. The time now is 06:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration