LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-09-2011, 11:38 AM   #1
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Rep: Reputation: Disabled
Question Script writing help


Hi everyone,
I am trying to create a script that does the following. The script needs to run daily. I need the output (what’s show on the screen as if I was typing the commands, with the commands dumped into a text file).
For the record, this is the first script I am trying to write and I am totally lost.

1. Create Log.
2. Scp dic*.txt root@10.0.120.19:/root/dictionary ivan@10.0.120 .123: /srv/ivan
3. Telnet 10.0.120.123 7777
a. Load Right-processing-board
i. Stat statistics-0
ii. Exit
b. Exit
c. “Load t t t t” and wait for response.
d. Exit
4. Ssh root@10.0.120.22
a. “Service daemon stop” and wait for response.
b. “Service stop” and wait for response.
c. “Service start” and wait for response.
d. “Service daemon start” and wait for response.
e. Exit
5. Telnet 10.0.120.123 7777
a. Load Right-processing-board
i. dump pcie-output -0
ii. Exit
b. Exit
6. Email log file output
End script

Thank you so much!
 
Old 11-09-2011, 11:51 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,952

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by alonchu View Post
Hi everyone,
I am trying to create a script that does the following. The script needs to run daily. I need the output (what’s show on the screen as if I was typing the commands, with the commands dumped into a text file).
For the record, this is the first script I am trying to write and I am totally lost.

1. Create Log.
2. Scp dic*.txt root@10.0.120.19:/root/dictionary ivan@10.0.120 .123: /srv/ivan
3. Telnet 10.0.120.123 7777
a. Load Right-processing-board
i. Stat statistics-0
ii. Exit
b. Exit
c. “Load t t t t” and wait for response.
d. Exit
4. Ssh root@10.0.120.22
a. “Service daemon stop” and wait for response.
b. “Service stop” and wait for response.
c. “Service start” and wait for response.
d. “Service daemon start” and wait for response.
e. Exit
5. Telnet 10.0.120.123 7777
a. Load Right-processing-board
i. dump pcie-output -0
ii. Exit
b. Exit
6. Email log file output
End script

Thank you so much!
Ok..you've told us what you WANT...now show us what you've DONE. We're not going to write the script for you, but we'll be glad to HELP you with it.

First, check out the bash scripting guide:
http://tldp.org/LDP/abs/html/

...and check out the expect scripting guide:
http://linux.die.net/man/1/expect
http://www.linuxjournal.com/article/3065

SCP'ing the files over is fairly easy, especially if you've got your keys swapped between machines. Otherwise, your script will prompt you for a password. Also, I'd break up the two different copy commands into two different lines, then use expect to telnet over and issue your commands. When you run it, you can just append the output to the log file you create at the beginning, then mail it off when you're done, and delete it.
 
Old 11-09-2011, 12:02 PM   #3
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hey T-Bone,
Thank you very much!
I wasn't expecting someone to write it for me.
I was looking for a point at like the websites you gave.

I am still working on a draft.

Also, could you give me the command for the logging everything to a txt file.

Thanks,
Alon.
 
Old 11-09-2011, 12:30 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,952

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by alonchu View Post
Hey T-Bone,
Thank you very much!
I wasn't expecting someone to write it for me.
I was looking for a point at like the websites you gave.
I am still working on a draft.
Thanks for that...you'd be amazed at how many folks come here, expecting just that...someone to write their scripts for them. Guidance is ALWAYS available.
Quote:
Also, could you give me the command for the logging everything to a txt file.
Could do it a few different ways, but when you create your log file, just use the append ">>" to shove things into it. So, for example:
Code:
ls /some/path >> /path/to/logfile.txt
df -kPH >> /path/to/logfile.txt
would write the results of those commands into the logfile.txt file, one after the other. If you're going to be issuing several commands, though, consider putting a break into the log, so you know what step is happening, like
Code:
echo "Step five starts now...telnetting to host" >> /path/to/logfile.txt
...then after step five completes...
echo "Step five completed...going on to step six...." >> /path/to/logfile.txt
 
