Automatisation of telnet, ftp or ssh

For ftp you have a quick and dirty trick :

$ ftp -n yourhost <<-here
> quote USER youruser
> quote PASS yourpassword
> ls
> quit
> here

(you can do a put, get, whatever you want instead of “ls”)

For telnet similar idea:

$ (echo -e “open yourhostr”; sleep 2;echo -e “youruserr”; sleep 1; echo -e “yourpassword”; sleep 1; echo -e “unamer”; sleep 1; echo -e “exit”) | telnet

That sound pretty good but … it does not work on solaris (ok on Linux), and no way for ssh :(
So if this shoddy doesn’t meet your expectations…

Expect is what you are looking for. Available for GNU-linux, solaris… MSwindows… at http://expect.nist.gov/
For debian do it cool:

apt-get install expect

I do it with 2 scripts. I’m sure that you can do better, but that’s work ;-)

– script to call the expect and to make things good looking:
host=$1
psswd=$2
cmd=$3
os=$( ./expectaTion.tcl $host $psswd $cmd| awk “/$cmd/ “‘{getline; print}’|tr -d ‘

One Response to “Automatisation of telnet, ftp or ssh”

  1. Pascal says:

    – script expect : expectacion.tcl

    #!/usr/bin/expect
    set timeout 180
    set host [lindex $argv 0]
    set user root
    # user is root here but is can be an other and after send a “su” to do serious things

    set password [lindex $argv 1]
    set cmd [lindex $argv 2]

    # you can spawn ssh ftp, whatelses…
    spawn /usr/bin/telnet $host
    # keep in mind that the login strings can be other
    expect “ogin: ”
    send “$user\r”
    expect “assword: ”
    send “$password\r”
    # of course the prompt can be also other
    expect “root# ”
    send “$cmd\r”
    expect “root# ”
    send “exit\r”
    expect eof

    – end of the script

    Pascal.

Leave a Reply

You must be logged in to post a comment.