Using script to save dynamic network ip address to file? - ip

Is there a way I could use AppleScript to check for my network ip address? Or an alternative coding option? I need to save the ip address to a file if it has changed and then upload it to my ftp server with a date and time stamp. I have this part figured out its just checking for the address.

ifconfig.me just returns a plain text public IP:
do shell script "curl ifconfig.me > ip.txt"

Try:
set filePath to POSIX path of ((path to desktop as text) & "myIp.txt")
set myIp to do shell script "curl checkip.dyndns.org | grep -Eo [0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+ > " & quoted form of filePath

Related

SNMP TRAPS sending to other file, than /var/log/messages

I have configuration
snmptrapd.conf like below:
disableAuthorization yes
authCommunity log,execute,net public
I wanted to redirect all messages for other file, ex. /var/log/snmp.log, not for /var/log/messages. I tried also reconfigure rsyslog.conf file:
snmp.* /var/log/snmp.log
but I have error like that
sie 17 12:50:47 snmp rsyslogd[20398]: unknown facility name "snmp" [v8.24.0]
My question is, how to redirect all SNMP traps to other file by using rsyslog.conf or snmptrapd.conf
I know, that I can save output manually by using command like below, but I need working deamon as a service, not a single command from bash shell.
snmptrapd -f -Le -A -Lf /var/log/snmptrapd.log
You can use -t option with snmptrapd.
snmptrapd -tLf /your-log-location/yourlogfile.log --disableAutherization=yes
Try this:
# LOGFILE="path to logfile"
# specify the pathname of the logfile; if none or the empty string "" is
# given, use the syslog() mechanism to log the traps
# Default: ""

How to indicate a port number in a unix command (SCP) when copying files from a one remote server to another?

I have a case when there are 2 remote hosts, each with a unique port number, which i both need to indicate when using SCP command. There is no problem indicating the unique (non 22) port number for the first one, but i can't figure out how to indicate it for the second one.
Here is how it looks like now -
scp -P 1234 username#site.com:/path/to/file username2#site2.com:/path/to/directory
The way that you are running scp in your question, it would just invoke the scp program on the first host, telling it to send the file to the second host. You can directly invoke ssh to run the scp command on the first host however you like it:
ssh -p1234 user1#host1 'scp -P2345 /path/to/file user2#host2:/path/to/directory'
If the first host can't connect directly to the second host, you'd normally use the scp option -3 to send the data from the first host to the second host through you local host. If you're in that position, you could emulate how scp runs that kind of transfer, though it's sort of a hack:
yes "" | tr '\n' '\0' |
ssh -p1234 user1#host1 'scp -f /path/to/file' |
ssh -p2345 user2#host2 'scp -t /path/to/directory'
This is based on how the SCP protocol works. The first ssh line runs scp on the first remote host in "remote source" mode to send the source file. The second ssh lines runs scp on the second host in "remote sink" mode to receive the file and write it to the directory. "remote source" and "remote sink" are what scp normally runs on the remote system when receiving or sending files, respectively.
The yes "" | tr '\n' '\0' | line creates a stream of NUL (Ascii 0) bytes which are needed for the protocol to work. The scp -f instance reads these and won't send files without them. There are other ways to generate a stream of NUL bytes if you like.
no idea how to do this with arguments, but you can try editing ~/.ssh/config
Host test
HostName test.com
Port 22000
User me

Automating sftp using IBM AIX(UNIX) shell script

I am trying to automate my SFTP command using a UNIX shell script but for some reason it doesn't work. Here is my code as below. Please share your thoughts/insights.
#This ftp script will copy all the files from source directory into the local directory.
#!/bin/sh
HOST='sftp.xyz.com'
USER='ABC'
PASSWORD='123'
SRC_DIR='From_Src'
TRGT_DIR='/work/'
FILE='abc.txt'
sftp -u ${USER},${PASSWORD} sftp://${HOST} <<EOF
cd $SRC_DIR
lcd $TRGT_DIR
get $FILE
bye
EOF
echo "DONE"
When I try executing the above code I get the below error.
sftp: illegal option -- u
usage: sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
[-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
[-o ssh_option] [-P port] [-R num_requests] [-S program]
[-s subsystem | sftp_server] host
sftp [user#]host[:file ...]
sftp [user#]host[:dir[/]]
sftp -b batchfile [user#]host
There is no -u option for sftp, see the manual for available options. You can pass the username in this format:
sftp username#hostname
So in your case:
sftp sftp://${USER}#${HOST} <<EOF
This will prompt you the password though. If you don't want a password prompt, take a look at this topic: How to run the sftp command with a password from Bash script?
First, learn how to set up keys so that you can ssh, scp, and sftp to a server without a password. Look at ssh-keygen. It is fairly easy. I bet there are how tos on this site. In brief, generate your keys with ssh-keygen. They are created in ~/.ssh. Then add your public key to ~/.ssh/authorized_keys on the destination host where ~ is the home directory of the user you want to log in as. i.e. "ABC" in your example.
Now, you can just do "sftp ABC#sftp.xyz.com" and you will be at the sftp prompt on sftp.xyz.com. From there, getting your script to work should be easy.
My real suggestion is blow off sftp and use scp. e.g.
scp /path/to/the/source_file user#host:/remote/path/file
Its that simple. No "cd" and other gunk to deal with. You are making this way harder than it really is.
Good luck

Need to create a ksh script for transferring files between application server and db server

I have a requirement where i need to automate the transfer of a particular file from one location in the application server to the database server.
I can do it manually by using these steps but i need it in the script
#! /bin/ksh
directory path where the file need to be
ftp (hostname of the application server)
username
password
file location in the apps server
get filename
quit
Any help is Really appreciated....
Regards
Sam
You could have just searched it:
ftp -inv <<EOF
open app.server.address
user username password
cd some/location
get filename
bye
EOF
Expect is a great tool for scripting such scenarios:
#!/usr/bin/expect -f
spawn ftp ftp.ftpsite.com
expect "ftp> "
send -- "user MYUSER\r"
expect "Password: "
send -- "MYPASSWORD\r"
expect "ftp> "
send -- "cd /pub/test\r"
expect "ftp> "
send -- "put myfile\r"
expect "File transfered"
send -- "exit\r"

How to change shell in unix

I'm new to unix. I need to copy file over ssh. This is what I do
me#localhost ~ $ ssh you#remotehost
Then I established ssh so I get
you#remotehost ~ $
I'd like to use scp to copy files from localhost to remotehost. Once I have ssh connection, how do I change to prompt back to me#localhost so that I can use the scp command? Is there a command for that?
Edit: The reason I need the ssh is because after I copied the file I have to execute it. Is there a way to remain in the ssh session and use scp to copy the file that I'm editing at localhost
You do not have to first create an SSH connection to use SCP. Simply use the scp command from your shell, and it will connect to the other server.
Most shells exit with exit. CtrlD may also work.
You can also:
scp /path/to/local-file you#remotehost:/remote/path
Try screen command.
You can use scp on either side. Here are two examples:
If you are on your local host:
scp myfile you#remotehost:
If you are on the remote host:
scp you#<localhost's hostname>:myfile .
Substitute your localhost's hostname for <localhost's hostname> in the second command. If you are behind a router, it will be easier to use the first one.
Both assume that myfile is in the home directory on localhost and is being sent to the home directory on remotehost.

Resources