#P (PID con Port)
# v1.07 20/05/2008 sam@unix.ms
#
# If you have a Solaris 8, 9 or 10 box and you can't
# install lsof, try this. It maps PIDS to ports and vice versa.
# It also shows you which peers are connected on which port.
# Wildcards are accepted for -p and -P options.
#
# The script borrows Eric Steed's excellent "getport.sh" script.
#
if [ $# -lt 1 ]
then
echo >&2 "usage: $0 [-p PORT] [-P PID] [-a ALL ] (Wildcards OK)"
exit 1
fi
while getopts :p:P:a opt
do
case "${opt}" in
p ) port=${OPTARG};;
P ) pid=${OPTARG};;
a ) all=all;;
[?]) # unknown flag
echo >&2 "usage: $0 [-p PORT] [-P PID] [-a ALL ] (Wildcards OK) "
exit 1;;
esac
done
shift `expr $OPTIND - 1`
if [ $port ]
then
# Enter the port number, get the PID
#
port=${OPTARG}
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | grep -v ptree | awk '{print $1};'`
do
result=`pfiles $proc 2> /dev/null| grep "port: $port"`
if [ ! -z "$result" ]
then
program=`ps -fo comm -p $proc | tail -1`
echo "$proc\t$program\t$port\n$result"
echo "_________________________________________________________"
fi
done
elif [ $pid ]
then
# Enter the PID, get the port
#
pid=$OPTARG
# Print out the information
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | grep -v ptree | grep $pid| awk '{print $1};'`
do
result=`pfiles $proc 2> /dev/null| grep port:`
if [ ! -z "$result" ]
then
program=`ps -fo comm -p $pid | tail -1`
echo "$proc\t$program\n$result"
echo "_________________________________________________________"
fi
done
elif [ $all ]
then
# Show all PIDs, Ports and Peers
#
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for pid in `ptree -a | grep -v ptree |sort -n | awk '{print $1};'`
do
out=`pfiles $pid 2>/dev/null| grep "port:"`
if [ ! -z "$out" ]
then
name=`ps -fo comm -p $pid | tail -1`
echo "$pid\t$name\n$out"
echo "_________________________________________________________"
fi
done
fi
exit 0
Thursday, September 30, 2010
Wednesday, September 29, 2010
SFTP With com.jcraft.jsch
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
public class SCPUtil
{
private static final Logger _logger = Logger.getLogger(SCPUtil.class);
static FileInputStream utilConf = null;
static Properties props = null;
public static void main(String[] args) {
if (args.length < 1)
{
System.out.println("Usage SCPUtil <config file name> ");
}
else
{
System.out.println("Property File " + args[0]);
try {
utilConf = new FileInputStream(new File(args[0]));
props = new Properties();
props.load(utilConf);
String server = props.getProperty("hostname");
String loginId = props.getProperty("login");
System.out.println(server);
String passwd = props.getProperty("passowrd");
String localdrv = props.getProperty("localdrv");
String remotedrv = props.getProperty("remotedrv");
String file = props.getProperty("file");
get(server, loginId, passwd, localdrv, remotedrv, file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void get(String server, String loginId, String passwd, String localdrv, String remotedrv, String file)
throws Exception
{
boolean downloadStatus = false;
Session session = null;
try
{
JSch jsch = new JSch();
System.out.println(loginId);
session = jsch.getSession(loginId, server, 22);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.setPassword(passwd);
session.connect();
session.getHost();
session.getHostKey();
_logger.info("Connected to [" + server + "] with login " + loginId);
Channel channel = session.openChannel("sftp");
channel.connect();
_logger.info("Connected to Server :::" + server);
ChannelSftp c = (ChannelSftp)channel;
c.cd(remotedrv);
_logger.info("Remote address " + remotedrv);
Vector lst = c.ls(remotedrv);
for (int i = 0; i < lst.size(); i++)
{
ChannelSftp.LsEntry le = (ChannelSftp.LsEntry)lst.get(i);
_logger.info("if dirctory :::" + le.getAttrs().isDir());
if (le.getAttrs().isDir())
continue;
String fName1 = le.getFilename();
_logger.info("fName1::::" + fName1);
System.out.println("file name " + fName1);
if (!fName1.equals(file))
continue;
downloadStatus = true;
_logger.info("Downloading" + fName1);
System.out.println("Downloading file name ::" + fName1);
c.get(fName1, localdrv);
String name = null;
System.out.println("Downloaded file name ::" + fName1);
}
if (!downloadStatus)
throw new Exception("File not Found in Remote Directory", new FileNotFoundException());
c.quit();
}
catch (SftpException e) {
_logger.fatal("Problem with SFTP -" + e.getMessage());
throw e;
} catch (JSchException e) {
_logger.fatal("Problem with Connection -" + e.getMessage());
throw e;
}
catch (Exception e) {
_logger.fatal("Unknown Exception -" + e.getMessage());
throw e;
}
finally
{
if (session != null)
session.disconnect();
}
}
public static class MyUserInfo
implements UserInfo
{
public String getPassphrase()
{
return null;
}
public String getPassword()
{
return null;
}
public boolean promptPassword(String a)
{
return true;
}
public boolean promptPassphrase(String a)
{
return true;
}
public boolean promptYesNo(String a)
{
return true;
}
public void showMessage(String s)
{
}
}
}
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
public class SCPUtil
{
private static final Logger _logger = Logger.getLogger(SCPUtil.class);
static FileInputStream utilConf = null;
static Properties props = null;
public static void main(String[] args) {
if (args.length < 1)
{
System.out.println("Usage SCPUtil <config file name> ");
}
else
{
System.out.println("Property File " + args[0]);
try {
utilConf = new FileInputStream(new File(args[0]));
props = new Properties();
props.load(utilConf);
String server = props.getProperty("hostname");
String loginId = props.getProperty("login");
System.out.println(server);
String passwd = props.getProperty("passowrd");
String localdrv = props.getProperty("localdrv");
String remotedrv = props.getProperty("remotedrv");
String file = props.getProperty("file");
get(server, loginId, passwd, localdrv, remotedrv, file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void get(String server, String loginId, String passwd, String localdrv, String remotedrv, String file)
throws Exception
{
boolean downloadStatus = false;
Session session = null;
try
{
JSch jsch = new JSch();
System.out.println(loginId);
session = jsch.getSession(loginId, server, 22);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.setPassword(passwd);
session.connect();
session.getHost();
session.getHostKey();
_logger.info("Connected to [" + server + "] with login " + loginId);
Channel channel = session.openChannel("sftp");
channel.connect();
_logger.info("Connected to Server :::" + server);
ChannelSftp c = (ChannelSftp)channel;
c.cd(remotedrv);
_logger.info("Remote address " + remotedrv);
Vector lst = c.ls(remotedrv);
for (int i = 0; i < lst.size(); i++)
{
ChannelSftp.LsEntry le = (ChannelSftp.LsEntry)lst.get(i);
_logger.info("if dirctory :::" + le.getAttrs().isDir());
if (le.getAttrs().isDir())
continue;
String fName1 = le.getFilename();
_logger.info("fName1::::" + fName1);
System.out.println("file name " + fName1);
if (!fName1.equals(file))
continue;
downloadStatus = true;
_logger.info("Downloading" + fName1);
System.out.println("Downloading file name ::" + fName1);
c.get(fName1, localdrv);
String name = null;
System.out.println("Downloaded file name ::" + fName1);
}
if (!downloadStatus)
throw new Exception("File not Found in Remote Directory", new FileNotFoundException());
c.quit();
}
catch (SftpException e) {
_logger.fatal("Problem with SFTP -" + e.getMessage());
throw e;
} catch (JSchException e) {
_logger.fatal("Problem with Connection -" + e.getMessage());
throw e;
}
catch (Exception e) {
_logger.fatal("Unknown Exception -" + e.getMessage());
throw e;
}
finally
{
if (session != null)
session.disconnect();
}
}
public static class MyUserInfo
implements UserInfo
{
public String getPassphrase()
{
return null;
}
public String getPassword()
{
return null;
}
public boolean promptPassword(String a)
{
return true;
}
public boolean promptPassphrase(String a)
{
return true;
}
public boolean promptYesNo(String a)
{
return true;
}
public void showMessage(String s)
{
}
}
}
Subscribe to:
Comments (Atom)