I am trying to write a script which calls the following command
[root#xxxxx imtadmin]# admin tower
-------------------------------------------------------------------
GMS: address=xxxxx-42450, cluster=CLRTY-SA, physical address=xxxxx:45155
-------------------------------------------------------------------
NSA Tower Shell
Usage:
history Show admin shell command history.
!<command prefix> Execute a command maching the given prefix from the command history.
refresh Wake up monitor thread, to update clients.
autodiscovery <on|off> Turn autodiscovery on or off.
add <host(s)> Add given host(s) by address.
remove <host(s)> Remove given host(s) by address.
list clients Print listing of clients.
list services Print listing of client services.
select <address|index> Select a specific client host by address.
connect <address> [port] Connect to a given Select a specific
client host by address.
trace Send trace message on multicast channel.
trace <on|off> Turn trace listening on or off.
password <password> Change password for this session only.
reset password Reset password to local machine value.
group <group> Change channel group for this session only.
(such as CLRTY for listening to PPM
broadcasts.)
reset group Reset group to local machine value.
list group List group members.
exit|quit Exit the tower.
? Print this message.
> list clients **TERMINAL INPUT***
Discovered Clients:
--------------------------
1) xxxxx.ec2.internal:9091 [xxxxx-20212]
--------------------------
> refresh **TERMINAL INPUT***
> list clients **TERMINAL INPUT***
Discovered Clients:
--------------------------
1) xxxxxx.ec2.internal:9091 [xxxxx-59053]
2) yyyyy.ec2.internal:9091 [yyyyy-20212]
3) zzzzzz.ec2.internal:9091 [zzzzz]
--------------------------
>
My Script:
#!/usr/bin/expect
spawn /opt/clarity/bin/admin tower
set timeout -1
expect -re "^>{1}"
send "list clients"
My script does call the command admin tower and stops at the prompt > but it doesn't accept input provided by script or input I provide from the terminal (my goal is to provide input from the script).
This should work :
send "list clients\n"
expect eof
Following worked. Thanks, #Philippe for hinting I need to add expect after providing input.
#!/usr/bin/expect
spawn /opt/clarity/bin/admin tower
expect -re "^>"
send "list clients\r"
expect -re "^>{1}"
send "refresh\r"
expect -re "^>{1}"
send "list clients\r"
expect -re "^>{1}"
send "exit\r"
Related
I am using expect via telnet to connect the box and then using expect script as follows:
expect -c 'spawn -noecho telnet IP' exp_script
On the expect script [exp_script] - Setting the user prompt and then using interact to interact the user to pass their cmd and stay on the prompt for further uses.
However I need to restrict the user to interact with box for certain time, the time frame will be dynamic and changes based on users.
So my question is how to restrict the users to interact the box via expect with certain timeframe only and then user should auto exit from box.
Highly appreciated for any help here!!
This uses the Tcl after command to schedule code to run after X milliseconds
spawn -noecho telnet IP
proc get_user_time_in_milliseconds {username} {
# checks here
set limit 5000 ;# => 5 seconds
return $limit
}
proc byebye {spawn_id} {
send_user "\n\nYour time is up. Bye!\n"
exp_close $spawn_id
exit
}
after [get_user_time_in_milliseconds $env(LOGNAME)] [list byebye $spawn_id]
# rest of expect code goes here.
interact
I have a very simple expect script to connect to a Cisco Wireless Lan Controller via Cisco router.
My problem is that after some commands expect stops suddenly without finish all sentences and without any error.
I think that the problem is with * from password but I'm not sure.
That's my script
#!/usr/bin/expect
set timeout 10
spawn telnet X.Y.Z.W
expect "User Access Verification"
expect "Username:"
send "admin\r"
expect "Password:"
send "PASSWORD\r"
expect "Router#"
send "telnet WLC_IP\r"
expect "(Cisco Controller)"
expect "User:"
send "admin\r"
expect "Password:"
send "PASSWORD\r"
expect "Cisco Controller"
send " config paging disable\r"
expect "Cisco Controller"
send " show ap auto-rf 802.11b AP-NAME\r"
expect "Cisco Controller"
send "logout\r";
And this is the output after running,
spawn telnet X.Y.Z.W
Trying X.Y.Z.W...
Connected to X.Y.Z.W.
Escape character is '^]'.
User Access Verification
Username: admin
Password:
Router#telnet WLC_IP
Trying WLC_IP ... Open
(Cisco Controller)
User: admin
Password:**********
(Cisco Controller) >?
clear Clear selected configuration elements.
config Configure switch options and settings.
cping Send capwap echo packets to a specified mobility peer IP address.
debug Manages system debug options.
eping Send Ethernet-over-IP echo packets to a specified mobility peer IP address.
grep Print lines matching a pattern.
help Help
license Manage Software License
linktest Perform a link test to a specified MAC address.
logout Exit this session. Any unsaved changes are lost.
mping Send Mobility echo packets to a specified mobility peer IP address.
ping Send ICMP echo packets to a specified IP address.
reset Reset options.
save Save switch configurations.
show Display switch options and settings.
test Test trigger commands
transfer Transfer a file to or from the switch.
(Cisco Controller) >config paging disable
(Cisco Controller) >[root#mailman scripts]#
Also, there are an "?" after password that I don't know why is it.
Any ideas?
Thanks in advance!
after setting expect eof at the end of my script it works well! I'm not sure if it's mandatory but works for me.
Thank you!
I have the following scripts which is supposed to pull the IP address from a file device-list.txt and then telnet to the device, login and execute a show run. The script logs in to the device successfully, but gives me an error after that. Any ideas where im going wrong?
for device in `cat device-list.txt`; do ./test4 $device;done
cat test4
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user myusername
set pass mypassword
set timeout 10
log_file -a ~/results.log
send_user "\n"
send_user ">>>>> Working on $hostname # [exec date] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect "#"
send “term len 0\n”
send “show running-config\n”
expect “end\r”
send “\n”
send “exit\n”
User Access Verification
Username: myusername
Password:
ROUTER#usage: send [args] string
while executing
"send “term len 0\n”"
(file "./test4" line 26)
Your problem is due to incorrect double quotes. You have to use double quotes ", not smart quotes “
send “term len 0\n” ; # Wrong
send "term len 0\r"; # Correct
Note :
Basically, Expect will work with two feasible commands such as send and expect. If send is used, then it is mandatory to have expect (in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)
This is because without that we will be missing out what is happening in the spawned process as Expect will assume that you simply need to send one string value and not expecting anything else from the session.
So, we recommend to use expect after each send, to make sure our commands actually sent to the spawned process. (You have not used expect after sending some commands such as term len 0\r). Also, use \r instead of \n.
I am trying to transfer a file from one server to a remote server with the help of a TCL script.
But my script stops after the message "200 Port set okay" and continues to rum from the below telnet session.
I have checked the destination location, my file is not transferred.
Please suggest what can I do or where I am wrong
#!/usr/bin/tclsh
#!/usr/bin/expect
package require Expect
set p "mm155_005.006.010.200_bt.fw"
#**************************************************************\
FILE TRANSFER TO REMOTE SERVER \
***************************************************************
spawn ftp 10.87.121.26
expect "User (10.87.121.26:(none)):"
send "user\r"
expect "Password:"
send "pswd\r"
expect "ftp>"
send "cd FW\r"
expect "ftp>"
send "ha\r"
expect "ftp>"
send "bi\r"
expect "ftp>"
send "mput \"$p\"\r"
expect "mput $p? "
send "yes\r"
expect "ftp>"
send "ls\r"
#**************************************************************\
RUNNING THE TRANSFERED FILE \
***************************************************************
spawn telnet 10.87.121.26
expect "Login: "
send "user\r"
expect "password: "
send "pswd\r"
expect "*? > "
send "cd FW\r"
expect "*? > "
send "burnboot 30 5.6(10.200)\r"
Output
spawn ftp 10.87.121.26
Connected to 10.87.121.26.
220 VxWorks FTP server (VxWorks VxWorks5.4.2) ready.
Name (10.87.121.26:vkumar): user
331 Password required
Password:
230 User logged in
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd FW
250 Changed directory to "C:/FW"
ftp> ha
Hash mark printing on (1024 bytes/hash mark).
ftp> bi
200 Type set to I, binary mode
ftp> mput "mm155_005.006.010.200_bt.fw"
mput mm155_005.006.010.200_bt.fw? yes
200 Port set okay \ I am unable to see hash progress bar after this line
spawn telnet 10.87.121.26
Trying 10.87.121.26...
Connected to 10.87.121.26.
Escape character is '^]'.
Login: user
password:
node84.7.PXM.a > cd FW
node84.7.PXM.a > bash-2.05b$
Instead of rolling your own solution in Expect (I also did this about 9 years ago), use the FTP module from tcllib -- it's already battle-hardened.
http://tcllib.sourceforge.net/doc/ftp.html
The script as reported is unlikely to produce exactly that output; there is nothing from the ls done after the mput. However, if the mput is hanging the most likely problem is that there is a firewall issue; FTP uses multiple sockets to do file transfers (which is why FTP is such a pain when it comes to overall firewall management). In particular, it has a command channel (the socket which you communicate with the FTP server over) and a separate data channel per file (and also with the output of some remote commands, such as ls); that's what that Port set okay is about. This is not firewall-friendly, and it's easy to misconfigure firewalls in this area (especially when there is NAT also in place).
You might (i.e., try this first) want to use passive mode instead, as that reduces the complexity at the firewall level. Try issuing a passive before the mput (just as you currently issue a binary).
Here's a bash script I wrote with a similar function;
You should be able to adapt it to your needs.
Adding a few lines to SSH in and run the script should be quite trivial.
As a side note; why are you using TCL for this?
#!/bin/bash
fileName=`ls /home/user/downloads -t1 | head -n1`
latestFile="/home/user/downloads/$fileName"
echo $latestFile
hostname="HOSTNAME"
username="USER"
password="PASS"
ftp -inv $hostname << EOF
quote USER $username
quote PASS $password
cd transfer/jirabackup
binary
put $latestFile $fileName
quit
Recently I have been involved for the preparation of the shell script on SunOS with csh shell. I will have multiple queries but first the short program isn't working.
[username]% expect - << EOF
Spawn telnet 74.125.71.103
expect "Password:"
send "google\r"
EOF
The following error pops up:
/bin/csh: Event not found
[username]% expect: Command not found
Please advise.
This script should run in following manner :
Telent the IP and use the existing passwd (explicitly given in the script).
After the telnet, it shows MENU options
MB station
RC
ODU
AP
SU
Exit
type 1 // a "MB station" MENU options will open i.e.
1 - Show
2 - Unit Control
type 2 // UC MENU options will open i.e
1 - Change Password
2 - Reset
type 1 //change passwd MENU options will open i.e.
1 - Change PC Password
2 - Change LU Password
3 - Change Admin Password
type 3 // to change ADMIN passwd
MB station - Change Admin Password
Enter New Password : XYZ enter
Re-enter Password : XYZ enter
New password accepted
3 times escape // to escape from telnet
1.MB station
2. RC
3. ODU
4. AP
5. SU
6. Exit
type 6 // to exit
Exit? [Y/N] y
Connection to host lost.
then move to step with different IP. The IP values will be given by the user one-time while executing the script at the prompt e.g. ./pass-change IPs.txt
The Event not found message implies you're trying to do some kind of history substitution. This normally involves the ! character. Did you type something with a ! character in it at some point?
And expect: Command not found means just what it says: the shell wasn't able to find the expect command. Is it installed? If not, you should install it if you can, or ask a system administrator to install it for you, or, failing that, obtain the source and build and install it under your home directory.
Once you fix that, there's an expect command called spawn, and it's case-sensitive; Spawn won't be recognized.
If you're specifically asking how to accomplish the above without using expect, please update your question to make that clear.
I have a working script below, you are getting error because i believe you are trying to run the script using some sh or other command. Try to run it as ./script. I used below sample script for file transfer but i did teh key exchange manyally by ssh back and forth which is one time activity.
#!/usr/bin/expect -f
set timeout 130
spawn ssh "idk#server.com"
expect "password: "
send "pass#1234\r"
expect "$ "
send "sh /home/nathalok/HTML/run.sh\r"
expect "$ "
send "exit\r"