Find the answer to your Linux question:
Results 1 to 7 of 7
Hey guys I'm a new member to these forums after always looking here for answers. I'm stumped on a perl script that I'm hoping is very easy for someone else. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6

    Remote telnet from file


    Hey guys I'm a new member to these forums after always looking here for answers. I'm stumped on a perl script that I'm hoping is very easy for someone else.

    I'm looking for a perl script (or bash whatever is easier) to telnet to a remote IP using the usernames/passwords that are (currently) in a CSV file but any type of file will do.

    I know how to make a script to log into a remote telnet session if I put in the IP and U/P manually, but I need the script to read from a file and input the IP/U/P.

    Any help... links... anything at all is greatly appreciated!

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6
    Or if anyone has one for a SSH login, I'm sure the conversion is very easy.

    Remote SSH login script that reads from a file filled with usernames/passwords/ips

    Thanks again!

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,221
    For ssh, you usually work with authorized keys and ssh-agent(s).
    With that in place, you won´t need to provide a password for the connections.

    For the multiple connections based on a file:
    Depends on what you want to do.
    a) interactive console:
    Have a look at SourceForge.net: clusterssh
    The config file lets you configure (multiple) groups of hosts

    b) "run the same script everywhere"
    parallel-ssh - PSSH: Parallel SSH Tools - Google Project Hosting
    Here, you can provide a file with a list of hosts "-h)
    and can "send input" via another file with "-I".
    This other file would contain bash/whatever commands
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6
    Thank you for the response!

    I've been exposed to the SSH keys but it does not pertain to what I need accomplished. I was using the example for an SSH login script just because I figured it would be real easy to convert to telnet.


    Where I'm at right now: I have a bash script running EXPECT to auto-log into my telnet server.
    --

    #!/usr/bin/expect -f
    spawn telnet x.x.x.x
    expect -re "User Name"
    send "USERNAME\r"
    expect -re "Password"
    send "PASSWORD\r"
    expect -re "4- Logout"
    send "4\r"
    sleep 3
    send "exit\r"
    expect eof

    --

    The only problem is I have to manually put in the IP/USERNAME/PASSWORD.

    Is there a way that I can have file ... lets call it 'logins.txt' or something that my EXPECT script can read from and just go down the list of IPS/USERNAMES/PASSWORDs?

  5. #5
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6
    Alright guys in case anyone was wondering how my progress was going (which i'm sure all of you were )

    Almost everything is working.

    #!/usr/bin/expect
    INPUT=./pdu_logins.csv
    OLDIFS="$IFS"
    IFS="1G,"
    [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
    while read acctPDUID acctID rackPDU username password ip type
    do

    spawn telnet $ip
    sleep1
    expect -re "User Name"
    send "$username\r"
    expect -re "Password"
    send "$password\r"
    expect -re "4- Logout"
    send "4\r"
    sleep 3
    send "exit\r"

    done < $INPUT
    IFS=$OLDIFS



    Here's my error

    $ ./login2.sh
    invalid command name "INPUT=./pdu_logins.csv"
    while executing
    "INPUT=./pdu_logins.csv"
    (file "./login2.sh" line 2)


    The weird part is if I run these separate..
    #!/usr/bin/expect -f
    spawn telnet $ip
    sleep1
    expect -re "User Name"
    send "$username\r"
    expect -re "Password"
    send "$password\r"
    expect -re "4- Logout"
    send "4\r"
    sleep 3
    send "exit\r"


    and

    #!/bin/bash
    INPUT=./pdu_logins.csv
    OLDIFS="$IFS"
    IFS="1G,"
    [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
    while read acctPDUID acctID rackPDU username password ip type
    do
    echo '$ip'
    done < $INPUT
    IFS=$OLDIFS


    They work. So it's something about combining them. I think it has to do with the #!/bin/bash and the #!/usr/bin/expect -f


    Any advice? I feel so close!!
    Last edited by SirLucas; 08-11-2011 at 03:08 PM.

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6
    *small bump* for edit to clarify things. And my deadline is today sORRY!

  7. #7
    Just Joined!
    Join Date
    Aug 2011
    Posts
    6
    I finally got it in case anyone out there might ever need a script like this.


    #!/bin/bash
    INPUT=/directory/file
    OLDIFS=$IFS
    IFS=,
    [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
    while read username password ip
    do

    /usr/bin/expect - << EndMark

    spawn telnet $ip
    sleep 2
    expect -re "User Name"
    sleep 1
    send $username
    send "\r"
    sleep 1
    expect -re "Password"
    sleep 1
    send $password
    send "\r"
    sleep 1
    expect -re "4- Logout"
    send "4"
    send "\r"
    sleep 2


    EndMark

    done < $INPUT
    IFS=$OLDIFS



    Works like a charm!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •