Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tcl script for creating folders on multiple remote servers from local machines and transfer files to those servers into created folder

143 views
Skip to first unread message

kj

unread,
Nov 12, 2008, 2:49:29 AM11/12/08
to
Hi all,
I am newbie in Tcl. I have just started learning Tcl fundamentals
I want to code a script that does the following.
1) Reads a file in which server names and corresponding ip addresses
are saved. (Approx 5)
name1 <space> xxx.xxx.x.xxx
name2 <space> xxx.xxx.x.xxx
name3 <space> xxx.xxx.x.xxx
name4 <space> xxx.xxx.x.xxx
name5 <space> xxx.xxx.x.xxx
-this is file which will be having server name and ip addresses as
above format.
2) Takes 1st name and ip, telnet to the server, enters username and
password.
3) creates a directory.
4) Transfers the file via FTP to created directory.
5) logout from the server.
6) Reads second entry in the file repeats from step2 until all server
IPs are not covered.

I am able to make a directory by using telnet session and by using
list (for server name and IPs).
But I am unable to use file for getting name and IPs of server. And
also transfer a file via FTP to created Directory.
Can you please help me

eiji

unread,
Nov 12, 2008, 3:53:53 AM11/12/08
to
Hi kj,

I can't help you with all of that.
I have no idea how to do that, but from what I know, Expect is the
tool you should look for, which seems to work with Tcl as well:
http://wiki.tcl.tk/201

I guess you can write a script and Expect knows Tcl commands and
extends Tcl with some more commands to do the server/login-stuff.

###############################
namespace eval ::srvupdate {}

proc ::srvupdate::ReadSrvs {} {

set srv_file "../servers.dat"

set ret_list [list]

if [catch {open $srv_file r} fileIdread] {
return -code error "$::errorInfo: $srv_file"
} else {
set fileVar [read $fileIdread]
close $fileIdread
set fileList [split $fileVar "\n"]
foreach line $fileList {
if {[scan $line "%s %s" srv_name ip] == 2} {
lappend ret_list $srv_name $ip
}
}
}
return $ret_list
}

proc ::srvupdate::DoExpectThing {} {

foreach {srv_name ip} [ReadSrvs] {
#
# Use Expect here
#
ExpectWrap $srv_name $ip
}
}

proc ::srvupdate::ExpectWrap {srv_name ip} {

# connect
# do the login stuff and whatever

puts "$srv_name $ip"
}

::srvupdate::DoExpectThing
###################################

This only is a starting point.
This script reads your servers from servers.dat (change path) and now
the question is, what to do in "ExpectWrap".
Maybe someone else can help you with that.

Check out http://wiki.tcl.tk/201 with lots of information.

Regards,
Eiji

kj

unread,
Nov 12, 2008, 5:01:42 AM11/12/08
to
I am already using expect here. Below is what I doing.

################################
set username <username>
set passed <password>

foreach server {srv1 srv2 srv3 srv4 srv5} ipadd {<ip1> <ip2> <ip3>
<ip4> <ip5>} {

spawn telnet $ipadd;
expect "username:";
send "$username\r";
expect "password:";
send "$password";

expect "ftp>";
send "mkdir /home/<dirname>\r";

expect "ftp>";
send "exit\r";
}

this is working well. I am able to create a directory as well.
But Instead of those 2 list, I want to use a file which is having
server entries as below.
srv1 xxx.xxx.xx.xxx
srv2 xxx.xxx.xx.xxx
srv3 xxx.xxx.xx.xxx
srv4 xxx.xxx.xx.xxx
srv5 xxx.xxx.xx.xxx

script should read the file, take name and IP, create a folder, FTP
the test file from local machine to respective server.

What problem I am facing is
1) I am not able to read the file and get server name and IP
2) while I am in Telnet session, how do I enter in FTP session to
transfer a file

eiji

unread,
Nov 12, 2008, 5:35:25 AM11/12/08
to

> I am already using expect here. Below is what I doing.

> What problem I am facing is


> 1) I am not able to read the file and get server name and IP
> 2) while I am in Telnet session, how do I enter in FTP session to
> transfer a file

For 1)

This should work with the code above:
####################
proc ::srvupdate::ExpectWrap {srv_name ipadd} {

# connect
# do the login stuff and whatever

set password "my_password"
set username "my_username"


spawn telnet $ipadd;
expect "username:";
send "$username\r";
expect "password:";
send "$password";

expect "ftp>";
send "mkdir /home/<dirname>\r";

expect "ftp>";
send "exit\r";

#puts "$srv_name $ip"
}
##########################
I just changed ExpectWrap to do the Expect-magic.
You still have to change "set srv_file "../servers.dat"" to points to
the right file.

When it come to the file transfer I'm lost.
That's not my sector. Sorry.

Regards,
Eiji

kj

unread,
Nov 12, 2008, 6:08:00 AM11/12/08
to

From your 1st reply my problem was solved. The code worked perfactly.

Only thing where I was getting confused is while running in telnet
session is it possible to invoke ftp session or not.

But Thanks very much for the precious time and help.

Cameron Laird

unread,
Nov 12, 2008, 9:06:53 AM11/12/08
to
In article <e2cc45f3-8a2f-4de6...@d10g2000pra.googlegroups.com>,
kj <kinjalde...@gmail.com> wrote:
.
.
.

>From your 1st reply my problem was solved. The code worked perfactly.
>
>Only thing where I was getting confused is while running in telnet
>session is it possible to invoke ftp session or not.
>
>But Thanks very much for the precious time and help.

I've lost track of your goal.

Yes, it certainly is possible to launch an FTP session from a telnet section.

Do any other questions remain for you?

Glenn Jackman

unread,
Nov 12, 2008, 11:29:42 AM11/12/08
to
At 2008-11-12 05:01AM, "kj" wrote:
> I am already using expect here. Below is what I doing.
>
> ################################
> set username <username>
> set passed <password>
>
> foreach server {srv1 srv2 srv3 srv4 srv5} ipadd {<ip1> <ip2> <ip3>
> <ip4> <ip5>} {
>
[... expect controlling ftp ...]

> }
>
> this is working well. I am able to create a directory as well.
> But Instead of those 2 list, I want to use a file which is having
> server entries as below.
> srv1 xxx.xxx.xx.xxx
> srv2 xxx.xxx.xx.xxx
> srv3 xxx.xxx.xx.xxx
> srv4 xxx.xxx.xx.xxx
> srv5 xxx.xxx.xx.xxx
>
> script should read the file, take name and IP, create a folder, FTP
> the test file from local machine to respective server.
>
> What problem I am facing is
> 1) I am not able to read the file and get server name and IP

Here's how:

if {[catch {open $filename r} fh] != 0} {
# cannot open $filename, error message is in $fh
error $fh
}
while {[gets $fh line] != -1} {
foreach {server ipadd} [split $line] break
puts "handling server $server ($ipadd)"
# do your ftp thing
}
close $fh

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

kj

unread,
Nov 12, 2008, 10:57:56 PM11/12/08
to

My entire problem is solved.
Thanks for the help and time.

0 new messages