Tamir.SharpSSH, C# and some linux shell command

I’ve found some interesting .NET class for accessing the linux server via SSH tunnel on this site – http://www.tamirgal.com/blog/page/SharpSSH.aspx. I’ve almost gave my task after experimenting the WinSCP active x plugin. I’ve decided to search again another code and found this library.
There are samples there on the source code but I’ve found the code ‘SSHExeTest.cs’ helpful for me.
Here is the revise code I have :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tamir.SharpSsh;
namespace WinSCP_Test
{
public partial class Form1 : Form
{
string _ipAddress = "192.168.0.160";
string _username = "root"; // :) dont worry i'mjust playing on virtualbox I think its better not to use root in production
string _password = "xxxxxxx";
SshExec exec;

public Form1()
{
InitializeComponent();
exec = new SshExec(_ipAddress, _username);
exec.Password = _password;
exec.Connect();
}
private bool isScriptRunning()
{

string command = "ps ax | grep -e '[t]est.py'";
string output = exec.RunCommand(command);
string[] output_arr = output.Trim().Split(' ');
return output != "";
}
private void runScript()
{

string command = "/usr/bin/python /root/test.py > /dev/null &";
exec.RunCommand(command);
//Console.WriteLine(output);

}
private void killScript()
{
string command = "ps ax | grep -e '[t]est.py'";

string output = exec.RunCommand(command);
string[] output_arr = output.Trim().Split(' ');
output = exec.RunCommand("kill -9 " + output_arr[0]);
}

 

private void button1_Click(object sender, EventArgs e)
{
if (this.isScriptRunning())
{
if (MessageBox.Show("Script is running kill it?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.killScript();
}
}
else
{
if (MessageBox.Show("Script is is not running,run it?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.runScript();
}

}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
exec.Close();
}
}
}

Fixing the “Permission denied” error on /var/lib/asterisk/agi-bin/dialparties.agi

A client calls us earlier  complaining that their  incoming calls are not going through. The call just drops  in. I’ve tried to trace also their log full the /var/log/asterisk/full confirming the issue.  Upon tracing the log file, I can see that there is entries that says “Permission denied”  during my call attempt.

 


[Aug 23 11:09:52] VERBOSE[32336] pbx.c: -- Executing [s@macro-dial:3] AGI("DAHDI/77-1", "dialparties.agi") in new stack
[Aug 23 11:09:52] VERBOSE[32336] res_agi.c: -- Launched AGI Script /var/lib/asterisk/agi-bin/dialparties.agi
[Aug 23 11:09:52] VERBOSE[32337] app_mixmonitor.c: == Begin MixMonitor Recording DAHDI/77-1
[Aug 23 11:09:52] VERBOSE[32336] res_agi.c: dialparties.agi: Failed to execute '/var/lib/asterisk/agi-bin/dialparties.agi': Permission denied
[Aug 23 11:09:52] VERBOSE[32336] pbx.c: -- Executing [s@macro-dial:4] NoOp("DAHDI/77-1", "Returned from dialparties with no extensions to call and DIALSTATUS: ") in new stack
[Aug 23 11:09:52] VERBOSE[32336] pbx.c: -- Executing [s@macro-exten-vm:10] GotoIf("DAHDI/77-1", "0?exit,return") in new stack
[

 

The issue though is just fixed by doing the chmod on /var/lib/asterisk/agi-bin/dialparties.agi.

Just do : chmod 777 /var/lib/asterisk/agi-bin/dialparties.agi

Calling again the  dialer server  after that ‘fix’  and my call went through.

 

PHP simple text file writer and reader code

For file writing

<?php
   $filename = "c:\\temp\\mydata.txt";
   $mystring = fopen($filename, "wb");
   $handle = fopen($filename, "wb");
   $newstring = str_replace("oldword", "newword", $mystring);
   $numbytes = fwrite($handle, $newstring);
   fclose($handle);
   print "$numbytes bytes written\n";
?>

For file reading

<?php
$lines = file('YourFile.txt');
foreach ($lines as $line_num => $line)
{
print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n";
}
?>

Google Chrome – No space between words in pdf

In my ubuntu Natty, the rendering of my php script  in google chrome  displays without spaces between words in pdf. But if i view that on other OS like win7 or winxp with google chrome, the script will just display fine.

It turns out that the problem is i need an MS true type core  fonts on my ubuntu. So here is the command:

apt-get install msttcorefonts
fc-cache -fv

This is free anyway, you just have to agree with the EULA during installation.