1 members found this post helpful.
Old 11-09-2011, 12:34 PM   #5
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Thanks for that...you'd be amazed at how many folks come here, expecting just that...someone to write their scripts for them. Guidance is ALWAYS available.

Could do it a few different ways, but when you create your log file, just use the append ">>" to shove things into it. So, for example:
Code:
ls /some/path >> /path/to/logfile.txt
df -kPH >> /path/to/logfile.txt
would write the results of those commands into the logfile.txt file, one after the other. If you're going to be issuing several commands, though, consider putting a break into the log, so you know what step is happening, like
Code:
echo "Step five starts now...telnetting to host" >> /path/to/logfile.txt
...then after step five completes...
echo "Step five completed...going on to step six...." >> /path/to/logfile.txt
That is exactly what I was looking for and the clarifications are great!
Much obliged!
 
1 members found this post helpful.
Old 11-13-2011, 07:52 PM   #6
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hello again,

Well I have put together a script.
But for some reason, when I try to send a command it stops at load (no enter just the screen freezes and waits for user input)

Any Ideas?

Quote:
#!/bin/bash
set timeout 60
expect << EOF
spawn telnet 10.0.120.123 7777
expect ">"
send -- "load t t t t\r”
expect “>”
send -- “exit\r”
exit
EOF
 
1 members found this post helpful.
Old 11-13-2011, 08:15 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,952

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by alonchu View Post
Hello again,

Well I have put together a script.
But for some reason, when I try to send a command it stops at load (no enter just the screen freezes and waits for user input)

Any Ideas?
Try removing the "--" before the send payload string.
 
Old 11-13-2011, 08:25 PM   #8
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Original Poster
Rep: Reputation: Disabled
turn out I was missing " on the EOF art the top.

Thank you!
 
1 members found this post helpful.
Old 11-14-2011, 09:29 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,952

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by alonchu View Post
turn out I was missing " on the EOF art the top.

Thank you!
Hehe...I missed it too! It's always something simple....
 
Old 11-14-2011, 11:25 AM   #10
alonchu
LQ Newbie
 
Registered: Nov 2011
Location: New York, NY
Posts: 9

Original Poster
Rep: Reputation: Disabled
Question

So i have the first part working.

However, with this script:
Code:
#!/bin/bash
echo on
expect << "EOF"
spawn telnet 10.0.120.123 7777
expect "ivan::at>"
send "focus right-processing-board\r"
expect “:pm2>”
send “stat statistics-0\r”
expect “:pm2> ”
send “exit\r”
exit
EOF
I get this
Quote:
spawn telnet 10.0.120.123 7777
Trying 10.0.120.123...
Connected to 10.0.120.123 (10.0.120.123).
Escape character is '^]'.
@@OK@@ 100 200 Welcome!
ivan::at> focus right-processing-board
pm2
@@OK@@ 101 200
m2> usage: send [args] string
while executing
"send “stat statistics-0\r”"
and with this script (I add the -- next to the send) I only get the first part of the command sent.

Code:
Code:
#!/bin/bash
echo on
expect << "EOF"
spawn telnet 10.0.120.123 7777
expect "ivan::at>"
send "focus right-processing-board\r"
expect “:pm2>”
send -- “stat statistics-0\r”
expect “:pm2> ”
send -- “exit\r”
exit
EOF
Result:
Quote:
spawn telnet 10.0.120.123 7777
Trying 10.0.120.123...
Connected to 10.0.120.123 (10.0.120.123).
Escape character is '^]'.
@@OK@@ 100 200 Welcome!
ivan::at> focus right-processing-board
pm2
@@OK@@ 101 200
m2> stat[root@localhost script]#
Any ideas?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help in writing a script sumeet inani Linux - Newbie 3 07-28-2011 06:38 AM
Need help writing script Rustylinux SUSE / openSUSE 1 12-05-2006 11:05 AM
Script writing help, somebody? cwwilson721 Slackware 6 05-19-2006 10:40 PM
Help writing a script mral Linux - Newbie 4 12-29-2005 01:34 AM
help writing script willinusf Programming 7 07-20-2004 12:37 PM


All times are GMT -5. The time now is 03:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration