LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-05-2008, 11:51 PM   #1
trollkotze
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Rep: Reputation: 0
C program for automatic ssh login


Hi!
I am living in a student's hostel. In order to get Internet access here I must login to an ssh server (rather uncommon method, I guess). I then have access to the Internet as long as the ssh connection is alive. Internet access is not tunneled through ssh though. The ssh connection is only for authenticiation and serves no other purpose.
My problem now is that I find it rather unconvenient to login manually everytime I turn on the computer and leave one terminal occupied by the ssh client all the time.
The login process looks like this:
Code:
$ ssh -l 121581 139.18.143.253
... blah blah blah... password:
At this point I must enter my password and then I'm online.

I would like to automate the login process, e.g. write a C program or a bash script that I can put into the rc.local so that it does the login for me at startup without asking for my password.
I tried it this way:
Code:
$ ssh -l 121581 139.18.143.253 < sshpass
where sshpass was an ascii file where I saved my password. But piping didn't seem to work. I still had to enter it manually.

Why does piping not work here? I also tried it this weird way:
Code:
$ mkfifo fifo
$ mkfifo fofi
$ cat fofi | ssh -l 121581 139.18.143.253 > fifo & for x in `cat fifo | grep password`; do echo mypassword- > fofi; done
where "mypassword" is of course replaced by my password.
My computer then told me:
Code:
Pseudo-terminal will not be allocated because stdin is not a terminal.
whatever that means.

I don't understand much of this ssh stuff. In another forum I was told that I could try it via "public key authorization". That way I would not have to enter my password every time. I followed the instructions I was given there.
First I did this
Code:
trollkotze@a149010:~$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/trollkotze/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/trollkotze/.ssh/id_dsa.
Your public key has been saved in /home/trollkotze/.ssh/id_dsa.pub.
The key fingerprint is:
b0:51:76:3c:0c:7f:97:e0:ca:a5:fb:23:63:e5:6e:07 trollkotze@a149010
trollkotze@a149010:~$ ssh-copy-id -i ~/.ssh/id_dsa.pub 121581@139.18.143.253
32
121581@139.18.143.253's password:
and left the passphrase empty.
And after that I tried to login the way I was told:
Code:
trollkotze@a149010:~$ ssh 121581@139.18.143.253
121581@139.18.143.253's password:
Permission denied, please try again.
121581@139.18.143.253's password:
Permission denied, please try again.
121581@139.18.143.253's password:
As you can see I was still asked for a password but when I entered it now it was rejected.
I was told "public key authorization" must be activated on the server in order for this method to work. Due to the fact that it doesn't work I guess it is not activated. So this method is not an option for me either.

I guess a bash script with some piping won't do the job as the above examples suggest. So I would like to write a C program that logs in to the ssh server with my password. But I have absolutely no idea how to do that. Neither do I know much about sockets and network programming nor do I have any clue how exactly ssh works.
Is there an other and less painful way to do what I want to do?
Or is here, by chance, anyone with some experience in that field who could effortlessly hack together a chunk of C code or something that would do the job?

Any hints are highly appreciated.
Greetings

trollkotze
 
Old 01-06-2008, 12:10 AM   #2
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Rep: Reputation: 44
You might want to take a look at Net::SSH::Perl module. It would be much easier than writing it in C. You won't have to know any sockets or networking programming -- it will take care of all of that for you.

http://search.cpan.org/dist/Net-SSH-...et/SSH/Perl.pm

--jrtayloriv
 
Old 01-06-2008, 12:32 AM   #3
trollkotze
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you. That looks very promising.
 
Old 01-06-2008, 04:58 AM   #4
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 443

Rep: Reputation: 56
This can be made with a realy easy script:
Code:
#!/usr/bin/expect -f
spawn ssh username@isp_server_ip
expect "*password:*"
send -- "<password>\r"
interact
As you see out of this script, you will have to store your password in clear text, and that's a pretty lame security risk.
 
Old 01-06-2008, 05:33 AM   #5
trollkotze
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Original Poster
Rep: Reputation: 0
@Dinithion: Thank you very much. That helped me out. I didn't get that Perl stuff to work by now.
But about the security issue: Wouldn't that be the same in Perl? I mean, Perl is also an interpreter language and I would have to put my password into the code in plain text, or wouldn't I?
 
Old 01-09-2008, 08:53 PM   #6
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Rep: Reputation: 44
If you leave the password in plain text, make sure that you leave the script in root's home directory (/root). If somebody has access to /root, then the plain-text password stored there will be the least of your worries.

It shouldn't be that big of a risk if it's just the password to connect to get internet access at a hostel. I wouldn't worry about it. Just don't put it somewhere where any user other than root can read the file.

Last edited by jrtayloriv; 01-09-2008 at 08:54 PM.
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic ssh venki Linux - Newbie 6 02-03-2007 02:02 PM
Automatic login? Cooljerk Linux - Newbie 3 09-14-2005 04:52 PM
Automatic SSH Tunnels fearofcarpet Linux - Software 1 12-04-2003 11:36 PM
Automatic login Mad Echidna Linux - Software 1 12-17-2002 06:00 PM
Automatic Login SlavaVB Linux - General 2 03-11-2002 11:10 PM


All times are GMT -5. The time now is 03:43 PM.

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