LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2010, 07:48 AM   #1
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Rep: Reputation: 0
Post Telnet using expect


Hi guys,

Basically I need to telnet login to a machine, then run some commands. I've sorted the login bit, and this works: (the following is a new file that is called into an existing script)

Code:
#!/usr/bin/expect
set address [lindex $argv 0]
spawn "/bin/bash"
send "telnet ${address}\r"
expect "login:"
send "root\r"
expect "Password:"
send "pass_goes_here\r"
interact
The interact I don't want there, I want to send it a command to mount a filesystem, however when I put anything other than mount the script just ends and continues running the other script that called the expect script...

Not sure what I'm missing.

Heres pseudo for what I need to happen:

Code:
  1. Send Username
  2. Send Password
  3. cd /root/test
  4. mount machine2:/root/test /root/test
  5. close script and continue with the original script that called it
Hope that makes some sense.
 
Old 05-13-2010, 08:40 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,661

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by mikejreading View Post
Hi guys,

Basically I need to telnet login to a machine, then run some commands. I've sorted the login bit, and this works: (the following is a new file that is called into an existing script)

Code:
#!/usr/bin/expect
set address [lindex $argv 0]
spawn "/bin/bash"
send "telnet ${address}\r"
expect "login:"
send "root\r"
expect "Password:"
send "pass_goes_here\r"
interact
The interact I don't want there, I want to send it a command to mount a filesystem, however when I put anything other than mount the script just ends and continues running the other script that called the expect script...

Not sure what I'm missing.

Heres pseudo for what I need to happen:

Code:
  1. Send Username
  2. Send Password
  3. cd /root/test
  4. mount machine2:/root/test /root/test
  5. close script and continue with the original script that called it
Hope that makes some sense.
Yes, but a couple of things stand out. First, and I'm sure you'll get LOTS of feedback about this...you're logging in as ROOT via TELNET??? That is horribly unsafe...telnet sends the password in clear-text, over the network, and it's trivial to catch it. I'd strongly suggest using SSH. Also, I'd disable root logins over the network, for ANY protocol. You can always use "sudo" or even "su" to get root privileges, but login as root is always frowned upon.

Don't know how you're calling your mount statement, but are you putting it in as
Code:
send "mount <device> <path>"
It should just be as if you were entering any other command. An alternative would be to make a small shell-script on the remote box, that contains just the mount statement, and call that script via expect. I'd also look into doing this via Perl, since it has modules specifically written to handle telnet and SSH logins, but that may not be in your comfort zone.
 
Old 05-13-2010, 05:57 PM   #3
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Original Poster
Rep: Reputation: 0
No, im designing the system on a private network before putting it public. Using root just for the sake of permissions. Will be changing it to a user with SU before it goes anywhere public.


Yeah, thats how I'm sending the command, but it freezes after entering the password.

If I put the interact command after the send "password\r" it allows me to continue 'interacting' with the telnet session, however if i try to send another command it just stalls and ends the script.
 
Old 05-13-2010, 06:11 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
No, im designing the system on a private network before putting it public. Using root just for the sake of permissions. Will be changing it to a user with SU before it goes anywhere public.
Still, TBOne's advice about ssh is probably the best one you can get at this point. IMO it's better to start in a safe way and further develop it from there than to start it in an unsecure way and worry about security later on.
 
1 members found this post helpful.
  


Reply



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
Expect telnet, password problem Tarikc Linux - Desktop 5 09-26-2009 07:50 AM
Telnet script on Unix machine without expect cristoph_ Programming 7 12-24-2008 10:23 AM
Expect Scripting:- script not coming out of telnet session. nik1984 Programming 2 09-11-2008 08:14 AM
Expect Utility and Telnet metallica1973 Programming 1 12-28-2007 01:58 PM
telnet server not reliable for expect script powah Linux - Software 0 11-16-2005 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration