LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Telnet using expect (https://www.linuxquestions.org/questions/programming-9/telnet-using-expect-807577/)

mikejreading 05-13-2010 07:48 AM

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.

TB0ne 05-13-2010 08:40 AM

Quote:

Originally Posted by mikejreading (Post 3966915)
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.

mikejreading 05-13-2010 05:57 PM

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.

sycamorex 05-13-2010 06:11 PM

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.


All times are GMT -5. The time now is 08:39 PM.