How can I open a remote ssh session with perl
Hello , I want to open a remote ssh session using the perl script , I am able to login to the server using the simple script and i am also able to execute few commands which is defined in the script on the server. I am writing a script which takes the user name , password and hostname from the database and login to the server . I am only able to login to the server and my script logs out and only I can run few command if I provide those command in my script else I am not able to run those command after logging in. I need to run those command once I have logged in using my script and I want to track down all the commands being executed in a particular session. I know how to run command on remote shell using script , but I want to open remote shell using perl and want to run the command manually and terminate the session when I am done as we normally do with shell . Any help or guidance will be highly appreciated. -- Regards, Pratap Singh
Re: How can I open a remote ssh session with perl
Here is my code which logs in and performs some task but it does not show the output of top and pstree command what would i do to get the output of these commands along with vmstat and iostat #!/usr/bin/perl -w use Net::SSH::Perl $host = '192.168.3.36'; $username = 'root'; $login_passwd = 'password'; #$session -> close; #my $cmd = "ls -l"; my $ssh = Net::SSH::Perl->new($host); $ssh->login($username, $login_passwd); #my $arg= join '',@ARGV; #while ($arg) { while(<>) { $cmd = $_; my($stdout,$sterr, $stderr) = $ssh->cmd("$cmd"); print "\n$stdout\n$stderr "; print "quiting from the server " if eof; } Thanks for all the responses .. Regards, Pratap
Re: How can I open a remote ssh session with perl
> Hello , > Thanks Shlomi for valuable giving input on my code I will implement that > too. > > But still my question remains same is it possible to open a remote session > with perl and get the output of "top", "vmsate 1 10", "iostat 1" , "yum > install etc", etc .. > > > I am not getting the output of those command using my script,other (command > such as "w, ls,ls-al,") works fine, is there any way we can get the output > of these ("top", "vmstat 1 10", "iostat 1" , "yum install etc") commands. > > Thanks > Pratap >
Re: How can I open a remote ssh session with perl
Hello Peter , Did you try executing command "top -cd2", "iostat 1", vmstat 1 10" etc ? Did you get the output of those command ? I am not getting the output of these commands rest works fine for me . Pstree, I am getting partial output Here is the output at my end of pstree command />perl login.pl pstree ?-+-dbus-daemon |-dbus-launch |-dcopserver |-gconfd-2 |-kaccess |-kded |-kdeinit-+-artsd | |-kio_file | |-kio_pop3 | |-klauncher | |-konsole-+-bash---ssh | | |-bash---perl | | |-2*[bash---e---ssh] | | `-bash | |-kwin | `-pidgin |-kdesktop---run-mozilla.sh---firefox---9*[{firefox}] |-kicker |-kmail---4*[{kmail}] |-kmix |-knotify `-ksmserver = Thank you for your continued help. Regards, Pratap
Re: How can I open a remote ssh session with perl
Hello , Is there any way I can get these interactive output via my script or I am missing something? .. Thanks for your continued help. I need to get those interactive outputs to my script or is there any way to open a psuedo terminal. I do not want to execute command on server it should be examined via my script and then it should get executed . Let me know if this is possible using perl or I have to use any other scripting language like python. Thank you Pratap
Re: How can I open a remote ssh session with perl
This code(given below) is working fine for all the command but for some command it is not able to print the output. I am getting the following output == w 52 column window is too narrow == Similarly for top it is not printing all the field . top -cd2 top - 20:36:28 up 21:30, 18 users, load average: 1. Tasks: 38 total, 1 running, 37 sleeping, 0 sto Cpu(s): 8.4%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, Mem: 1025056k total, 1003548k used,21508k fre Swap: 2031608k total, 103608k used, 1928000k fre Actual top output on the same system is as below top -cd2 top - 20:37:05 up 21:30, 17 users, load average: 1.10, 1.07, 1.00 Tasks: 34 total, 1 running, 33 sleeping, 0 stopped, 0 zombie Cpu(s): 8.5%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1025056k total, 994780k used,30276k free, 2940k buffers Swap: 2031608k total, 103600k used, 1928008k free,86892k cached #!/usr/bin/perl -w use strict; use Net::SSH::Perl; my $host = '192.168.0.114'; my $username = 'pratap'; my $login_passwd = 'pratap123'; my $ssh = Net::SSH::Perl->new($host, $username); $ssh->config->set('interactive', 1) unless defined $ssh->config->get('interactive'); $ssh->login($username, $login_passwd); my $cmd; if ($cmd) { my($out, $err, $exit) = $ssh->cmd($cmd); print $out if $out; print $err if $err; } else { eval "use Term::ReadKey;"; ReadMode('raw'); eval "END { ReadMode('restore') };"; $ssh->shell; print "Connection to $host closed.\n"; } == Is there any way to get all the fields properly. Thank you Pratap
Re: How can I open a remote ssh session with perl
I am stuck with this issue does any one have any idea about this ... This code(given below) is working fine for all the command but for some command it is not able to print the output. I am getting the following output == w 52 column window is too narrow Output of the same command on the same system while accessing it using ssh konsole 03:37:01 up 1 day, 12:20, 11 users, load average: 0.72, 0.52, 0.40 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT pratap :0 -23:00 ?xdm? 31:52 0.06s /bin/sh /usr/bin/startkde pratap pts/2:0 23:003.00s 0.55s 0.51s perl log.pl pratap pts/3:0 23:004:26m 1.51s 1.50s ssh -X pra...@192.168.3.31 == Similarly for top it is not printing all the field . top -cd2 top - 20:36:28 up 21:30, 18 users, load average: 1. Tasks: 38 total, 1 running, 37 sleeping, 0 sto Cpu(s): 8.4%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, Mem: 1025056k total, 1003548k used,21508k fre Swap: 2031608k total, 103608k used, 1928000k fre Actual top output on the same system is as below top -cd2 top - 20:37:05 up 21:30, 17 users, load average: 1.10, 1.07, 1.00 Tasks: 34 total, 1 running, 33 sleeping, 0 stopped, 0 zombie Cpu(s): 8.5%us, 0.4%sy, 0.0%ni, 90.8%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1025056k total, 994780k used,30276k free, 2940k buffers Swap: 2031608k total, 103600k used, 1928008k free,86892k cached #!/usr/bin/perl -w use strict; use Net::SSH::Perl; my $host = '192.168.0.114'; my $username = 'pratap'; my $login_passwd = 'pratap123'; my $ssh = Net::SSH::Perl->new($host, $username); $ssh->config->set('interactive', 1) unless defined $ssh->config->get('interactive'); $ssh->login($username, $login_passwd); my $cmd; if ($cmd) { my($out, $err, $exit) = $ssh->cmd($cmd); print $out if $out; print $err if $err; } else { eval "use Term::ReadKey;"; ReadMode('raw'); eval "END { ReadMode('restore') };"; $ssh->shell; print "Connection to $host closed.\n"; } == Is there any way to get all the fields properly. Thank you Pratap
How to parse email header
Hello , I am trying to write a script which can parse email header and based on certain criteria/parameter it will update the email subject detail in the database. I have written a code but it is taking too much time to execute the code and each process is utilizing 100% of its cpu resources allocated to the process. The code itself is too big so I can not paste it here since there are lots operation being performed on the email header after fetching the correct emails. However I am pasting the search string which I am using and I think it is only bugy. search_string => '(.*)(From:)(\s*)(.*)(\s*)(\...@*\s*)(\s*)(\S*\s*\S*\s*\S*\s*\S*\s*\S*)(s*)(.*)(\s*)(id)(\s*)(\w\w\w\w\w\w-\w\w\w\w\w\w-\w\w*)(\s*)(.*)(\s*)(for \...@\s*)(\s*)(;)(\s*)(\S*\s*\S*\s*\S*\s*\S*)(\s*)(\d\d:\d\d:\d\d)(.*)(\s*)(Subject:)(\s*)(.*)(\s*)[(Primary Hostname)](\s*)(.*)', I want to gather particular field from the email header along with the complete message. The field which I want to select from the email header are 1 From address (Filed number 6) 2 For address (Since mails being forwarded to my email address from else where) (Filed number 17) 3 Date and Time (Filed number 22 and 24) 4 Message ID (Filed number 14) 5 Complete Subject Line (Filed number 29) 6 Complete Body (Filed number 33) Above gives me the output but it takes too much time sometimes it takes 30 min if multiple mail comes to email address since I am writing these details in a file at the moment but I am worried because I want to update all these details to my database. Is there any way around to sort this out and it should not take more than a sec to complete the task. I know the above search string is not perfect and it fails if mail header format is different . If someone can shed some light on this. I have very less experience in perl programming so if you want to ask any thing please ask me and if some could come over chat then it will be really appreciated. Thank you Pratap
Re: How to parse email header
Here it is Return-path: Envelope-to: sea...@example.com Delivery-date: Sat, 14 Aug 2010 18:08:06 -0700 Received: from localhost.localdomain ([127.0.0.1]:58763 helo=[192.168.0.122]) by server.example.com with esmtpa (Exim 4.69) (envelope-from ) id 1OkRhm-0001XX-DY for sea...@example.com; Sat, 14 Aug 2010 18:08:06 -0700 Received: from 192.68.0.34 ([192.68.0.34]) (SquirrelMail authenticated user sea...@project.example.com) by 192.168.0.122 with HTTP; Sat, 14 Aug 2010 18:08:06 -0700 Message-ID: <74e8b2237739aa507ffd1f8428cb3d1c.squir...@192.168.0.122> Date: Sat, 14 Aug 2010 18:08:06 -0700 Subject: "[65515 - Seeking - Reply] Need help" From: no-re...@testdemo.com To: sea...@example.com User-Agent: SquirrelMail/1.4.20 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal sea...@example.com === There are other headers too and they are bit different than this one so can it be generalized. Thank you Pratap
Re: How to parse email header
I got the problem fixed by using Mail::Header perl module and it is better than before. Thanks for all your inputs , really appreciated. Rregards, Pratap
PERL MYSQL query "IF EXISTS then UPDATE else INSERT"
Hello, I want to update my database if the key value is already there then it will update the required field else it will insert the new row in the same table. Insert query works fine but I am not able to figure out how to achieve this "IF EXISTS then UPDATE else INSERT" using a one liner MYSQL statement . If someone can give me an example to write such query and execute it from perl then it would be really appreciated. Thank you. Pratap
Merge two files with similar column entries
Hi , I have few files which contains user name and data transfer rate in MBs and this data is collected for year and for each month report is saved in 12 different files I have to merge all the files to prepare the final report Files are as below Filename1 : January #User Name #Data Transferred A. Paul 34 Jason60 Mayur Pandey 4 Kelly H 459000 Ryan M 349000 Filename2 : March #User Name #Data Transferred Senthl V R 60 Mayur Pandey 4 Kelly H 459000 Pratap S 349000 A. Paul 34 Similarly I have 10 more files I have to merge all these files to one file and final output should be like below one: Final Report: #Username #January #March . #December #Total A. Paul 3434 Not available 68 Pratap SNot available332000 7899887 Kelly H 459000 459000459000 3424448274 Mayur Pandey 44040 242424442 Senthl V R Not available 6034544 53546464 Jason 60 Not available 3434343 43434355 I need a perl script to automate this rather using excel or doing manually . These reports are generated monthly and names are not stored in sorted order. Names are stored in recent user who has used data transfer facility. And also there are occasions when a user have not used data transfer at all for particular month in this case name of the user will not appear in monthly report file. In this case I have to add not available in the month name column. Thank you